CMS 3D CMS Logo

Histos.cc
Go to the documentation of this file.
1 //FAMOS headers
3 
4 #include "TFile.h"
5 //#include "TH1.h"
6 #include "TH2.h"
7 #include "TProfile.h"
8 
9 #include <sstream>
10 
11 Histos* Histos::myself = nullptr;
12 
14 
16  if (!myself)
17  myself = new Histos();
18  return myself;
19 }
20 
22 
23 void Histos::book(const std::string& name, int nx, float xmin, float xmax, int ny, float ymin, float ymax) {
24  if (theHistos.find(name) != theHistos.end()) {
25  std::cout << "Histos::book() : Histogram " << name << " exists already. Nothing done" << std::endl;
26 
27  } else {
28  if (ny) {
29  theHistos[name] = new TH2F(name.c_str(), "", nx, xmin, xmax, ny, ymin, ymax);
30  theTypes[name] = 2;
31 
32  } else {
33  theHistos[name] = new TH1F(name.c_str(), "", nx, xmin, xmax);
34  theTypes[name] = 1;
35  }
36  }
37 }
38 
39 void Histos::book(const std::string& name, int nx, float xmin, float xmax, const std::string& option) {
40  if (theHistos.find(name) != theHistos.end()) {
41  std::cout << "Histos::book() : Histogram " << name << " exists already. Nothing done" << std::endl;
42 
43  } else {
44  theHistos[name] = new TProfile(name.c_str(), "", nx, xmin, xmax, option.c_str());
45  theTypes[name] = 3;
46  }
47 }
48 
50  TFile* f = new TFile(file.c_str(), "recreate");
51  f->cd();
52 
53  HistoItr ho;
54  for (ho = theObjects.begin(); ho != theObjects.end(); ++ho) {
55  (*ho).second->Write((*ho).first.c_str());
56  }
57 
58  HistoItr hh = theHistos.find(name);
59  if (name.empty())
60  for (hh = theHistos.begin(); hh != theHistos.end(); ++hh) {
61  if (theTypes[(*hh).first] == 1)
62  ((TH1F*)((*hh).second))->Write();
63  if (theTypes[(*hh).first] == 2)
64  ((TH2F*)((*hh).second))->Write();
65  if (theTypes[(*hh).first] == 3)
66  ((TProfile*)((*hh).second))->Write();
67  }
68 
69  else if (hh != theHistos.end()) {
70  if (theTypes[name] == 1)
71  ((TH1F*)((*hh).second))->Write();
72  if (theTypes[name] == 2)
73  ((TH2F*)((*hh).second))->Write();
74  if (theTypes[name] == 3)
75  ((TProfile*)((*hh).second))->Write();
76  }
77 
78  else
79  std::cout << "Histos::put() : Histogram " << name << " does not exist. Nothing done" << std::endl;
80 
81  f->Write();
82  f->Close();
83 }
84 
85 void Histos::divide(const std::string& h1, const std::string& h2, const std::string& h3) {
86  HistoItr hh1 = theHistos.find(h1);
87  HistoItr hh2 = theHistos.find(h2);
88  HistoItr hh3 = theHistos.find(h3);
89 
90  if (hh1 == theHistos.end() || hh2 == theHistos.end() || hh3 != theHistos.end()) {
91  if (hh1 == theHistos.end())
92  std::cout << "Histos::divide() : First histo " << h1 << " does not exist" << std::endl;
93 
94  if (hh2 == theHistos.end())
95  std::cout << "Histos::divide() : Second histo " << h2 << " does not exist" << std::endl;
96 
97  if (hh3 != theHistos.end())
98  std::cout << "Histos::divide() : Third histo " << h3 << " already exists" << std::endl;
99 
100  } else {
101  if (theTypes[h1] == 1 && theTypes[h2] == 1) {
102  theHistos[h3] = (TH1F*)((*hh1).second)->Clone(h3.c_str());
103  theTypes[h3] = 1;
104  ((TH1F*)theHistos[h3])->Divide((TH1F*)((*hh2).second));
105  }
106 
107  if (theTypes[h1] == 2 && theTypes[h2] == 2) {
108  theHistos[h3] = (TH2F*)((*hh1).second)->Clone(h3.c_str());
109  theTypes[h3] = 2;
110  ((TH2F*)theHistos[h3])->Divide((TH2F*)((*hh2).second));
111  }
112  }
113 }
114 
115 void Histos::addObject(const std::string& name, TObject* obj) {
116  HistoItr hh = theObjects.find(name);
117  if (hh != theObjects.end()) {
118  std::cout << "FamosHistos::addObject() : Object " << name << " already exists" << std::endl;
119  return;
120  }
121  // Potential source of memory leaks if not carefully used
122  theObjects.insert(std::pair<std::string, TObject*>(name, obj->Clone()));
123 }
124 
125 void Histos::fill(const std::string& name, float val1, float val2, float val3) {
126  // std::cout << " Fill " << name << " " << val1 << " " << val2 << " " << val3 << std::endl;
127  // std::cout << &theHistos << std::endl;
128  HistoItr hh = theHistos.find(name);
129  // std::cout << " Fill done " << std::endl;
130  if (hh == theHistos.end()) {
131  std::cout << "Histos::fill() : Histogram " << name << " does not exist" << std::endl;
132 
133  } else {
134  if (theTypes[name] == 1)
135  ((TH1F*)((*hh).second))->Fill(val1, val2);
136 
137  if (theTypes[name] == 2)
138  ((TH2F*)((*hh).second))->Fill(val1, val2, val3);
139 
140  if (theTypes[name] == 3)
141  ((TProfile*)((*hh).second))->Fill(val1, val2, val3);
142  }
143 }
144 
145 void Histos::fillByNumber(const std::string& name, int number, float val1, float val2, float val3) {
146  std::ostringstream oss;
147  oss << name << number;
148  fill(oss.str(), val1, val2, val3);
149 }
150 
152  const std::string& name, int n1, int n2, int nx, float xmin, float xmax, int ny, float ymin, float ymax) {
153  if (n1 > n2) {
154  std::cout << " Histos: problem with bookByNumber - Do nothing" << std::endl;
155  }
156  for (int ih = n1; ih <= n2; ++ih) {
157  std::ostringstream oss;
158  oss << name << ih;
159  book(oss.str(), nx, xmin, xmax, ny, ymin, ymax);
160  }
161 }
std::map< std::string, TObject * > theHistos
Definition: Histos.h:75
std::map< std::string, unsigned > theTypes
Definition: Histos.h:76
void bookByNumber(const std::string &name, int n1, int n2, int nx, float xmin, float xmax, int ny=0, float ymin=0., float ymax=0.)
Definition: Histos.cc:151
std::map< std::string, TObject * >::const_iterator HistoItr
Definition: Histos.h:21
void fill(const std::string &name, float val1, float val2=1., float val3=1.)
Fill an histogram.
Definition: Histos.cc:125
void addObject(const std::string &name, TObject *obj)
Add any object.
Definition: Histos.cc:115
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
void book(const std::string &name, int nx, float xmin, float xmax, int ny=0, float ymin=0., float ymax=0.)
Book an histogram (1D or 2D)
Definition: Histos.cc:23
static Histos * instance()
Definition: Histos.cc:15
double f[11][100]
void fillByNumber(const std::string &name, int number, float val1, float val2=1., float val3=1.)
Definition: Histos.cc:145
std::map< std::string, TObject * > theObjects
Definition: Histos.h:77
void put(const std::string &file, std::string name="")
Write one or all histogram(s) in a file.
Definition: Histos.cc:49
static Histos * myself
Definition: Histos.h:71
Histos()
Definition: Histos.cc:13
Definition: Histos.h:19
void divide(const std::string &h1, const std::string &h2, const std::string &h3)
Divide two histograms and put the result in the first.
Definition: Histos.cc:85
virtual ~Histos()
Destructor.
Definition: Histos.cc:21