CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/Validation/Geometry/src/TestHistoMgr.cc

Go to the documentation of this file.
00001 //#include "Utilities/Configuration/interface/Architecture.h"
00002 //#define StatTesting
00003 //#define PI121
00004 
00005 #include "Validation/Geometry/interface/TestHistoMgr.h"
00006 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00007 #ifdef StatTesting
00008 #include "Validation/SimG4GeometryValidation/interface/StatisticsComparator.h"
00009 #include "StatisticsTesting/Chi2ComparisonAlgorithm.h"
00010 #endif
00011 
00012 #ifdef PI121
00013 #include "StatisticsTesting/ComparisonResult.h"
00014 #endif
00015 #include <iostream>
00016 #include <cstdlib>
00017 
00018 //----------------------------------------------------------------------
00019 TestHistoMgr::TestHistoMgr()
00020 {
00021   //  pi_aida::Proxy_Selector::setHistogramType(pi_aida::Histogram_Native);
00022 
00023 }
00024  
00025 
00026 //----------------------------------------------------------------------
00027 TestHistoMgr::~TestHistoMgr()
00028 { 
00029   mih1::const_iterator ite1;
00030   mih2::const_iterator ite2;
00031   mihp1::const_iterator itep1;
00032   mihp2::const_iterator itep2;
00033 
00034   for( ite1 = theHistos1.begin(); ite1 != theHistos1.end(); ite1++ ){ 
00035     delete (*ite1).second;
00036   }
00037   for( ite2 = theHistos2.begin(); ite2 != theHistos2.end(); ite2++ ){
00038     delete (*ite2).second;
00039   }
00040   for( itep1 = theHistoProfs1.begin(); itep1 != theHistoProfs1.end(); itep1++ ){
00041     delete (*itep1).second;
00042   }
00043   for( itep2 = theHistoProfs2.begin(); itep2 != theHistoProfs2.end(); itep2++ ){    delete (*itep2).second;
00044   }
00045  
00046 }
00047 
00048 
00049 //----------------------------------------------------------------------
00050 void TestHistoMgr::save( const std::string& name )
00051 {
00052 
00053   std::cout << "=== save user histos ===" << std::endl;
00054   TFile fout(name.c_str(),"recreate");
00055   // write out the histos
00056   mih1::const_iterator ite1;
00057   mih2::const_iterator ite2;
00058   mihp1::const_iterator itep1;
00059   mihp2::const_iterator itep2;
00060 
00061   for( ite1 = theHistos1.begin(); ite1 != theHistos1.end(); ite1++ ){
00062     ((*ite1).second)->Write();
00063   }
00064   for( ite2 = theHistos2.begin(); ite2 != theHistos2.end(); ite2++ ){
00065     ((*ite2).second)->Write();
00066   }
00067   for( itep1 = theHistoProfs1.begin(); itep1 != theHistoProfs1.end(); itep1++ ){
00068     ((*itep1).second)->Write();
00069   }
00070   for( itep2 = theHistoProfs2.begin(); itep2 != theHistoProfs2.end(); itep2++ ){
00071     ((*itep2).second)->Write();
00072   }
00073 
00074 }
00075 
00076 void TestHistoMgr::openSecondFile( const std::string& name )
00077 {
00078 
00079   theFileRef = new TFile(name.c_str());
00080 
00081   /*  std::vector<std::string> objectNames = theStoreIn->listObjectNames();
00082   std::vector<std::string> objectTypes = theStoreIn->listObjectTypes(); 
00083   unsigned int siz = objectNames.size();
00084   for( unsigned int ii = 0; ii < siz; ii++ ) {
00085     //    std::cout << " HISTOS IN FILE " << std::endl;
00086 
00087     //   std::cout << " HISTO: " << objectNames[ii] << " " << objectTypes[ii] << std::endl;
00088     }*/
00089 }
00090 
00091 
00092 void TestHistoMgr::printComparisonResult( int ih )
00093 {
00094 
00095   /*
00096     TH1F* histo1 = getHisto1(ih);
00097   */
00098   /*  std::cout << ih << " Histo1 " << histo1;
00099       std::cout << histo1->GetName();
00100       std::cout << histo1->title();
00101       std::cout << histo1->entries();
00102       std::cout << histo1->axis().bins() << std::endl;
00103   */
00104   
00105 #ifdef StatTesting
00106   TH1F* histo2 = getHisto1FromSecondFile(histo1->GetName());
00107   
00108   StatisticsTesting::StatisticsComparator< StatisticsTesting::Chi2ComparisonAlgorithm > comparator;
00109 
00110 //  std::cout << " PrintComparisonResult " << histo1 << " " << histo2 << std::endl;
00111 #ifdef PI121
00112 qStatisticsTesting::ComparisonResult result = comparator.compare(*histo1, *histo2); 
00113 #else
00114   double result = comparator.compare(*histo1, *histo2); 
00115 #endif
00116 
00117   //  std::cout << " PrintComparisonResult " << histo1 << " " << histo2 << std::endl;
00118 
00119   //  double distance  = comparator.calculateDistance(histo1, histo2);
00120   // ---------------------------------------------------------
00121   // Do something with (e.g. print out) the result of the test 
00122   // ---------------------------------------------------------
00123   std::cout << " Result of the Chi2 Statistical Test: " << histo1->GetName() << std::endl
00124 #ifdef PI121
00125             << " distance = " << result.distance() << std::endl
00126             << " ndf = " << result.ndf() << std::endl
00127             << " p-value = " << result.quality() << std::endl
00128  #else
00129             << " p-value = " << result << std::endl
00130  #endif
00131             << "---------------- exampleReadXML  ENDS   ------------------ " 
00132             << std::endl;
00133 #ifdef PI121
00134   std::cout << "[OVAL]: HISTO_QUALITY " << histo1->GetName() <<" " << result.quality() << std::endl;
00135  #else 
00136   std::cout << "[OVAL]: HISTO_QUALITY " << histo1->GetName() <<" " << result << std::endl;
00137  #endif
00138   std::cout << std::endl << " mean= " << histo1->GetMean() << " " << histo1->GetName() << " " << histo1->GetTitle() << std::endl;
00139   std::cout << " rms= " << histo1->GetRMS() << " " << histo1->GetName() << " " << histo1->GetTitle() << std::endl;
00140 
00141 #else
00142   /*  std::cout << " LOG histo " << histo1->GetName() << " mean = " << histo1->mean() << " rms = " << histo1->rms() << std::endl;
00143   std::cout << " REF histo " << histo2->GetName() << " mean = " << histo2->mean() << " rms = " << histo2->rms() << std::endl;
00144   std::cout << " [OVAL]: " << histo1->GetName() << " DIFF_MEAN " << fabs(histo1->mean()-histo2->mean()) << " DIFF_RMS " << fabs(histo1->rms()-histo2->rms()) << std::endl;
00145   */
00146 
00147 #endif
00148 }
00149 
00150 
00151 bool TestHistoMgr::addHisto1( TH1F* sih )
00152 {
00153   int ih = atoi(sih->GetName());
00154   theHistos1[ih] = sih;
00155   std::cout << " addHisto1 " << sih->GetName() << " = " << ih << std::endl;
00156 
00157   return true;
00158 }
00159 
00160 bool TestHistoMgr::addHisto2( TH2F* sih )
00161 {
00162   int ih = atoi(sih->GetName());
00163   theHistos2[ih] = sih;
00164 
00165   return true;
00166 }
00167 
00168 
00169 bool TestHistoMgr::addHistoProf1( TProfile* sih )
00170 {
00171   int ih = atoi(sih->GetName());
00172   theHistoProfs1[ih] = sih;
00173   std::cout << " addProfHisto1 " << sih->GetName() << " = " << ih << std::endl;
00174 
00175   return true;
00176 }
00177 
00178 bool TestHistoMgr::addHistoProf2( TProfile2D* sih )
00179 {
00180   int ih = atoi(sih->GetName());
00181   theHistoProfs2[ih] = sih;
00182   std::cout << " addProfHisto2 " << sih->GetName() << " = " << ih << std::endl;
00183 
00184   return true;
00185 }
00186 
00187 
00188 TH1F* TestHistoMgr::getHisto1( int ih )
00189 {
00190   TH1F* his = 0;
00191 
00192   mih1::const_iterator ite = theHistos1.find( ih );
00193   if( ite != theHistos1.end() ) {
00194     his = (*ite).second;
00195   } else {
00196     std::cerr << "!!!! FATAL ERROR Histogram does not exist " << ih << std::endl;
00197     std::exception();
00198   }
00199   return his;
00200 
00201 }
00202 
00203 TH2F* TestHistoMgr::getHisto2( int ih )
00204 {
00205   TH2F* his = 0;
00206   mih2::const_iterator ite = theHistos2.find( ih );
00207   if( ite != theHistos2.end() ) {
00208     his = (*ite).second;
00209   } else {
00210     std::cerr << "!!!! FATAL ERROR Histogram does not exist " << ih << std::endl;
00211     std::exception();
00212   }
00213   return his;
00214 }
00215 
00216 TProfile* TestHistoMgr::getHistoProf1( int ih )
00217 {
00218   TProfile* his = 0;
00219   mihp1::const_iterator ite = theHistoProfs1.find( ih );
00220   if( ite != theHistoProfs1.end() ) {
00221    his = (*ite).second;
00222   } else {
00223     std::cerr << "!!!! FATAL ERROR Profile Histogram 1D does not exist " << ih << std::endl;
00224     std::exception();
00225   }
00226 
00227   return his;
00228 
00229 }
00230 
00231 
00232 TProfile2D* TestHistoMgr::getHistoProf2( int ih )
00233 {
00234   TProfile2D* his = 0;
00235   mihp2::const_iterator ite = theHistoProfs2.find( ih );
00236   if( ite != theHistoProfs2.end() ) {
00237    his = (*ite).second;
00238   } else {
00239     std::cerr << "!!!! FATAL ERROR Profile Histogram 2D does not exist " << ih << std::endl;
00240     std::exception();
00241   }
00242 
00243   return his;
00244 
00245 }
00246  
00247 TH1F* TestHistoMgr::getHisto1FromSecondFile( const char* hnam )
00248 {
00249   if( !theFileRef ){
00250     std::cerr << "!!!! FATAL ERROR Second file not yet opened " << std::endl;
00251     std::exception();
00252   }
00253 
00254 
00255   TH1F* his = (TH1F*)(*theFileRef).Get(hnam);
00256   if( !his ) {
00257     std::cerr << "!!!! FATAL ERROR Histogram does not exist in second file " << hnam << std::endl;
00258     theFileRef->ls();
00259     std::exception();
00260     /*  } else {
00261         std::cout << " getHisto1FromSecondFile " << std::endl;
00262     std::cout << his->GetName() << std::endl;
00263     std::cout << his->title() << std::endl;
00264     std::cout << his->entries() << std::endl;
00265     std::cout << his->axis().bins() << std::endl;
00266     std::cout << " end getHisto1FromSecondFile " << std::endl;
00267     */
00268   }
00269 
00270   return his;
00271 
00272 }