ROOTPWA
calcIntegrals.cc
Go to the documentation of this file.
1 
2 //
3 // Copyright 2010
4 //
5 // This file is part of rootpwa
6 //
7 // rootpwa is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // rootpwa is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with rootpwa. If not, see <http://www.gnu.org/licenses/>.
19 //
21 //-------------------------------------------------------------------------
22 // File and Version Information:
23 // $Rev:: $: revision of last commit
24 // $Author:: $: author of last commit
25 // $Date:: $: date of last commit
26 //
27 // Description:
28 // calculates integral matrix for set of amplitudes
29 //
30 //
31 // Author List:
32 // Boris Grube TUM (original author)
33 //
34 //
35 //-------------------------------------------------------------------------
36 
37 
38 #include <stdlib.h>
39 #include <unistd.h>
40 
41 #include "TROOT.h"
42 #include "TFile.h"
43 
44 #include "reportingUtils.hpp"
45 #include "fileUtils.hpp"
46 #include "ampIntegralMatrix.h"
47 
48 
49 using namespace std;
50 using namespace rpwa;
51 
52 
53 void
54 usage(const string& progName,
55  const int errCode = 0)
56 {
57  cerr << "computes integral matrix for given amplitude files" << endl
58  << endl
59  << "usage:" << endl
60  << progName
61  << " [-o output file -i TKey name -n max. # of events -r max. # of events -w weight file -v -h] "
62  << "amplitude files" << endl
63  << " where:" << endl
64  << " -o path path to output file (default: './norm.int')" << endl
65  << " -i name integral TKey name (only for .root format, default: 'integral')" << endl
66  << " -n # maximum number of events to process (default: all)" << endl
67  << " -r # number of events to renormalize to (default: no renormalization)" << endl
68  << " -w path path to MC weight file for de-weighting (default: none)" << endl
69  << " -v verbose; print debug output (default: false)" << endl
70  << " -h print help" << endl
71  << endl;
72  exit(errCode);
73 }
74 
75 
76 int
77 main(int argc,
78  char** argv)
79 {
80  printCompilerInfo();
81  printLibraryInfo ();
82  printSvnVersion ();
83  cout << endl;
84 
85  // force loading predefined std::complex dictionary
86  // see http://root.cern.ch/phpBB3/viewtopic.php?f=5&t=9618&p=50164
87  //gROOT->ProcessLine("#include <complex>");
88 
89  // parse command line options
90  const string progName = argv[0];
91  string outFileName = "./norm.int";
92  string integralName = "integral";
93  unsigned int maxNmbEvents = 0;
94  unsigned int nmbEventsRenorm = 0;
95  string weightFileName = "";
96  bool debug = false;
97  extern char* optarg;
98  extern int optind;
99  int c;
100  while ((c = getopt(argc, argv, "o:i:n:r:w:vh")) != -1)
101  switch (c) {
102  case 'o':
103  outFileName = optarg;
104  break;
105  case 'i':
106  integralName = optarg;
107  break;
108  case 'n':
109  maxNmbEvents = atoi(optarg);
110  break;
111  case 'r':
112  nmbEventsRenorm = atoi(optarg);
113  break;
114  case 'w':
115  weightFileName = optarg;
116  break;
117  case 'v':
118  debug = true;
119  break;
120  case 'h':
121  default:
122  usage(progName);
123  }
124 
125  // switch debug output
126  ampIntegralMatrix::setDebug(debug);
127 
128  // get input file names
129  if (optind >= argc) {
130  printErr << "you need to specify at least one amplitude file to process. aborting." << endl;;
131  usage(progName, 1);
132  }
133  vector<string> rootAmpFileNames;
134  vector<string> binAmpFileNames;
135  while (optind < argc) {
136  const string fileName = argv[optind++];
137  const string fileExt = extensionFromPath(fileName);
138  if (fileExt == "root") {
139 #ifdef USE_STD_COMPLEX_TREE_LEAFS
140  rootAmpFileNames.push_back(fileName);
141 #else
142  printErr << "reading of amplitudes in .root format not supported. "
143  << "upgrade your ROOT installation. skipping." << endl;
144 #endif
145  } else if (fileExt == "amp")
146  binAmpFileNames.push_back(fileName);
147  else
148  printWarn << "input file '" << fileName << "' is neither a .root nor a .amp file. "
149  << "skipping." << endl;
150  }
151  if ((rootAmpFileNames.size() == 0) and (binAmpFileNames.size() == 0)) {
152  printErr << "none of the specified input files is a .root or .amp file. aborting.";
153  usage(progName, 1);
154  }
155 
156  // calculate integral
158  integral.integrate(binAmpFileNames, rootAmpFileNames, maxNmbEvents, weightFileName);
159  if (nmbEventsRenorm > 0)
160  integral.renormalize(nmbEventsRenorm);
161 
162  // write out integral
163  const string outFileExt = extensionFromPath(outFileName);
164  if (outFileExt == "root") {
165 #ifdef USE_STD_COMPLEX_TREE_LEAFS
166  TFile* outFile = TFile::Open(outFileName.c_str(), "RECREATE");
167  if (not outFile) {
168  printErr << "cannot open output file '" << outFileName << "'. aborting." << endl;
169  exit(1);
170  }
171  const int nmbBytes = integral.Write(integralName.c_str());
172  outFile->Close();
173  if (nmbBytes == 0) {
174  printErr << "problems writing integral to TKey '" << integralName << "' "
175  << "in file '" << outFileName << "'" << endl;
176  exit(1);
177  } else
178  printSucc << "wrote integral to TKey '" << integralName << "' "
179  << "in file '" << outFileName << "'" << endl;
180 #else
181  printErr << "writing of integrals in .root format not supported. "
182  << "upgrade your ROOT installation. aborting." << endl;
183  exit(1);
184 #endif // USE_STD_COMPLEX_TREE_LEAFS
185  } else if (outFileExt == "int")
186  integral.writeAscii(outFileName);
187  else {
188  printErr << "output file '" << outFileName << "' should be either a .root or a .int file. "
189  << "aborting.";
190  exit(1);
191  }
192 
193  return 0;
194 }