CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TestHistoMgr.cc
Go to the documentation of this file.
1 //#include "Utilities/Configuration/interface/Architecture.h"
2 //#define StatTesting
3 //#define PI121
4 
6 #include "CLHEP/Units/GlobalSystemOfUnits.h"
7 #ifdef StatTesting
8 #include "Validation/SimG4GeometryValidation/interface/StatisticsComparator.h"
9 #include "StatisticsTesting/Chi2ComparisonAlgorithm.h"
10 #endif
11 
12 #ifdef PI121
13 #include "StatisticsTesting/ComparisonResult.h"
14 #endif
15 #include <iostream>
16 #include <cstdlib>
17 
18 //----------------------------------------------------------------------
20 {
21  // pi_aida::Proxy_Selector::setHistogramType(pi_aida::Histogram_Native);
22 
23 }
24 
25 
26 //----------------------------------------------------------------------
28 {
29  mih1::const_iterator ite1;
30  mih2::const_iterator ite2;
31  mihp1::const_iterator itep1;
32  mihp2::const_iterator itep2;
33 
34  for( ite1 = theHistos1.begin(); ite1 != theHistos1.end(); ite1++ ){
35  delete (*ite1).second;
36  }
37  for( ite2 = theHistos2.begin(); ite2 != theHistos2.end(); ite2++ ){
38  delete (*ite2).second;
39  }
40  for( itep1 = theHistoProfs1.begin(); itep1 != theHistoProfs1.end(); itep1++ ){
41  delete (*itep1).second;
42  }
43  for( itep2 = theHistoProfs2.begin(); itep2 != theHistoProfs2.end(); itep2++ ){ delete (*itep2).second;
44  }
45 
46 }
47 
48 
49 //----------------------------------------------------------------------
51 {
52 
53  std::cout << "=== save user histos ===" << std::endl;
54  TFile fout(name.c_str(),"recreate");
55  // write out the histos
56  mih1::const_iterator ite1;
57  mih2::const_iterator ite2;
58  mihp1::const_iterator itep1;
59  mihp2::const_iterator itep2;
60 
61  for( ite1 = theHistos1.begin(); ite1 != theHistos1.end(); ite1++ ){
62  ((*ite1).second)->Write();
63  }
64  for( ite2 = theHistos2.begin(); ite2 != theHistos2.end(); ite2++ ){
65  ((*ite2).second)->Write();
66  }
67  for( itep1 = theHistoProfs1.begin(); itep1 != theHistoProfs1.end(); itep1++ ){
68  ((*itep1).second)->Write();
69  }
70  for( itep2 = theHistoProfs2.begin(); itep2 != theHistoProfs2.end(); itep2++ ){
71  ((*itep2).second)->Write();
72  }
73 
74 }
75 
77 {
78 
79  theFileRef = new TFile(name.c_str());
80 
81  /* std::vector<std::string> objectNames = theStoreIn->listObjectNames();
82  std::vector<std::string> objectTypes = theStoreIn->listObjectTypes();
83  unsigned int siz = objectNames.size();
84  for( unsigned int ii = 0; ii < siz; ii++ ) {
85  // std::cout << " HISTOS IN FILE " << std::endl;
86 
87  // std::cout << " HISTO: " << objectNames[ii] << " " << objectTypes[ii] << std::endl;
88  }*/
89 }
90 
91 
93 {
94 
95  /*
96  TH1F* histo1 = getHisto1(ih);
97  */
98  /* std::cout << ih << " Histo1 " << histo1;
99  std::cout << histo1->GetName();
100  std::cout << histo1->title();
101  std::cout << histo1->entries();
102  std::cout << histo1->axis().bins() << std::endl;
103  */
104 
105 #ifdef StatTesting
106  TH1F* histo2 = getHisto1FromSecondFile(histo1->GetName());
107 
108  StatisticsTesting::StatisticsComparator< StatisticsTesting::Chi2ComparisonAlgorithm > comparator;
109 
110 // std::cout << " PrintComparisonResult " << histo1 << " " << histo2 << std::endl;
111 #ifdef PI121
112 qStatisticsTesting::ComparisonResult result = comparator.compare(*histo1, *histo2);
113 #else
114  double result = comparator.compare(*histo1, *histo2);
115 #endif
116 
117  // std::cout << " PrintComparisonResult " << histo1 << " " << histo2 << std::endl;
118 
119  // double distance = comparator.calculateDistance(histo1, histo2);
120  // ---------------------------------------------------------
121  // Do something with (e.g. print out) the result of the test
122  // ---------------------------------------------------------
123  std::cout << " Result of the Chi2 Statistical Test: " << histo1->GetName() << std::endl
124 #ifdef PI121
125  << " distance = " << result.distance() << std::endl
126  << " ndf = " << result.ndf() << std::endl
127  << " p-value = " << result.quality() << std::endl
128  #else
129  << " p-value = " << result << std::endl
130  #endif
131  << "---------------- exampleReadXML ENDS ------------------ "
132  << std::endl;
133 #ifdef PI121
134  std::cout << "[OVAL]: HISTO_QUALITY " << histo1->GetName() <<" " << result.quality() << std::endl;
135  #else
136  std::cout << "[OVAL]: HISTO_QUALITY " << histo1->GetName() <<" " << result << std::endl;
137  #endif
138  std::cout << std::endl << " mean= " << histo1->GetMean() << " " << histo1->GetName() << " " << histo1->GetTitle() << std::endl;
139  std::cout << " rms= " << histo1->GetRMS() << " " << histo1->GetName() << " " << histo1->GetTitle() << std::endl;
140 
141 #else
142  /* std::cout << " LOG histo " << histo1->GetName() << " mean = " << histo1->mean() << " rms = " << histo1->rms() << std::endl;
143  std::cout << " REF histo " << histo2->GetName() << " mean = " << histo2->mean() << " rms = " << histo2->rms() << std::endl;
144  std::cout << " [OVAL]: " << histo1->GetName() << " DIFF_MEAN " << fabs(histo1->mean()-histo2->mean()) << " DIFF_RMS " << fabs(histo1->rms()-histo2->rms()) << std::endl;
145  */
146 
147 #endif
148 }
149 
150 
151 bool TestHistoMgr::addHisto1( TH1F* sih )
152 {
153  int ih = atoi(sih->GetName());
154  theHistos1[ih] = sih;
155  std::cout << " addHisto1 " << sih->GetName() << " = " << ih << std::endl;
156 
157  return true;
158 }
159 
160 bool TestHistoMgr::addHisto2( TH2F* sih )
161 {
162  int ih = atoi(sih->GetName());
163  theHistos2[ih] = sih;
164 
165  return true;
166 }
167 
168 
169 bool TestHistoMgr::addHistoProf1( TProfile* sih )
170 {
171  int ih = atoi(sih->GetName());
172  theHistoProfs1[ih] = sih;
173  std::cout << " addProfHisto1 " << sih->GetName() << " = " << ih << std::endl;
174 
175  return true;
176 }
177 
178 bool TestHistoMgr::addHistoProf2( TProfile2D* sih )
179 {
180  int ih = atoi(sih->GetName());
181  theHistoProfs2[ih] = sih;
182  std::cout << " addProfHisto2 " << sih->GetName() << " = " << ih << std::endl;
183 
184  return true;
185 }
186 
187 
188 TH1F* TestHistoMgr::getHisto1( int ih )
189 {
190  TH1F* his = 0;
191 
192  mih1::const_iterator ite = theHistos1.find( ih );
193  if( ite != theHistos1.end() ) {
194  his = (*ite).second;
195  } else {
196  std::cerr << "!!!! FATAL ERROR Histogram does not exist " << ih << std::endl;
197  std::exception();
198  }
199  return his;
200 
201 }
202 
203 TH2F* TestHistoMgr::getHisto2( int ih )
204 {
205  TH2F* his = 0;
206  mih2::const_iterator ite = theHistos2.find( ih );
207  if( ite != theHistos2.end() ) {
208  his = (*ite).second;
209  } else {
210  std::cerr << "!!!! FATAL ERROR Histogram does not exist " << ih << std::endl;
211  std::exception();
212  }
213  return his;
214 }
215 
216 TProfile* TestHistoMgr::getHistoProf1( int ih )
217 {
218  TProfile* his = 0;
219  mihp1::const_iterator ite = theHistoProfs1.find( ih );
220  if( ite != theHistoProfs1.end() ) {
221  his = (*ite).second;
222  } else {
223  std::cerr << "!!!! FATAL ERROR Profile Histogram 1D does not exist " << ih << std::endl;
224  std::exception();
225  }
226 
227  return his;
228 
229 }
230 
231 
232 TProfile2D* TestHistoMgr::getHistoProf2( int ih )
233 {
234  TProfile2D* his = 0;
235  mihp2::const_iterator ite = theHistoProfs2.find( ih );
236  if( ite != theHistoProfs2.end() ) {
237  his = (*ite).second;
238  } else {
239  std::cerr << "!!!! FATAL ERROR Profile Histogram 2D does not exist " << ih << std::endl;
240  std::exception();
241  }
242 
243  return his;
244 
245 }
246 
247 TH1F* TestHistoMgr::getHisto1FromSecondFile( const char* hnam )
248 {
249  if( !theFileRef ){
250  std::cerr << "!!!! FATAL ERROR Second file not yet opened " << std::endl;
251  std::exception();
252  }
253 
254 
255  TH1F* his = (TH1F*)(*theFileRef).Get(hnam);
256  if( !his ) {
257  std::cerr << "!!!! FATAL ERROR Histogram does not exist in second file " << hnam << std::endl;
258  theFileRef->ls();
259  std::exception();
260  /* } else {
261  std::cout << " getHisto1FromSecondFile " << std::endl;
262  std::cout << his->GetName() << std::endl;
263  std::cout << his->title() << std::endl;
264  std::cout << his->entries() << std::endl;
265  std::cout << his->axis().bins() << std::endl;
266  std::cout << " end getHisto1FromSecondFile " << std::endl;
267  */
268  }
269 
270  return his;
271 
272 }
TProfile2D * getHistoProf2(int ih)
bool addHistoProf2(TProfile2D *ih)
bool addHisto1(TH1F *ih)
void openSecondFile(const std::string &name)
Definition: TestHistoMgr.cc:76
TProfile * getHistoProf1(int ih)
TH2F * getHisto2(int ih)
tuple result
Definition: query.py:137
void save(const std::string &name)
Definition: TestHistoMgr.cc:50
mihp2 theHistoProfs2
Definition: TestHistoMgr.h:47
TH1F * getHisto1FromSecondFile(const char *hnam)
void printComparisonResult(int ih)
Definition: TestHistoMgr.cc:92
bool addHistoProf1(TProfile *ih)
TH1F * getHisto1(int ih)
tuple cout
Definition: gather_cfg.py:121
mihp1 theHistoProfs1
Definition: TestHistoMgr.h:46
TFile * theFileRef
Definition: TestHistoMgr.h:49
bool addHisto2(TH2F *ih)