Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:26:55

0001 //FAMOS headers
0002 #include "FastSimulation/Utilities/interface/Histos.h"
0003 
0004 #include "TFile.h"
0005 //#include "TH1.h"
0006 #include "TH2.h"
0007 #include "TProfile.h"
0008 
0009 #include <sstream>
0010 
0011 Histos* Histos::myself = nullptr;
0012 
0013 Histos::Histos() {}
0014 
0015 Histos* Histos::instance() {
0016   if (!myself)
0017     myself = new Histos();
0018   return myself;
0019 }
0020 
0021 Histos::~Histos() {}
0022 
0023 void Histos::book(const std::string& name, int nx, float xmin, float xmax, int ny, float ymin, float ymax) {
0024   if (theHistos.find(name) != theHistos.end()) {
0025     std::cout << "Histos::book() : Histogram " << name << " exists already. Nothing done" << std::endl;
0026 
0027   } else {
0028     if (ny) {
0029       theHistos[name] = new TH2F(name.c_str(), "", nx, xmin, xmax, ny, ymin, ymax);
0030       theTypes[name] = 2;
0031 
0032     } else {
0033       theHistos[name] = new TH1F(name.c_str(), "", nx, xmin, xmax);
0034       theTypes[name] = 1;
0035     }
0036   }
0037 }
0038 
0039 void Histos::book(const std::string& name, int nx, float xmin, float xmax, const std::string& option) {
0040   if (theHistos.find(name) != theHistos.end()) {
0041     std::cout << "Histos::book() : Histogram " << name << " exists already. Nothing done" << std::endl;
0042 
0043   } else {
0044     theHistos[name] = new TProfile(name.c_str(), "", nx, xmin, xmax, option.c_str());
0045     theTypes[name] = 3;
0046   }
0047 }
0048 
0049 void Histos::put(const std::string& file, std::string name) {
0050   TFile* f = new TFile(file.c_str(), "recreate");
0051   f->cd();
0052 
0053   HistoItr ho;
0054   for (ho = theObjects.begin(); ho != theObjects.end(); ++ho) {
0055     (*ho).second->Write((*ho).first.c_str());
0056   }
0057 
0058   HistoItr hh = theHistos.find(name);
0059   if (name.empty())
0060     for (hh = theHistos.begin(); hh != theHistos.end(); ++hh) {
0061       if (theTypes[(*hh).first] == 1)
0062         ((TH1F*)((*hh).second))->Write();
0063       if (theTypes[(*hh).first] == 2)
0064         ((TH2F*)((*hh).second))->Write();
0065       if (theTypes[(*hh).first] == 3)
0066         ((TProfile*)((*hh).second))->Write();
0067     }
0068 
0069   else if (hh != theHistos.end()) {
0070     if (theTypes[name] == 1)
0071       ((TH1F*)((*hh).second))->Write();
0072     if (theTypes[name] == 2)
0073       ((TH2F*)((*hh).second))->Write();
0074     if (theTypes[name] == 3)
0075       ((TProfile*)((*hh).second))->Write();
0076   }
0077 
0078   else
0079     std::cout << "Histos::put() : Histogram " << name << " does not exist. Nothing done" << std::endl;
0080 
0081   f->Write();
0082   f->Close();
0083 }
0084 
0085 void Histos::divide(const std::string& h1, const std::string& h2, const std::string& h3) {
0086   HistoItr hh1 = theHistos.find(h1);
0087   HistoItr hh2 = theHistos.find(h2);
0088   HistoItr hh3 = theHistos.find(h3);
0089 
0090   if (hh1 == theHistos.end() || hh2 == theHistos.end() || hh3 != theHistos.end()) {
0091     if (hh1 == theHistos.end())
0092       std::cout << "Histos::divide() : First histo " << h1 << " does not exist" << std::endl;
0093 
0094     if (hh2 == theHistos.end())
0095       std::cout << "Histos::divide() : Second histo " << h2 << " does not exist" << std::endl;
0096 
0097     if (hh3 != theHistos.end())
0098       std::cout << "Histos::divide() : Third histo " << h3 << " already exists" << std::endl;
0099 
0100   } else {
0101     if (theTypes[h1] == 1 && theTypes[h2] == 1) {
0102       theHistos[h3] = (TH1F*)((*hh1).second)->Clone(h3.c_str());
0103       theTypes[h3] = 1;
0104       ((TH1F*)theHistos[h3])->Divide((TH1F*)((*hh2).second));
0105     }
0106 
0107     if (theTypes[h1] == 2 && theTypes[h2] == 2) {
0108       theHistos[h3] = (TH2F*)((*hh1).second)->Clone(h3.c_str());
0109       theTypes[h3] = 2;
0110       ((TH2F*)theHistos[h3])->Divide((TH2F*)((*hh2).second));
0111     }
0112   }
0113 }
0114 
0115 void Histos::addObject(const std::string& name, TObject* obj) {
0116   HistoItr hh = theObjects.find(name);
0117   if (hh != theObjects.end()) {
0118     std::cout << "FamosHistos::addObject() : Object " << name << " already exists" << std::endl;
0119     return;
0120   }
0121   // Potential source of memory leaks if not carefully used
0122   theObjects.insert(std::pair<std::string, TObject*>(name, obj->Clone()));
0123 }
0124 
0125 void Histos::fill(const std::string& name, float val1, float val2, float val3) {
0126   //  std::cout << " Fill " << name << " " << val1 << " " << val2 << " " << val3 << std::endl;
0127   //  std::cout << &theHistos << std::endl;
0128   HistoItr hh = theHistos.find(name);
0129   //  std::cout << " Fill done " << std::endl;
0130   if (hh == theHistos.end()) {
0131     std::cout << "Histos::fill() : Histogram " << name << " does not exist" << std::endl;
0132 
0133   } else {
0134     if (theTypes[name] == 1)
0135       ((TH1F*)((*hh).second))->Fill(val1, val2);
0136 
0137     if (theTypes[name] == 2)
0138       ((TH2F*)((*hh).second))->Fill(val1, val2, val3);
0139 
0140     if (theTypes[name] == 3)
0141       ((TProfile*)((*hh).second))->Fill(val1, val2, val3);
0142   }
0143 }
0144 
0145 void Histos::fillByNumber(const std::string& name, int number, float val1, float val2, float val3) {
0146   std::ostringstream oss;
0147   oss << name << number;
0148   fill(oss.str(), val1, val2, val3);
0149 }
0150 
0151 void Histos::bookByNumber(
0152     const std::string& name, int n1, int n2, int nx, float xmin, float xmax, int ny, float ymin, float ymax) {
0153   if (n1 > n2) {
0154     std::cout << " Histos: problem with bookByNumber - Do nothing" << std::endl;
0155   }
0156   for (int ih = n1; ih <= n2; ++ih) {
0157     std::ostringstream oss;
0158     oss << name << ih;
0159     book(oss.str(), nx, xmin, xmax, ny, ymin, ymax);
0160   }
0161 }