CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
Histos Class Reference

#include <Histos.h>

Public Types

typedef std::map< std::string,
TObject * >::const_iterator 
HistoItr
 

Public Member Functions

void addObject (const std::string &name, TObject *obj)
 Add any object. More...
 
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) More...
 
void book (const std::string &name, int nx, float xmin, float xmax, const std::string &option)
 
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.)
 
void debug (std::string p="") const
 
void divide (const std::string &h1, const std::string &h2, const std::string &h3)
 Divide two histograms and put the result in the first. More...
 
void fill (const std::string &name, float val1, float val2=1., float val3=1.)
 Fill an histogram. More...
 
void fillByNumber (const std::string &name, int number, float val1, float val2=1., float val3=1.)
 
void put (const std::string &file, std::string name="")
 Write one or all histogram(s) in a file. More...
 
virtual ~Histos ()
 Destructor. More...
 

Static Public Member Functions

static Histosinstance ()
 

Private Member Functions

 Histos ()
 

Private Attributes

TObject * theHisto
 
std::map< std::string, TObject * > theHistos
 
std::map< std::string, TObject * > theObjects
 
std::map< std::string, unsigned > theTypes
 

Static Private Attributes

static Histosmyself = 0
 

Detailed Description

This class provides an interface to root histograms

Author
Patrick Janot $Date: 16 Jan 2004 19:30

Definition at line 19 of file Histos.h.

Member Typedef Documentation

typedef std::map<std::string,TObject*>::const_iterator Histos::HistoItr

Definition at line 23 of file Histos.h.

Constructor & Destructor Documentation

Histos::~Histos ( )
virtual

Destructor.

Definition at line 20 of file Histos.cc.

20 {}
Histos::Histos ( )
private

Definition at line 13 of file Histos.cc.

Referenced by instance().

13 {}

Member Function Documentation

void Histos::addObject ( const std::string &  name,
TObject *  obj 
)

Add any object.

Definition at line 152 of file Histos.cc.

References gather_cfg::cout, and theObjects.

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 }
std::map< std::string, TObject * >::const_iterator HistoItr
Definition: Histos.h:23
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, TObject * > theObjects
Definition: Histos.h:79
void Histos::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 at line 23 of file Histos.cc.

References gather_cfg::cout, mergeVDriftHistosByStation::name, theHistos, and theTypes.

Referenced by bookByNumber().

25  {
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 }
std::map< std::string, unsigned > theTypes
Definition: Histos.h:78
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, TObject * > theHistos
Definition: Histos.h:77
void Histos::book ( const std::string &  name,
int  nx,
float  xmin,
float  xmax,
const std::string &  option 
)

Book a TProfile option="S" -> spread "" -> error on mean (from Root documentation)

Definition at line 51 of file Histos.cc.

References gather_cfg::cout, mergeVDriftHistosByStation::name, theHistos, and theTypes.

53  {
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 }
std::map< std::string, unsigned > theTypes
Definition: Histos.h:78
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, TObject * > theHistos
Definition: Histos.h:77
void Histos::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 at line 200 of file Histos.cc.

References book(), and gather_cfg::cout.

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 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
tuple cout
Definition: gather_cfg.py:121
void Histos::debug ( std::string  p = "") const
inline

Definition at line 64 of file Histos.h.

References gather_cfg::cout, AlCaHLTBitMon_ParallelJobs::p, and theHistos.

64 {std::cout << " Histos myMap : "<< &theHistos << " " << p <<std::endl;}
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, TObject * > theHistos
Definition: Histos.h:77
void Histos::divide ( const std::string &  h1,
const std::string &  h2,
const std::string &  h3 
)

Divide two histograms and put the result in the first.

Definition at line 108 of file Histos.cc.

References gather_cfg::cout, theHistos, and theTypes.

Referenced by python.rootplot.utilities.Hist::__div__().

108  {
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 }
std::map< std::string, TObject * >::const_iterator HistoItr
Definition: Histos.h:23
std::map< std::string, unsigned > theTypes
Definition: Histos.h:78
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, TObject * > theHistos
Definition: Histos.h:77
void Histos::fill ( const std::string &  name,
float  val1,
float  val2 = 1.,
float  val3 = 1. 
)

Fill an histogram.

Definition at line 168 of file Histos.cc.

References gather_cfg::cout, HcalObjRepresent::Fill(), theHistos, and theTypes.

Referenced by fillByNumber(), and CaloGeometryHelper::getClosestCell().

168  {
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 }
std::map< std::string, TObject * >::const_iterator HistoItr
Definition: Histos.h:23
std::map< std::string, unsigned > theTypes
Definition: Histos.h:78
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, TObject * > theHistos
Definition: Histos.h:77
void Histos::fillByNumber ( const std::string &  name,
int  number,
float  val1,
float  val2 = 1.,
float  val3 = 1. 
)

Definition at line 193 of file Histos.cc.

References fill().

194 {
195  std::ostringstream oss;
196  oss << name << number;
197  fill(oss.str(),val1,val2,val3);
198 }
void fill(const std::string &name, float val1, float val2=1., float val3=1.)
Fill an histogram.
Definition: Histos.cc:168
Histos * Histos::instance ( )
static

Definition at line 15 of file Histos.cc.

References Histos(), and myself.

Referenced by EcalHitMaker::EcalHitMaker(), and CaloGeometryHelper::getClosestCell().

15  {
16  if (!myself) myself = new Histos();
17  return myself;
18 }
static Histos * myself
Definition: Histos.h:73
Histos()
Definition: Histos.cc:13
void Histos::put ( const std::string &  file,
std::string  name = "" 
)

Write one or all histogram(s) in a file.

Definition at line 69 of file Histos.cc.

References gather_cfg::cout, f, theHistos, theObjects, and theTypes.

69  {
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 }
std::map< std::string, TObject * >::const_iterator HistoItr
Definition: Histos.h:23
std::map< std::string, unsigned > theTypes
Definition: Histos.h:78
double f[11][100]
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

Member Data Documentation

Histos * Histos::myself = 0
staticprivate

Definition at line 73 of file Histos.h.

Referenced by instance().

TObject* Histos::theHisto
private

Definition at line 76 of file Histos.h.

std::map<std::string,TObject*> Histos::theHistos
private

Definition at line 77 of file Histos.h.

Referenced by book(), debug(), divide(), fill(), and put().

std::map<std::string,TObject*> Histos::theObjects
private

Definition at line 79 of file Histos.h.

Referenced by addObject(), and put().

std::map<std::string,unsigned> Histos::theTypes
private

Definition at line 78 of file Histos.h.

Referenced by book(), divide(), fill(), and put().