CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/Validation/RecoJets/src/ManipHist.cc

Go to the documentation of this file.
00001 #include "Validation/RecoJets/interface/ManipHist.h"
00002 #include "Validation/RecoJets/interface/RootPostScript.h"
00003 #include <cmath>
00004 
00005 using namespace std;
00006 
00007 void 
00008 ManipHist::configBlockSum(ConfigFile& cfg)
00009 {
00010   try{
00011     //-----------------------------------------------
00012     // histogram manipulations
00013     //-----------------------------------------------  
00014     readVector( cfg.read<std::string>( "histWeights" ), weights_);
00015   }
00016   catch(...){
00017     std::cerr << "ERROR during reading of config file" << std::endl;
00018     std::cerr << "      misspelled variables in cfg ?" << std::endl;
00019     std::cerr << "      [--called in configBlockSum]"  << std::endl;
00020     std::exit(1);
00021   }
00022 }
00023 
00024 void 
00025 ManipHist::configBlockDivide(ConfigFile& cfg)
00026 {
00027   try{
00028     //-----------------------------------------------
00029     // histogram manipulations
00030     //-----------------------------------------------
00031     errorType_ = cfg.read<int>( "errorType" );
00032   }
00033   catch(...){
00034     std::cerr << "ERROR during reading of config file"   << std::endl;
00035     std::cerr << "      misspelled variables in cfg ?"   << std::endl;
00036     std::cerr << "      [--called in configBlockDivide]" << std::endl;
00037     std::exit(1);
00038   }
00039 }
00040 
00041 void 
00042 ManipHist::sumHistograms()
00043 {
00044   //-----------------------------------------------
00045   // loop histograms via the list of histogram
00046   // names stored in histList_, open a new page 
00047   // for each histogram & plot each sample in 
00048   // the same canvas
00049   //-----------------------------------------------
00050   for(int idx=0; idx<(int)histList_.size(); ++idx){
00051     //-----------------------------------------------
00052     // loop all samples via the list sampleList_, which 
00053     // containas the histograms of each sample as 
00054     // TObjects in TObjectArrays
00055     //-----------------------------------------------
00056     TH1F buffer;
00057     std::vector<TObjArray>::const_iterator hist = sampleList_.begin();
00058     for(int jdx=0; hist!=sampleList_.end(); ++hist, ++jdx){
00059       TH1F& hadd = *((TH1F*)(*hist)[idx]);
00060       //apply weights if required
00061       if( jdx<((int)weights_.size()) ){
00062         if( weights_[jdx]>0 ){
00063           hadd.Scale(weights_[jdx]);
00064         }
00065       }
00066       //buffer first histogram unchanged
00067       if(jdx==0){
00068         buffer = hadd;
00069       }
00070       //add histograms buffer last one
00071       else{
00072         hadd.Add(&buffer);
00073         buffer = hadd;
00074       }
00075     }
00076     //reset maximum for all histograms
00077     hist = sampleList_.begin();
00078     for(; hist!=sampleList_.end(); ++hist){
00079       TH1F& hadd = *((TH1F*)(*hist)[idx]);
00080       hadd.SetMaximum(buffer.GetMaximum());
00081     }
00082   }
00083 }
00084 
00085 void 
00086 ManipHist::divideAndDrawPs()
00087 {
00088   //-----------------------------------------------
00089   // define canvas
00090   //-----------------------------------------------
00091   TCanvas *canv = new TCanvas("canv", "histograms", 600, 600);
00092   setCanvasStyle( *canv  );
00093 
00094   //-----------------------------------------------
00095   // number of files/directories in root file must 
00096   // be >1 otherwise return with error message
00097   //-----------------------------------------------
00098   if((sampleList_.size()!=2) && (dirNameList_.size()+fileList_.size()!=3)){
00099     std::cerr << "ERROR number of indicated root files/directories " << std::endl;
00100     std::cerr << "      is insonsistent. Need sample & reference"    << std::endl;
00101     std::cerr << "      file/directory being specified solitarily"   << std::endl;
00102     return;
00103   }
00104   //-----------------------------------------------
00105   // open output file
00106   //-----------------------------------------------
00107   TString output( writeTo_.c_str() );
00108   output += "/";
00109   output += "inspectRatio";
00110   output += ".";
00111   output += writeAs_;
00112   TPostScript psFile( output, 111); //112 for portrait
00113   //-----------------------------------------------
00114   // loop histograms via the list of histogram
00115   // names stored in histList_, open a new page 
00116   // for each histogram & plot each sample in 
00117   // the same canvas, divide 
00118   //-----------------------------------------------
00119   for(int idx=0; idx<(int)histList_.size(); ++idx){
00120     psFile.NewPage();
00121     TH1F& hsam = *((TH1F*)(sampleList_[0])[idx] ); //recieve sample
00122     TH1F& href = *((TH1F*)(sampleList_[1])[idx] ); //recieve reference
00123     divideHistograms(hsam, href, errorType_);
00124     
00125     setCanvLog( *canv, idx );
00126     setCanvGrid( *canv, idx );
00127     setHistStyles( hsam, idx, 0 ); //there is only one sample
00128     
00129     hsam.Draw();
00130     canv->RedrawAxis( );
00131     canv->Update( );
00132     if(idx == (int)histList_.size()-1){
00133       psFile.Close();
00134     }
00135   }
00136   canv->Close();
00137   delete canv;
00138 }
00139 
00140 void 
00141 ManipHist::divideAndDrawEps()
00142 {
00143   //-----------------------------------------------
00144   // define canvas
00145   //-----------------------------------------------
00146   TCanvas *canv = new TCanvas("canv", "histograms", 600, 600);
00147   setCanvasStyle( *canv  );
00148 
00149   //-----------------------------------------------
00150   // number of files/directories in root file must 
00151   // be >1 otherwise return with error message
00152   //-----------------------------------------------
00153   if((sampleList_.size()!=2) && (dirNameList_.size()+fileList_.size()!=3)){
00154     std::cerr << "ERROR number of indicated root files/directories " << std::endl;
00155     std::cerr << "      is insonsistent. Need sample & reference"    << std::endl;
00156     std::cerr << "      file/directory being specified solitarily"   << std::endl;
00157     return;
00158   }
00159   //-----------------------------------------------
00160   // loop histograms via the list of histogram
00161   // names stored in histList_, open a new page 
00162   // for each histogram & plot each sample in 
00163   // the same canvas
00164   //-----------------------------------------------  
00165   for(int idx=0; idx<(int)histList_.size(); ++idx){
00166     //-----------------------------------------------
00167     // open output files
00168     //-----------------------------------------------
00169     TString output( writeTo_.c_str() );
00170     output += "/";
00171     output += histList_[ idx ];
00172     output += ".";
00173     output += writeAs_;
00174     TPostScript psFile( output, 113);
00175     psFile.NewPage();
00176     TH1F& hsam = *((TH1F*)(sampleList_[0])[idx] ); //recieve sample
00177     TH1F& href = *((TH1F*)(sampleList_[1])[idx] ); //recieve reference
00178     divideHistograms(hsam, href, errorType_);
00179     
00180     setCanvLog( *canv, idx );
00181     setCanvGrid( *canv, idx );
00182     setHistStyles( hsam, idx, 0 ); //there is only one sample
00183     
00184     hsam.Draw();
00185     canv->RedrawAxis( );
00186     canv->Update( );
00187     psFile.Close();
00188   }
00189   canv->Close();
00190   delete canv;
00191 }
00192 
00193 TH1F& 
00194 ManipHist::divideHistograms(TH1F& nom, TH1F& denom, int err)
00195 {
00196   //------------------------------------------------
00197   // divide histograms bin by bin & add appropriate 
00198   // error according to specification in errorType_
00199   //------------------------------------------------
00200   for(int idx=0; idx<denom.GetNbinsX(); ++idx){
00201     double dx=nom.GetBinError(idx+1), x=nom.GetBinContent(idx+1);
00202     double dn=denom.GetBinError(idx+1), n=denom.GetBinContent(idx+1);
00203 
00204     if(n==0){
00205       nom.SetBinContent( idx+1, 0. );
00206       nom.SetBinError  ( idx+1, 0. );      
00207     }
00208     else{
00209       nom.SetBinContent( idx+1, x/n );
00210       if(err==1){
00211         nom.SetBinError( idx+1, ratioCorrelatedError  (x, dx, n, dn) );
00212       }
00213       else{
00214         nom.SetBinError( idx+1, ratioUncorrelatedError(x, dx, n, dn) );
00215       }
00216     }
00217   }
00218   return nom;
00219 }
00220 
00221 double 
00222 ManipHist::ratioCorrelatedError(double& x, double& dx, double& n, double& dn)
00223 {
00224   //------------------------------------------------
00225   // Get error deps of correlated ratio eps=x/n:
00226   //
00227   // For gaussian distributed quantities the formular: 
00228   // * deps=eps*Sqrt((dx/x)^2+(1-2*eps)*(dn/n)^2))
00229   // automatically turns into 
00230   // * deps=Sqrt(eps*(1-eps)/n)
00231   //------------------------------------------------
00232   if(x<=0) return  0;
00233   if(n==0) return -1;
00234   return (x/n<1) ? (x/n)*sqrt(std::fabs((dx*dx)/(n*n)+(1.-2.*(x/n))*(dn*dn)/(n*n))): 
00235                    (n/x)*sqrt(std::fabs((dn*dn)/(x*x)+(1.-2.*(n/x))*(dx*dx)/(x*x)));
00236 }
00237 
00238 double 
00239 ManipHist::ratioUncorrelatedError(double& x, double& dx, double& n, double& dn)
00240 {
00241   //------------------------------------------------
00242   // Get error deps of uncorrelated ratio eps=x/n:
00243   //------------------------------------------------
00244   if(x==0) return 0;
00245   if(n==0) return 0;
00246   return (x/n)*sqrt((dx*dx)/(x*x)+(dn*dn)/(n*n));
00247 }