ROOTPWA
testLikelihoodDiffPlots.C
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 // generates difference plots for two trees with log likelihood
29 // values and corresponding derivatives generated by
30 // testLikelihood
31 //
32 //
33 // Author List:
34 // Boris Grube TUM (original author)
35 //
36 //
37 //-------------------------------------------------------------------------
38 
39 
40 #include <iostream>
41 #include <sstream>
42 #include <string>
43 #include <map>
44 #include <algorithm>
45 #include <cmath>
46 
47 #include "TFile.h"
48 #include "TTree.h"
49 #include "TH1.h"
50 
51 
52 using namespace std;
53 
54 
55 void testLikelihoodDiffPlots(const string& inFileNameA = "testLikelihood.root",
56  const string& inFileNameB = "testLikelihood.root",
57  const string& outFileName = "testLikelihoodDiff.root",
58  const string& inTreeName = "testLikelihoodTree")
59 {
60  // open input files, get trees, and read number of parameters
61  const string inFileNames[2] = {inFileNameA, inFileNameB};
62  TFile* inFiles[2] = {0, 0};
63  TTree* inTrees[2] = {0, 0};
64  // leaf variables
65  Double_t logLikelihood[2] = {0, 0};
66  map<string, double>* derivatives [2] = {0, 0};
67  for (unsigned int i = 0; i < 2; ++i) {
68  inFiles[i] = TFile::Open(inFileNames[i].c_str(), "READ");
69  if (!inFiles[i] || inFiles[i]->IsZombie()) {
70  cout << "cannot open file '" << inFileNames[i] << "'. aborting." << endl;
71  throw;
72  }
73  inFiles[i]->GetObject(inTreeName.c_str(), inTrees[i]);
74  if (!inTrees[i]) {
75  cout << "cannot find tree '" << inTreeName << "'. aborting." << endl;
76  throw;
77  }
78  // connect leafs
79  inTrees[i]->SetBranchAddress("logLikelihood", &logLikelihood[i]);
80  inTrees[i]->SetBranchAddress("derivatives", &derivatives [i]);
81  inTrees[i]->GetEntry(0);
82  }
83  if (derivatives[0]->size() != derivatives[1]->size()) {
84  cout << "derivative arrays have different size: "
85  << derivatives[0]->size() << " vs. " << derivatives[1]->size() << ". aborting." << endl;
86  throw;
87  }
88 
89  // create output file
90  TFile* outFile = TFile::Open(outFileName.c_str(), "RECREATE");
91  if (!outFile || outFile->IsZombie()) {
92  cout << "cannot open file '" << outFileName << "'. aborting." << endl;
93  throw;
94  }
95 
96  // define histograms
97  TH1D* hLikelihoodDiffAbs = new TH1D
98  ("hLikelihoodDiffAbs", "hLikelihoodDiffAbs;L_{CPU} - L_{GPU};Count", 10000, -1e-7, 1e-7);
99  TH1D* hLikelihoodDiffRel = new TH1D
100  ("hLikelihoodDiffRel", "hLikelihoodDiffRel;1 - L_{GPU} / L_{CPU};Count", 10000, -1e-11, 1e-11);
101  TH1D* hDerivDiffTotAbs = new TH1D
102  ("hDerivDiffTotAbs", "hDerivDiffTotAbs;#nabla_{CPU} - #nabla_{GPU};Count",
103  10000, -1e-10, 1e-10);
104  TH1D* hDerivDiffTotRel = new TH1D
105  ("hDerivDiffTotRel", "hDerivDiffTotRel;1 - #nabla_{GPU} / #nabla_{CPU};Count",
106  10000, -1e-10, 1e-10);
107  map<string, TH1D*> hDerivDiffAbs;
108  map<string, TH1D*> hDerivDiffRel;
109  for (map<string, double>::const_iterator i = derivatives[0]->begin();
110  i != derivatives[0]->end(); ++i) {
111  stringstream suf;
112  suf << "_" << i->first;
113  hDerivDiffAbs[i->first] =
114  new TH1D(("hDerivDiffAbs" + suf.str()).c_str(),
115  ("hDerivDiffAbs" + suf.str() + ";#nabla_{CPU} - #nabla_{GPU};Count").c_str(),
116  10000, -1e-11, 1e-11);
117  hDerivDiffRel[i->first] =
118  new TH1D(("hDerivDiffRel" + suf.str()).c_str(),
119  ("hDerivDiffRel" + suf.str() + ";1 - #nabla_{GPU} / #nabla_{CPU};Count").c_str(),
120  10000, -1e-11, 1e-11);
121  }
122 
123  // loop over tree and fill histograms
124  // long int nmbEntries = min(inTrees[0]->GetEntries(), inTrees[1]->GetEntries());
125  long int nmbEntries = 100;
126  for (long int entry = 0; entry < nmbEntries; ++entry) {
127  Long64_t treeEntries[2] = {inTrees[0]->LoadTree(entry), inTrees[1]->LoadTree(entry)};
128  inTrees[0]->GetEntry(treeEntries[0]);
129  inTrees[1]->GetEntry(treeEntries[1]);
130 
131  hLikelihoodDiffAbs->Fill(logLikelihood[0] - logLikelihood[1]);
132  hLikelihoodDiffRel->Fill(1 - logLikelihood[1] / logLikelihood[0]);
133  // cout << "[" << entry << "] = " << logLikelihood[0] << " vs. " << logLikelihood[1] << endl;
134  for (map<string, double>::const_iterator i = derivatives[0]->begin();
135  i != derivatives[0]->end(); ++i) {
136  map<string, double>::const_iterator j = derivatives[1]->find(i->first);
137  if (j == derivatives[1]->end()) {
138  cout << "cannot find derivative for '" << i->first << "' in second tree. skipping." << endl;
139  continue;
140  }
141  double diffAbs = i->second - j->second;
142  double diffRel = 1 - j->second / i->second;
143  hDerivDiffAbs[i->first]->Fill(diffAbs);
144  hDerivDiffRel[i->first]->Fill(diffRel);
145  hDerivDiffTotAbs->Fill(diffAbs);
146  hDerivDiffTotRel->Fill(diffRel);
147  }
148  }
149 
150  // find gradients with maximum mean and RMS of relative difference
151  double maxMean = 0;
152  string maxMeanParName;
153  double maxRms = 0;
154  string maxRmsParName;
155  for (map<string, TH1D*>::const_iterator i = hDerivDiffRel.begin(); i != hDerivDiffRel.end(); ++i) {
156  const double mean = abs((double)i->second->GetMean());
157  if (mean > maxMean) {
158  maxMean = mean;
159  maxMeanParName = i->first;
160  }
161  const double rms = i->second->GetRMS();
162  if (rms > maxRms) {
163  maxRms = rms;
164  maxRmsParName = i->first;
165  }
166  }
167  cout << "max. mean of relative diff. = " << maxMean << " @ [" << maxMeanParName << "]" << endl;
168  cout << "max. RMS of relative diff. = " << maxRms << " @ [" << maxRmsParName << "]" << endl;
169 
170  // write outout and cleanup
171  inFiles[0]->Close();
172  inFiles[1]->Close();
173  outFile->Write();
174  outFile->Close();
175 }