ROOTPWA
compareTFitBins.C
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <string>
4 #include <cassert>
5 #include <algorithm>
6 #include <map>
7 
8 #include "TFile.h"
9 #include "TTree.h"
10 
11 #include "../TFitBin.h"
12 
13 
14 using namespace std;
15 
16 
17 bool
19  TFitBin& binB,
20  const double threshold,
21  map<string, double>& maxDeviations)
22 {
23  if (binA.mass() != binB.mass()) {
24  cout << "bins have different mass: " << binA.mass() << " vs. " << binB.mass() << endl;
25  return false;
26  }
27 
28  if (binA.rawEvents() != binB.rawEvents()) {
29  cout << "bins have different number of events: " << binA.rawEvents() << " vs. " << binB.rawEvents() << endl;
30  return false;
31  }
32 
33  if (binA.nwaves() != binB.nwaves()) {
34  cout << "bins have different number of waves: " << binA.nwaves() << " vs. " << binB.nwaves() << endl;
35  return false;
36  }
37  const int nmbWaves = binA.nwaves();
38  bool binsAreEqual = true;
39  for (int i = 0; i < nmbWaves; ++i)
40  if (binA.waveDesignator(i) != binB.waveDesignator(i)) {
41  cout << "bins have different wave designator at index " << setw(2) << i << ": " << binA.waveDesignator(i) << " vs. " << binB.waveDesignator(i) << endl;
42  binsAreEqual = false;
43  }
44  if (!binsAreEqual)
45  return false;
46 
47  const double likelihoodDiff = binA.logli() - binB.logli();
48  if (likelihoodDiff > threshold) {
49  cout << "likelihood difference " << likelihoodDiff << " is above threshold." << endl;
50  //return false;
51  }
52 
53  // compare amplitudes
54  if (binA.namps() != binB.namps()) {
55  cout << "bins have different number of amplitudes: " << binA.namps() << " vs. " << binB.namps() << endl;
56  //return false;
57  }
58  // const int nmbAmps = binA.namps();
59 // maxDeviations["amplitude"] = 0;
60 // for (int i = 0; i < nmbAmps; ++i) {
61 // const TComplex diff = binA.amp(i) - binB.amp(i);
62 // if (diff.Re() > maxDeviations["amplitude"])
63 // maxDeviations["amplitude"] = diff.Re();
64 // if (diff.Im() > maxDeviations["amplitude"])
65 // maxDeviations["amplitude"] = diff.Im();
66 // if ((diff.Re() > threshold) || (diff.Im() > threshold)) {
67 // cout << " --- "<< binA.wavename(i) << endl;
68 // cout << " --- "<< binB.wavename(i) << endl;
69 // cout << "amplitude difference " << setw(2) << i << " is above threshold: " << binA.amp(i) << " - " << binB.amp(i) << " = " << diff << endl;
70 // binsAreEqual = false;
71 // }
72 // }
73 
74  // compare intensities
75  maxDeviations["intensity"] = 0;
76  for (int i = 0; i < nmbWaves; ++i) {
77  const double diff = binA.intens(i) - binB.intens(binA.wavetitle(i));
78  if (diff > maxDeviations["intensity"])
79  maxDeviations["intensity"] = diff;
80  if (diff > threshold)
81  {
82  cout << " --- "<< binA.wavetitle(i) << endl;
83  cout << " --- "<< binB.wavetitle(i) << endl;
84  cout << "intensity difference " << setw(2) << i << " is above threshold: " << binA.intens(i) << " - " << binB.intens(i) << " = " << diff << endl;
85  binsAreEqual = false;
86  }
87  }
88  // compare intensity errors
89  maxDeviations["intensity error"] = 0;
90  for (int i = 0; i < nmbWaves; ++i) {
91  const double diff = binA.err(i) - binB.err(binA.wavetitle(i));
92  if (diff > maxDeviations["intensity error"])
93  maxDeviations["intensity error"] = diff;
94  if (diff > threshold) {
95  cout << "difference of intensity error " << setw(2) << i << " is above threshold: " << binA.err(i) << " - " << binB.err(binA.wavetitle(i)) << " = " << diff << endl;
96  binsAreEqual = false;
97  }
98  }
99 
100  if (!binsAreEqual)
101  return false;
102  else
103  return true;
104 }
105 
106 
107 bool
108 compareTFitBins(const string& fileNameA = "result.2000.root",
109  const string& fileNameB = "result.root",
110  const double threshold = 0.5)
111 {
112  const string fileNames[2] = {fileNameA,
113  fileNameB};
114  const string treeName = "pwa";
115  const string branchName = "fitbin";
116 
117  cout << "comparing TFitBin trees in " << fileNames[0] << " and " << fileNames[1] << "." << endl;
118 
119  // open files
120  TFile* f[2];
121  for (unsigned int i = 0; i < 2; ++i) {
122  f[i] = TFile::Open(fileNames[i].c_str(), "READ");
123  if (!f[i] || f[i]->IsZombie()) {
124  cerr << "cannot open file '" << fileNames[i] << "'." << endl;
125  return false;
126  }
127  }
128 
129  // read in fit bins
130  TFitBin* b[2];
131  TTree* t[2];
132  for (unsigned int i = 0; i < 2; ++i) {
133  f[i]->GetObject(treeName.c_str(), t[i]);
134  if (!t[i]) {
135  cerr << "cannot find tree '"<< treeName << "' in file '" << fileNames[i] << "'." << endl;
136  return false;
137  } else {
138  b[i] = new TFitBin();
139  t[i]->SetBranchAddress(branchName.c_str(), &b[i]);
140  }
141  }
142 
143  // compare tree entries
144  map<string, double> maxDeviations;
145  bool treesEqual = true;
146  const unsigned int nmbEntries[2] = {t[0]->GetEntries(),
147  t[1]->GetEntries()};
148  if (nmbEntries[0] != nmbEntries[1])
149  cerr << "warning: trees have not the same number of entries: " << nmbEntries[0] << " != " << nmbEntries[1] << ". comparing up to smaller index." << endl;
150  const unsigned int maxNmbEntries = min(nmbEntries[0], nmbEntries[1]);
151  for (unsigned int i = 0; i < maxNmbEntries; ++i) {
152  t[0]->GetEntry(i);
153  t[1]->GetEntry(i);
154  map<string, double> deviations;
155  if (!binsEqual(*b[0], *b[1], threshold, deviations))
156  treesEqual = false;
157  for (map<string, double>::const_iterator i = deviations.begin(); i != deviations.end(); ++i)
158  if ((maxDeviations.find(i->first) == maxDeviations.end()) || (i->second > maxDeviations[i->first]))
159  maxDeviations[i->first] = i->second;
160  }
161 
162  cout << "maximum deviations:" << endl;
163  for (map<string, double>::const_iterator i = maxDeviations.begin(); i != maxDeviations.end(); ++i)
164  cout << " " << i->first << " = " << i->second << "; threshold = " << threshold << endl;
165 
166  // cleanup
167  for (unsigned int i = 0; i < 2; ++i)
168  if (f[i]) {
169  f[i]->Close();
170  delete f[i];
171  }
172  return treesEqual;
173 }