CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/FastSimulation/Utilities/src/Histos.cc

Go to the documentation of this file.
00001 //FAMOS headers
00002 #include "FastSimulation/Utilities/interface/Histos.h"
00003 
00004 #include "TFile.h"
00005 //#include "TH1.h"
00006 #include "TH2.h"
00007 #include "TProfile.h"
00008 
00009 #include <sstream>
00010 
00011 Histos* Histos::myself = 0;
00012 
00013 Histos::Histos() {}
00014 
00015 Histos* Histos::instance() {
00016   if (!myself) myself = new Histos();
00017   return myself;
00018 }
00019 
00020 Histos::~Histos() {}
00021 
00022 void 
00023 Histos::book(const std::string& name, 
00024              int nx, float xmin, float xmax,
00025              int ny, float ymin, float ymax) {
00026   
00027   if ( theHistos.find(name) != theHistos.end() ) { 
00028 
00029     std::cout << "Histos::book() : Histogram " 
00030          << name << " exists already. Nothing done" << std::endl;
00031 
00032   } else {
00033 
00034     if ( ny ) {
00035 
00036       theHistos[name] = new TH2F(name.c_str(),"",nx,xmin,xmax,ny,ymin,ymax);
00037       theTypes[name] = 2;
00038 
00039     } else {
00040 
00041       theHistos[name] = new TH1F(name.c_str(),"",nx,xmin,xmax);
00042       theTypes[name] = 1;
00043 
00044     }
00045 
00046   }
00047 
00048 }
00049 
00050 void 
00051 Histos::book(const std::string& name, 
00052              int nx, float xmin, float xmax,
00053              const std::string& option) {
00054   
00055   if ( theHistos.find(name) != theHistos.end() ) { 
00056 
00057     std::cout << "Histos::book() : Histogram " 
00058          << name << " exists already. Nothing done" << std::endl;
00059 
00060   } else {
00061 
00062     theHistos[name] = new TProfile(name.c_str(),"",nx,xmin,xmax,option.c_str());
00063     theTypes[name] = 3;  
00064   }
00065 
00066 }
00067 
00068 void 
00069 Histos::put(const std::string& file, std::string name) {
00070 
00071   TFile * f = new TFile(file.c_str(),"recreate");
00072   f->cd();
00073   
00074   HistoItr ho ;
00075   for(ho=theObjects.begin();ho!=theObjects.end();++ho)
00076     {
00077       (*ho).second->Write((*ho).first.c_str());
00078     }
00079 
00080 
00081   HistoItr hh = theHistos.find(name);
00082   if ( name == "" ) 
00083     for ( hh  = theHistos.begin(); 
00084           hh != theHistos.end(); 
00085           ++hh ) { 
00086       if ( theTypes[(*hh).first] == 1 )  ( (TH1F*)((*hh).second) )->Write();
00087       if ( theTypes[(*hh).first] == 2 )  ( (TH2F*)((*hh).second) )->Write();
00088       if ( theTypes[(*hh).first] == 3 )  ( (TProfile*)((*hh).second) )->Write();
00089     }
00090 
00091   else 
00092     if ( hh != theHistos.end() ) { 
00093       if ( theTypes[name] == 1 )  ( (TH1F*)((*hh).second) )->Write();
00094       if ( theTypes[name] == 2 )  ( (TH2F*)((*hh).second) )->Write();
00095       if ( theTypes[name] == 3 )  ( (TProfile*)((*hh).second) )->Write();
00096     }
00097 
00098   else 
00099     std::cout << "Histos::put() : Histogram " 
00100          << name << " does not exist. Nothing done" << std::endl;
00101 
00102   f->Write();
00103   f->Close();
00104 
00105 }  
00106 
00107 void
00108 Histos::divide(const std::string& h1, const std::string& h2, const std::string& h3) {
00109 
00110   HistoItr hh1 = theHistos.find(h1);
00111   HistoItr hh2 = theHistos.find(h2);
00112   HistoItr hh3 = theHistos.find(h3);
00113 
00114   if ( hh1 == theHistos.end() ||
00115        hh2 == theHistos.end() ||
00116        hh3 != theHistos.end() ) {
00117 
00118     if ( hh1 == theHistos.end() ) 
00119       std::cout << "Histos::divide() : First histo " 
00120            << h1 << " does not exist" << std::endl;
00121 
00122     if ( hh2 == theHistos.end() ) 
00123       std::cout << "Histos::divide() : Second histo " 
00124            << h2 << " does not exist" << std::endl;
00125 
00126     if ( hh3 != theHistos.end() ) 
00127       std::cout << "Histos::divide() : Third histo " 
00128            << h3 << " already exists" << std::endl;
00129 
00130   } else {
00131 
00132     if ( theTypes[h1] == 1 && theTypes[h2] == 1 ) { 
00133       
00134       theHistos[h3] = (TH1F*) ((*hh1).second)->Clone(h3.c_str());
00135       theTypes[h3] = 1;
00136       ((TH1F*)theHistos[h3])->Divide( (TH1F*)( (*hh2).second ) );
00137 
00138     }
00139       
00140     if ( theTypes[h1] == 2 && theTypes[h2] == 2 ) { 
00141 
00142       theHistos[h3] = (TH2F*)((*hh1).second)->Clone(h3.c_str());
00143       theTypes[h3] = 2;
00144       ((TH2F*)theHistos[h3])->Divide( (TH2F*)( (*hh2).second ) );
00145       
00146     }
00147 
00148   }
00149 
00150 }
00151 
00152 void Histos::addObject(const std::string& name, TObject * obj)
00153 {
00154   HistoItr hh = theObjects.find(name);
00155   if (hh != theObjects.end())
00156     {
00157       std::cout << "FamosHistos::addObject() : Object " << name 
00158                 << " already exists" << std::endl;
00159       return;
00160     }
00161   // Potential source of memory leaks if not carefully used 
00162   theObjects.insert(std::pair<std::string,TObject*>(name,obj->Clone()));
00163 }
00164 
00165 
00166 
00167 void 
00168 Histos::fill(const std::string& name, float val1, float val2,float val3) {
00169 
00170   //  std::cout << " Fill " << name << " " << val1 << " " << val2 << " " << val3 << std::endl;
00171   //  std::cout << &theHistos << std::endl;
00172   HistoItr hh = theHistos.find(name);
00173   //  std::cout << " Fill done " << std::endl;
00174   if ( hh == theHistos.end() ) {
00175 
00176     std::cout << "Histos::fill() : Histogram " << name 
00177          << " does not exist" << std::endl;
00178 
00179   } else {
00180 
00181     if ( theTypes[name] == 1 ) 
00182       ( (TH1F*) ( (*hh).second ) )->Fill(val1,val2);
00183 
00184     if ( theTypes[name] == 2 ) 
00185       ( (TH2F*) ( (*hh).second ) )->Fill(val1,val2,val3);
00186     
00187     if ( theTypes[name] == 3 ) 
00188       ( (TProfile*) ( (*hh).second ) )->Fill(val1,val2,val3);
00189   }
00190 
00191 }
00192 
00193 void Histos::fillByNumber(const std::string& name,int number,float val1,float val2,float val3)
00194 {
00195   std::ostringstream oss;
00196   oss << name << number;
00197   fill(oss.str(),val1,val2,val3);
00198 }
00199 
00200 void Histos::bookByNumber(const std::string& name, int n1,int n2,
00201                           int nx  , float xmin   , float xmax,
00202                           int ny  , float ymin,  float ymax)
00203 {
00204   if(n1>n2)
00205     {
00206       std::cout <<" Histos: problem with bookByNumber - Do nothing" << std::endl;
00207     }
00208   for(int ih=n1;ih<=n2;++ih)
00209     {
00210        std::ostringstream oss;
00211        oss << name << ih;
00212        book(oss.str(),nx,xmin,xmax,ny,ymin,ymax);
00213     }
00214 
00215 }