CMS 3D CMS Logo

TestHistoMgr.cc
Go to the documentation of this file.
1 //#define StatTesting
2 //#define PI121
3 
5 #include <CLHEP/Units/SystemOfUnits.h>
6 #ifdef StatTesting
7 #include "Validation/SimG4GeometryValidation/interface/StatisticsComparator.h"
8 #include "StatisticsTesting/Chi2ComparisonAlgorithm.h"
9 #endif
10 
11 #ifdef PI121
12 #include "StatisticsTesting/ComparisonResult.h"
13 #endif
14 #include <iostream>
15 #include <cstdlib>
16 
17 //----------------------------------------------------------------------
19  // pi_aida::Proxy_Selector::setHistogramType(pi_aida::Histogram_Native);
20 }
21 
22 //----------------------------------------------------------------------
24  for (auto& it : theHistos1) {
25  delete it.second;
26  }
27  for (auto& it : theHistos2) {
28  delete it.second;
29  }
30  for (auto& it : theHistoProfs1) {
31  delete it.second;
32  }
33  for (auto& it : theHistoProfs2) {
34  delete it.second;
35  }
36 }
37 
38 //----------------------------------------------------------------------
40  edm::LogInfo("MaterialBudget") << "TestHistoMgr: Save user histos";
41 
42  TFile fout(name.c_str(), "recreate");
43 
44  // write out the histos
45  mih1::const_iterator ite1;
46  mih2::const_iterator ite2;
47  mihp1::const_iterator itep1;
48  mihp2::const_iterator itep2;
49 
50  for (ite1 = theHistos1.begin(); ite1 != theHistos1.end(); ite1++) {
51  ((*ite1).second)->Write();
52  }
53  for (ite2 = theHistos2.begin(); ite2 != theHistos2.end(); ite2++) {
54  ((*ite2).second)->Write();
55  }
56  for (itep1 = theHistoProfs1.begin(); itep1 != theHistoProfs1.end(); itep1++) {
57  ((*itep1).second)->Write();
58  }
59  for (itep2 = theHistoProfs2.begin(); itep2 != theHistoProfs2.end(); itep2++) {
60  ((*itep2).second)->Write();
61  }
62 }
63 
64 void TestHistoMgr::openSecondFile(const std::string& name) { theFileRef = std::make_unique<TFile>(name.c_str()); }
65 
67 #ifdef StatTesting
68 
69  TH1F* histo2 = getHisto1FromSecondFile(histo1->GetName());
70 
71  StatisticsTesting::StatisticsComparator<StatisticsTesting::Chi2ComparisonAlgorithm> comparator;
72 
73 #ifdef PI121
74 
75  qStatisticsTesting::ComparisonResult result = comparator.compare(*histo1, *histo2);
76 
77 #else
78 
79  double result = comparator.compare(*histo1, *histo2);
80 
81 #endif
82 
83  // ---------------------------------------------------------
84  // Do something with (e.g. print out) the result of the test
85  // ---------------------------------------------------------
86  edm::LogInfo << "TestHistoMgr: Result of the Chi2 Statistical Test: " << histo1->GetName();
87 #ifdef PI121
88  << " distance = " << result.distance() << std::endl
89  << " ndf = " << result.ndf() << std::endl
90  << " p-value = " << result.quality() << std::endl
91 #else
92  << " p-value = " << result << std::endl
93 #endif
94  << "---------------- exampleReadXML ENDS ------------------ " << std::endl;
95 #ifdef PI121
96  std::cout << "[OVAL]: HISTO_QUALITY " << histo1->GetName() << " " << result.quality() << std::endl;
97 #else
98  std::cout << "[OVAL]: HISTO_QUALITY " << histo1->GetName() << " " << result << std::endl;
99 #endif
100  std::cout << std::endl
101  << " mean= " << histo1->GetMean() << " " << histo1->GetName() << " " << histo1->GetTitle() << std::endl;
102  std::cout << " rms= " << histo1->GetRMS() << " " << histo1->GetName() << " " << histo1->GetTitle() << std::endl;
103 
104 #else
105 #endif
106 }
107 
108 bool TestHistoMgr::addHisto1(TH1F* sih) {
109  int ih = atoi(sih->GetName());
110  theHistos1[ih] = sih;
111  edm::LogInfo("MaterialBudget") << "TestHistoMgr: addHisto1: " << ih << " = " << sih->GetTitle();
112  return true;
113 }
114 
115 bool TestHistoMgr::addHisto2(TH2F* sih) {
116  int ih = atoi(sih->GetName());
117  theHistos2[ih] = sih;
118  return true;
119 }
120 
121 bool TestHistoMgr::addHistoProf1(TProfile* sih) {
122  int ih = atoi(sih->GetName());
123  theHistoProfs1[ih] = sih;
124  edm::LogInfo("MaterialBudget") << "TestHistoMgr: addHistoProf1: " << ih << " = " << sih->GetTitle();
125  return true;
126 }
127 
128 bool TestHistoMgr::addHistoProf2(TProfile2D* sih) {
129  int ih = atoi(sih->GetName());
130  theHistoProfs2[ih] = sih;
131  edm::LogInfo("MaterialBudget") << "TestHistoMgr: addHistoProf2: " << ih << " = " << sih->GetTitle();
132  return true;
133 }
134 
135 TH1F* TestHistoMgr::getHisto1(int ih) {
136  TH1F* his = nullptr;
137 
138  mih1::const_iterator ite = theHistos1.find(ih);
139  if (ite != theHistos1.end()) {
140  his = (*ite).second;
141  } else {
142  edm::LogError("MaterialBudget") << "TestHistoMgr: getHisto1 Histogram does not exist " << ih;
143  std::exception();
144  }
145  return his;
146 }
147 
148 TH2F* TestHistoMgr::getHisto2(int ih) {
149  TH2F* his = nullptr;
150  mih2::const_iterator ite = theHistos2.find(ih);
151  if (ite != theHistos2.end()) {
152  his = (*ite).second;
153  } else {
154  edm::LogError("MaterialBudget") << "TestHistoMgr: getHisto2 Histogram does not exist " << ih;
155  std::exception();
156  }
157  return his;
158 }
159 
160 TProfile* TestHistoMgr::getHistoProf1(int ih) {
161  TProfile* his = nullptr;
162  mihp1::const_iterator ite = theHistoProfs1.find(ih);
163  if (ite != theHistoProfs1.end()) {
164  his = (*ite).second;
165  } else {
166  edm::LogError("MaterialBudget") << "TestHistoMgr: Profile Histogram 1D does not exist " << ih;
167  std::exception();
168  }
169  return his;
170 }
171 
172 TProfile2D* TestHistoMgr::getHistoProf2(int ih) {
173  TProfile2D* his = nullptr;
174  mihp2::const_iterator ite = theHistoProfs2.find(ih);
175  if (ite != theHistoProfs2.end()) {
176  his = (*ite).second;
177  } else {
178  edm::LogError("MaterialBudget") << "TestHistoMgr: Profile Histogram 2D does not exist " << ih;
179  std::exception();
180  }
181  return his;
182 }
183 
184 TH1F* TestHistoMgr::getHisto1FromSecondFile(const char* hnam) {
185  TH1F* his = new TH1F();
186  if (!theFileRef) {
187  edm::LogError("MaterialBudget") << "TestHistoMgr: Second file not yet opened ";
188  std::exception();
189  } else {
190  his = (TH1F*)(*theFileRef).Get(hnam);
191  }
192 
193  if (!his) {
194  edm::LogError("MaterialBudget") << "TestHistoMgr: FATAL ERROR Histogram does not exist in second file " << hnam;
195  theFileRef->ls();
196  std::exception();
197  }
198  return his;
199 }
TProfile2D * getHistoProf2(int ih)
bool addHistoProf2(TProfile2D *ih)
bool addHisto1(TH1F *ih)
void openSecondFile(const std::string &name)
Definition: TestHistoMgr.cc:64
TProfile * getHistoProf1(int ih)
Log< level::Error, false > LogError
TH2F * getHisto2(int ih)
void save(const std::string &name)
Definition: TestHistoMgr.cc:39
Log< level::Info, false > LogInfo
mihp2 theHistoProfs2
Definition: TestHistoMgr.h:46
std::unique_ptr< TFile > theFileRef
Definition: TestHistoMgr.h:48
TH1F * getHisto1FromSecondFile(const char *hnam)
void printComparisonResult(int ih)
Definition: TestHistoMgr.cc:66
bool addHistoProf1(TProfile *ih)
TH1F * getHisto1(int ih)
mihp1 theHistoProfs1
Definition: TestHistoMgr.h:45
bool addHisto2(TH2F *ih)