CMS 3D CMS Logo

Histos Class Reference

This class provides an interface to root histograms. More...

#include <FastSimulation/Utilities/interface/Histos.h>

List of all members.

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.
void 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).
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).
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.
void fill (const std::string &name, float val1, float val2=1., float val3=1.)
 Fill an histogram.
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.
virtual ~Histos ()
 Destructor.

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.

00020 {}

Histos::Histos (  )  [private]

Definition at line 13 of file Histos.cc.

Referenced by instance().

00013 {}


Member Function Documentation

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

Add any object.

Definition at line 152 of file Histos.cc.

References GenMuonPlsPt100GeV_cfg::cout, lat::endl(), hh, and theObjects.

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 }

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 GenMuonPlsPt100GeV_cfg::cout, lat::endl(), theHistos, and theTypes.

00053                                       {
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 }

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 GenMuonPlsPt100GeV_cfg::cout, lat::endl(), theHistos, and theTypes.

Referenced by bookByNumber(), and CalorimetryManager::CalorimetryManager().

00025                                              {
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 }

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(), GenMuonPlsPt100GeV_cfg::cout, and lat::endl().

Referenced by CalorimetryManager::CalorimetryManager().

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 }

void Histos::debug ( std::string  p = ""  )  const [inline]

Definition at line 64 of file Histos.h.

References GenMuonPlsPt100GeV_cfg::cout, lat::endl(), p, and theHistos.

00064 {std::cout << " Histos myMap : "<< &theHistos << " " << p <<std::endl;}

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 GenMuonPlsPt100GeV_cfg::cout, lat::endl(), theHistos, and theTypes.

00108                                                                             {
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 }

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 GenMuonPlsPt100GeV_cfg::cout, lat::endl(), hh, theHistos, and theTypes.

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

00168                                                                      {
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 }

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().

00194 {
00195   std::ostringstream oss;
00196   oss << name << number;
00197   fill(oss.str(),val1,val2,val3);
00198 }

Histos * Histos::instance ( void   )  [static]

Definition at line 15 of file Histos.cc.

References Histos(), and myself.

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

00015                          {
00016   if (!myself) myself = new Histos();
00017   return myself;
00018 }

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 GenMuonPlsPt100GeV_cfg::cout, lat::endl(), f, hh, theHistos, theObjects, and theTypes.

Referenced by CalorimetryManager::~CalorimetryManager().

00069                                                  {
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 }  


Member Data Documentation

Histos * Histos::myself = 0 [static, private]

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().


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:24:18 2009 for CMSSW by  doxygen 1.5.4