CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
12 
14 
16  if (!myself) myself = new Histos();
17  return myself;
18 }
19 
21 
22 void
23 Histos::book(const std::string& name,
24  int nx, float xmin, float xmax,
25  int ny, float ymin, float ymax) {
26 
27  if ( theHistos.find(name) != theHistos.end() ) {
28 
29  std::cout << "Histos::book() : Histogram "
30  << name << " exists already. Nothing done" << std::endl;
31 
32  } else {
33 
34  if ( ny ) {
35 
36  theHistos[name] = new TH2F(name.c_str(),"",nx,xmin,xmax,ny,ymin,ymax);
37  theTypes[name] = 2;
38 
39  } else {
40 
41  theHistos[name] = new TH1F(name.c_str(),"",nx,xmin,xmax);
42  theTypes[name] = 1;
43 
44  }
45 
46  }
47 
48 }
49 
50 void
51 Histos::book(const std::string& name,
52  int nx, float xmin, float xmax,
53  const std::string& option) {
54 
55  if ( theHistos.find(name) != theHistos.end() ) {
56 
57  std::cout << "Histos::book() : Histogram "
58  << name << " exists already. Nothing done" << std::endl;
59 
60  } else {
61 
62  theHistos[name] = new TProfile(name.c_str(),"",nx,xmin,xmax,option.c_str());
63  theTypes[name] = 3;
64  }
65 
66 }
67 
68 void
69 Histos::put(const std::string& file, std::string name) {
70 
71  TFile * f = new TFile(file.c_str(),"recreate");
72  f->cd();
73 
74  HistoItr ho ;
75  for(ho=theObjects.begin();ho!=theObjects.end();++ho)
76  {
77  (*ho).second->Write((*ho).first.c_str());
78  }
79 
80 
81  HistoItr hh = theHistos.find(name);
82  if ( name == "" )
83  for ( hh = theHistos.begin();
84  hh != theHistos.end();
85  ++hh ) {
86  if ( theTypes[(*hh).first] == 1 ) ( (TH1F*)((*hh).second) )->Write();
87  if ( theTypes[(*hh).first] == 2 ) ( (TH2F*)((*hh).second) )->Write();
88  if ( theTypes[(*hh).first] == 3 ) ( (TProfile*)((*hh).second) )->Write();
89  }
90 
91  else
92  if ( hh != theHistos.end() ) {
93  if ( theTypes[name] == 1 ) ( (TH1F*)((*hh).second) )->Write();
94  if ( theTypes[name] == 2 ) ( (TH2F*)((*hh).second) )->Write();
95  if ( theTypes[name] == 3 ) ( (TProfile*)((*hh).second) )->Write();
96  }
97 
98  else
99  std::cout << "Histos::put() : Histogram "
100  << name << " does not exist. Nothing done" << std::endl;
101 
102  f->Write();
103  f->Close();
104 
105 }
106 
107 void
108 Histos::divide(const std::string& h1, const std::string& h2, const std::string& h3) {
109 
110  HistoItr hh1 = theHistos.find(h1);
111  HistoItr hh2 = theHistos.find(h2);
112  HistoItr hh3 = theHistos.find(h3);
113 
114  if ( hh1 == theHistos.end() ||
115  hh2 == theHistos.end() ||
116  hh3 != theHistos.end() ) {
117 
118  if ( hh1 == theHistos.end() )
119  std::cout << "Histos::divide() : First histo "
120  << h1 << " does not exist" << std::endl;
121 
122  if ( hh2 == theHistos.end() )
123  std::cout << "Histos::divide() : Second histo "
124  << h2 << " does not exist" << std::endl;
125 
126  if ( hh3 != theHistos.end() )
127  std::cout << "Histos::divide() : Third histo "
128  << h3 << " already exists" << std::endl;
129 
130  } else {
131 
132  if ( theTypes[h1] == 1 && theTypes[h2] == 1 ) {
133 
134  theHistos[h3] = (TH1F*) ((*hh1).second)->Clone(h3.c_str());
135  theTypes[h3] = 1;
136  ((TH1F*)theHistos[h3])->Divide( (TH1F*)( (*hh2).second ) );
137 
138  }
139 
140  if ( theTypes[h1] == 2 && theTypes[h2] == 2 ) {
141 
142  theHistos[h3] = (TH2F*)((*hh1).second)->Clone(h3.c_str());
143  theTypes[h3] = 2;
144  ((TH2F*)theHistos[h3])->Divide( (TH2F*)( (*hh2).second ) );
145 
146  }
147 
148  }
149 
150 }
151 
152 void Histos::addObject(const std::string& name, TObject * obj)
153 {
154  HistoItr hh = theObjects.find(name);
155  if (hh != theObjects.end())
156  {
157  std::cout << "FamosHistos::addObject() : Object " << name
158  << " already exists" << std::endl;
159  return;
160  }
161  // Potential source of memory leaks if not carefully used
162  theObjects.insert(std::pair<std::string,TObject*>(name,obj->Clone()));
163 }
164 
165 
166 
167 void
168 Histos::fill(const std::string& name, float val1, float val2,float val3) {
169 
170  // std::cout << " Fill " << name << " " << val1 << " " << val2 << " " << val3 << std::endl;
171  // std::cout << &theHistos << std::endl;
172  HistoItr hh = theHistos.find(name);
173  // std::cout << " Fill done " << std::endl;
174  if ( hh == theHistos.end() ) {
175 
176  std::cout << "Histos::fill() : Histogram " << name
177  << " does not exist" << std::endl;
178 
179  } else {
180 
181  if ( theTypes[name] == 1 )
182  ( (TH1F*) ( (*hh).second ) )->Fill(val1,val2);
183 
184  if ( theTypes[name] == 2 )
185  ( (TH2F*) ( (*hh).second ) )->Fill(val1,val2,val3);
186 
187  if ( theTypes[name] == 3 )
188  ( (TProfile*) ( (*hh).second ) )->Fill(val1,val2,val3);
189  }
190 
191 }
192 
193 void Histos::fillByNumber(const std::string& name,int number,float val1,float val2,float val3)
194 {
195  std::ostringstream oss;
196  oss << name << number;
197  fill(oss.str(),val1,val2,val3);
198 }
199 
200 void Histos::bookByNumber(const std::string& name, int n1,int n2,
201  int nx , float xmin , float xmax,
202  int ny , float ymin, float ymax)
203 {
204  if(n1>n2)
205  {
206  std::cout <<" Histos: problem with bookByNumber - Do nothing" << std::endl;
207  }
208  for(int ih=n1;ih<=n2;++ih)
209  {
210  std::ostringstream oss;
211  oss << name << ih;
212  book(oss.str(),nx,xmin,xmax,ny,ymin,ymax);
213  }
214 
215 }
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:200
std::map< std::string, TObject * >::const_iterator HistoItr
Definition: Histos.h:23
std::map< std::string, unsigned > theTypes
Definition: Histos.h:78
void fill(const std::string &name, float val1, float val2=1., float val3=1.)
Fill an histogram.
Definition: Histos.cc:168
void addObject(const std::string &name, TObject *obj)
Add any object.
Definition: Histos.cc:152
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:193
void put(const std::string &file, std::string name="")
Write one or all histogram(s) in a file.
Definition: Histos.cc:69
static Histos * myself
Definition: Histos.h:73
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:108
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, TObject * > theHistos
Definition: Histos.h:77
std::map< std::string, TObject * > theObjects
Definition: Histos.h:79
virtual ~Histos()
Destructor.
Definition: Histos.cc:20