47 #include "TObjString.h"
50 #include "reportingUtils.hpp"
59 const int errCode = 0)
61 cerr <<
"calculates difference of two amplitude files an writes result into tree" << endl
65 <<
" [-o out file -v -h] .amp file 1 .amp file 2" << endl
67 <<
" -o file path to output ROOT file (default: none)" << endl
68 <<
" -v verbose; print debug output (default: false)" << endl
69 <<
" -h print help" << endl
80 unsigned int& massBinMin,
81 unsigned int& massBinMax)
85 if ((pos = path.find_last_of(
"/")) == string::npos)
87 string massBinDir = path.substr(0, pos);
89 if ((pos = massBinDir.find_last_of(
"/")) == string::npos)
91 massBinDir = massBinDir.substr(0, pos);
93 if ((pos = massBinDir.find_last_of(
"/")) == string::npos)
95 massBinDir = massBinDir.substr(pos + 1);
97 if ((pos = massBinDir.find_last_of(
".")) == string::npos)
99 massBinMin =
atoi(massBinDir.substr(0, pos ).c_str());
100 massBinMax =
atoi(massBinDir.substr(pos + 1).c_str());
101 if ((massBinMin == 0) or (massBinMax == 0))
117 const string progName = argv[0];
118 string outFileName =
"";
120 string ampFileNames[2] = {
"",
""};
124 while ((c = getopt(argc, argv,
"o:vh")) != -1)
127 outFileName = optarg;
138 if (argc - optind != 2) {
139 printErr <<
"you have to specify two amplitude files. aborting." << endl;;
142 ampFileNames[0] = argv[optind++];
143 ampFileNames[1] = argv[optind++];
146 printInfo <<
"comparing amplitude files '" << ampFileNames[0] <<
"' and "
147 <<
"'" << ampFileNames[1] << endl;
148 ifstream ampFiles[2];
149 for (
unsigned int i = 0;
i < 2; ++
i) {
150 ampFiles[
i].open(ampFileNames[
i].c_str());
151 if (not ampFiles[
i]) {
152 printErr <<
"cannot open amplitude file '" << ampFileNames[
i] <<
"'. aborting." << endl;
158 vector<complex<double> > amps[2];
159 for (
unsigned int i = 0;
i < 2; ++
i) {
161 while (ampFiles[
i].read((
char*) &,
sizeof(complex<double>)))
162 amps[
i].push_back(amp);
164 if (amps[0].size() != amps[1].size())
165 printWarn <<
"the two amplitude files have different number of amplitudes "
166 <<
"(" << amps[0].size() <<
" vs. " << amps[1].size() <<
")." << endl;
167 const unsigned int nmbAmps = min(amps[0].size(), amps[1].size());
171 if (outFileName !=
"") {
172 printInfo <<
"writing difference tree to '" << outFileName <<
"'" << endl;
173 outFile = TFile::Open(outFileName.c_str(),
"RECREATE");
177 TTree* tree =
new TTree(
"ampDiffTree",
"ampDiffTree");
179 printErr <<
"problems creating tree 'ampDiffTree' " <<
"in file '" << outFileName <<
"'" << endl;
184 const string cmd =
"basename '" + ampFileNames[0] +
"'";
185 TObjString* ampName =
new TObjString(gSystem->GetFromPipe(cmd.c_str()));
187 UInt_t massBinMin = 0, massBinMax = 0;
195 printWarn <<
"cannot determine mass bin boundaries from file paths" << endl;
197 printInfo <<
"extracted mass bin boundaries [" << massBinMin <<
", " << massBinMax <<
"] from file paths" << endl;
201 const int bufSize = 256000;
202 tree->Branch(
"ampName",
"TObjString", &Name, bufSize, split);
203 tree->Branch(
"eventNmb", &eventNmb,
"eventNmb/i");
204 tree->Branch(
"massBinMin", &massBinMin,
"massBinMin/i");
205 tree->Branch(
"massBinMax", &massBinMax,
"massBinMax/i");
206 tree->Branch(
"valReal", valReal,
"valReal[2]/D");
207 tree->Branch(
"valImag", valImag,
"valImag[2]/D");
208 tree->Branch(
"absDiffReal", &absDiffReal,
"absDiffReal/D");
209 tree->Branch(
"absDiffImag", &absDiffImag,
"absDiffImag/D");
210 tree->Branch(
"relDiffReal", &relDiffReal,
"relDiffReal/D");
211 tree->Branch(
"relDiffImag", &relDiffImag,
"relDiffImag/D");
214 double maxAbsDiff = 0;
215 long int maxAbsDiffIndex = -1;
216 double maxRelDiff = 0;
217 long int maxRelDiffIndex = -1;
218 for (
unsigned int i = 0;
i < nmbAmps; ++
i) {
219 const complex<double> absDiff = amps[0][
i] - amps[1][
i];
220 const complex<double> relDiff = complex<double>(absDiff.real() / amps[0][
i].real(),
221 absDiff.imag() / amps[0][
i].imag());
225 valReal[0] = amps[0][
i].real();
226 valImag[0] = amps[0][
i].imag();
227 valReal[1] = amps[1][
i].real();
228 valImag[1] = amps[1][
i].imag();
229 absDiffReal = absDiff.real();
230 absDiffImag = absDiff.imag();
231 relDiffReal = relDiff.real();
232 relDiffImag = relDiff.imag();
237 const unsigned int nmbDigits = numeric_limits<double>::digits10 + 1;
239 s.precision(nmbDigits);
240 s.setf(ios_base::scientific, ios_base::floatfield);
241 s <<
" " << setw(49) << amps[0][
i] <<
" - " << setw(49) << amps[1][
i]
242 <<
" = " << setw(49) << absDiff <<
", relative "
243 <<
"(" << setw(23) << relDiff.real() <<
", " << setw(23) << relDiff.imag() <<
" )";
244 cout << s.str() << endl;
247 const double absMax = max(fabs(absDiffReal), fabs(absDiffImag));
248 const double relMax = max(fabs(relDiffReal), fabs(relDiffImag));
249 if (absMax > maxAbsDiff) {
253 if (relMax > maxRelDiff) {
258 printInfo <<
"maximum observed deviations: absolute = " << maxPrecision(maxAbsDiff) <<
" "
259 <<
"(event " << maxAbsDiffIndex <<
"), relative = " << maxPrecision(maxRelDiff) <<
" "
260 <<
"(event " << maxRelDiffIndex <<
")" << endl;
266 printInfo <<
"wrote difference tree to '" << outFileName <<
"'" << endl;