CMS 3D CMS Logo

Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes

Histos Class Reference

#include <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, int ny=0, float ymin=0., float ymax=0.)
 Book an histogram (1D or 2D)
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.
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.

{}
Histos::Histos ( ) [private]

Definition at line 13 of file Histos.cc.

Referenced by instance().

{}

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.

{
  HistoItr hh = theObjects.find(name);
  if (hh != theObjects.end())
    {
      std::cout << "FamosHistos::addObject() : Object " << name 
                << " already exists" << std::endl;
      return;
    }
  // Potential source of memory leaks if not carefully used 
  theObjects.insert(std::pair<std::string,TObject*>(name,obj->Clone()));
}
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, theTypes, SiStripMonitorClusterAlca_cfi::xmax, SiStripMonitorClusterAlca_cfi::xmin, SiStripMonitorClusterAlca_cfi::ymax, and SiStripMonitorClusterAlca_cfi::ymin.

Referenced by bookByNumber().

                                             {
  
  if ( theHistos.find(name) != theHistos.end() ) { 

    std::cout << "Histos::book() : Histogram " 
         << name << " exists already. Nothing done" << std::endl;

  } else {

    if ( ny ) {

      theHistos[name] = new TH2F(name.c_str(),"",nx,xmin,xmax,ny,ymin,ymax);
      theTypes[name] = 2;

    } else {

      theHistos[name] = new TH1F(name.c_str(),"",nx,xmin,xmax);
      theTypes[name] = 1;

    }

  }

}
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, theTypes, SiStripMonitorClusterAlca_cfi::xmax, and SiStripMonitorClusterAlca_cfi::xmin.

                                      {
  
  if ( theHistos.find(name) != theHistos.end() ) { 

    std::cout << "Histos::book() : Histogram " 
         << name << " exists already. Nothing done" << std::endl;

  } else {

    theHistos[name] = new TProfile(name.c_str(),"",nx,xmin,xmax,option.c_str());
    theTypes[name] = 3;  
  }

}
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(), gather_cfg::cout, SiStripMonitorClusterAlca_cfi::xmax, SiStripMonitorClusterAlca_cfi::xmin, SiStripMonitorClusterAlca_cfi::ymax, and SiStripMonitorClusterAlca_cfi::ymin.

{
  if(n1>n2)
    {
      std::cout <<" Histos: problem with bookByNumber - Do nothing" << std::endl;
    }
  for(int ih=n1;ih<=n2;++ih)
    {
       std::ostringstream oss;
       oss << name << ih;
       book(oss.str(),nx,xmin,xmax,ny,ymin,ymax);
    }

}
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.

{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 gather_cfg::cout, theHistos, and theTypes.

                                                                            {

  HistoItr hh1 = theHistos.find(h1);
  HistoItr hh2 = theHistos.find(h2);
  HistoItr hh3 = theHistos.find(h3);

  if ( hh1 == theHistos.end() ||
       hh2 == theHistos.end() ||
       hh3 != theHistos.end() ) {

    if ( hh1 == theHistos.end() ) 
      std::cout << "Histos::divide() : First histo " 
           << h1 << " does not exist" << std::endl;

    if ( hh2 == theHistos.end() ) 
      std::cout << "Histos::divide() : Second histo " 
           << h2 << " does not exist" << std::endl;

    if ( hh3 != theHistos.end() ) 
      std::cout << "Histos::divide() : Third histo " 
           << h3 << " already exists" << std::endl;

  } else {

    if ( theTypes[h1] == 1 && theTypes[h2] == 1 ) { 
      
      theHistos[h3] = (TH1F*) ((*hh1).second)->Clone(h3.c_str());
      theTypes[h3] = 1;
      ((TH1F*)theHistos[h3])->Divide( (TH1F*)( (*hh2).second ) );

    }
      
    if ( theTypes[h1] == 2 && theTypes[h2] == 2 ) { 

      theHistos[h3] = (TH2F*)((*hh1).second)->Clone(h3.c_str());
      theTypes[h3] = 2;
      ((TH2F*)theHistos[h3])->Divide( (TH2F*)( (*hh2).second ) );
      
    }

  }

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

                                                                     {

  //  std::cout << " Fill " << name << " " << val1 << " " << val2 << " " << val3 << std::endl;
  //  std::cout << &theHistos << std::endl;
  HistoItr hh = theHistos.find(name);
  //  std::cout << " Fill done " << std::endl;
  if ( hh == theHistos.end() ) {

    std::cout << "Histos::fill() : Histogram " << name 
         << " does not exist" << std::endl;

  } else {

    if ( theTypes[name] == 1 ) 
      ( (TH1F*) ( (*hh).second ) )->Fill(val1,val2);

    if ( theTypes[name] == 2 ) 
      ( (TH2F*) ( (*hh).second ) )->Fill(val1,val2,val3);
    
    if ( theTypes[name] == 3 ) 
      ( (TProfile*) ( (*hh).second ) )->Fill(val1,val2,val3);
  }

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

{
  std::ostringstream oss;
  oss << name << number;
  fill(oss.str(),val1,val2,val3);
}
Histos * Histos::instance ( ) [static]

Definition at line 15 of file Histos.cc.

References Histos(), and myself.

                         {
  if (!myself) myself = new Histos();
  return myself;
}
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.

                                                 {

  TFile * f = new TFile(file.c_str(),"recreate");
  f->cd();
  
  HistoItr ho ;
  for(ho=theObjects.begin();ho!=theObjects.end();++ho)
    {
      (*ho).second->Write((*ho).first.c_str());
    }


  HistoItr hh = theHistos.find(name);
  if ( name == "" ) 
    for ( hh  = theHistos.begin(); 
          hh != theHistos.end(); 
          ++hh ) { 
      if ( theTypes[(*hh).first] == 1 )  ( (TH1F*)((*hh).second) )->Write();
      if ( theTypes[(*hh).first] == 2 )  ( (TH2F*)((*hh).second) )->Write();
      if ( theTypes[(*hh).first] == 3 )  ( (TProfile*)((*hh).second) )->Write();
    }

  else 
    if ( hh != theHistos.end() ) { 
      if ( theTypes[name] == 1 )  ( (TH1F*)((*hh).second) )->Write();
      if ( theTypes[name] == 2 )  ( (TH2F*)((*hh).second) )->Write();
      if ( theTypes[name] == 3 )  ( (TProfile*)((*hh).second) )->Write();
    }

  else 
    std::cout << "Histos::put() : Histogram " 
         << name << " does not exist. Nothing done" << std::endl;

  f->Write();
  f->Close();

}  

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