CMS 3D CMS Logo

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