NueUtilities::Histograms Class Reference

#include <NueUtilities.h>

List of all members.

Public Member Functions

 Histograms ()
 ~Histograms ()
bool DoesSetExist (TString setName) const
unsigned int NumSets () const
TString GetSetName (unsigned int setIndex) const
TH1 * GetHistogram (TString set, TString subset, TString name)

Protected Member Functions

bool OpenFile (TString outputFile, TString option="READ")
void CloseFile ()
void MakeHisto (TString setName, TString subSetName, TString name, int numBinsX, double lowValueX, double highValueX)
void MakeHisto (TString setName, TString subSetName, TString name, int numBinsX, double lowValueX, double highValueX, int numBinsY, double lowValueY, double highValueY)
void MakeHisto (TString setName, TString subSetName, TString name, int numBinsX, double lowValueX, double highValueX, int numBinsY, double lowValueY, double highValueY, int numBinsZ, double lowValueZ, double highValueZ)
void MakeHisto (TString setName, TString subSetName, TString name, int numBinsX, double lowValueX, double highValueX, int numBinsY, double lowValueY, double highValueY, int numBinsZ, double lowValueZ, double highValueZ, int dimensions)
void AutoRetrieveHistograms ()
void RetrieveHistogram (TString setName, TString subsetName, TString histoName, unsigned int dimensions)
void FillHisto (TString setName, TString subSetName, TString name, double valX, double eventWeight)
void FillHisto (TString setName, TString subSetName, TString name, double valX, double valY, double eventWeight)
void FillHisto (TString setName, TString subSetName, TString name, double valX, double valY, double valZ, double eventWeight)
void FillHisto (TString setName, TString subSetName, TString name, double valX, double valY, double valZ, double eventWeight, int dimensions)
void WriteHisto (TString setName, TString subSetName, TString name)
void HistoTask (TString action, TString setName, TString subSetName, TString name, int numBinsX, double lowValueX, double highValueX, double fillValX, double eventWeight)
void HistoTask (TString action, TString setName, TString subSetName, TString name, int numBinsX, double lowValueX, double highValueX, int numBinsY, double lowValueY, double highValueY, double fillValX, double fillValY, double eventWeight)
void HistoTask (TString action, TString setName, TString subSetName, TString name, int numBinsX, double lowValueX, double highValueX, int numBinsY, double lowValueY, double highValueY, int numBinsZ, double lowValueZ, double highValueZ, double fillValX, double fillValY, double fillValZ, double eventWeight)
void HistoTask (TString action, TString setName, TString subSetName, TString name, int numBinsX, double lowValueX, double highValueX, int numBinsY, double lowValueY, double highValueY, int numBinsZ, double lowValueZ, double highValueZ, int dimensions, double fillValX, double fillValY, double fillValZ, double eventWeight)

Private Member Functions

 Histograms (const Histograms &)
void operator= (const Histograms &)

Private Attributes

std::map< unsigned int,
std::map< TString, std::map
< TString, std::map< TString,
TH1 * > > > > 
_histos
std::vector< TString > _setNames
TFile * _file

Detailed Description

Definition at line 209 of file NueUtilities.h.


Constructor & Destructor Documentation

NueUtilities::Histograms::Histograms (  ) 

Definition at line 1014 of file NueUtilities.cxx.

References _file.

01015 {
01016  _file=NULL;
01017 }

NueUtilities::Histograms::~Histograms (  ) 

Definition at line 1019 of file NueUtilities.cxx.

References CloseFile().

01020 {
01021  CloseFile();
01022 }

NueUtilities::Histograms::Histograms ( const Histograms  )  [inline, private]

Definition at line 297 of file NueUtilities.h.

00297 {};


Member Function Documentation

void NueUtilities::Histograms::AutoRetrieveHistograms (  )  [protected]

Definition at line 720 of file NueUtilities.cxx.

References _file, RetrieveHistogram(), and FidVol::setName().

Referenced by OpenFile().

00721 {
00722   TString currentDir = gDirectory->GetPath();
00723   _file->cd();
00724 
00725   // loop over all keys in this directory
00726   TIter nextSetKey( _file->GetListOfKeys() );
00727   TKey *setKey;
00728   while((setKey = (TKey*)nextSetKey()))
00729   {
00730     TObject *set = setKey->ReadObj();
00731     if( set->IsA()->InheritsFrom( "TDirectory" ) )
00732     {
00733       TString setName = set->GetName();
00734       _file->cd(setName);
00735       TIter nextSubsetKey( gDirectory->GetListOfKeys() );
00736       TKey *subsetKey;
00737       while((subsetKey = (TKey*)nextSubsetKey()))
00738       {
00739         TObject *subset = subsetKey->ReadObj();
00740         if( subset->IsA()->InheritsFrom( "TDirectory" ) )
00741         {
00742           TString subsetName = subset->GetName();
00743           _file->cd(setName+"/"+subsetName);
00744           TIter nextHistoKey( gDirectory->GetListOfKeys() );
00745           TKey *histoKey;
00746           while((histoKey = (TKey*)nextHistoKey()))
00747           {
00748             TObject *histo = histoKey->ReadObj();
00749             if( histo->IsA()->InheritsFrom( "TH1" ) )
00750             {
00751               TString histoName = histo->GetName();
00752               int dimensions = 1;
00753               if( histo->IsA()->InheritsFrom( "TH3" ) ) dimensions = 3;
00754               else if( histo->IsA()->InheritsFrom( "TH2" ) ) dimensions = 2;
00755               RetrieveHistogram(setName,subsetName,histoName,dimensions);
00756             }//If found directory subset
00757           } // while subset keys
00758           _file->cd(setName);  //return to set dir
00759         }//If found directory subset
00760       } // while subset keys
00761      _file->cd();  //return to top dir
00762     }//If found directory set
00763   } // while set keys
00764 
00765   //return to original directory
00766   gROOT->cd(currentDir);
00767 }

void NueUtilities::Histograms::CloseFile ( void   )  [protected]

Definition at line 1024 of file NueUtilities.cxx.

References _file, _histos, and _setNames.

Referenced by OpenFile(), and ~Histograms().

01025 {
01026  if(_file != NULL)
01027  {
01028   //This should delete the histogram pointers
01029   _file->Close();
01030   delete _file;
01031  }
01032  _file=NULL;
01033  _histos.clear(); //shouldn't cause memory leak if _file->Close() worked
01034  _setNames.clear();
01035 }

bool NueUtilities::Histograms::DoesSetExist ( TString  setName  )  const

Definition at line 1062 of file NueUtilities.cxx.

References _setNames, and find().

Referenced by MakeHisto(), and RetrieveHistogram().

01063 {
01064   return (_setNames.end() != find (_setNames.begin(), _setNames.end(), setName) );
01065 }

void NueUtilities::Histograms::FillHisto ( TString  setName,
TString  subSetName,
TString  name,
double  valX,
double  valY,
double  valZ,
double  eventWeight,
int  dimensions 
) [protected]

Definition at line 813 of file NueUtilities.cxx.

References _histos, exit(), and find().

00816 {
00817   if( _histos.find(dimensions) == _histos.end() )
00818   {
00819    cout << "\n\n ERROR: Cannot find set of histograms for TH"<< dimensions<< "D"<< endl;
00820    cout << " Call to fill "<< setName << " " << subSetName << " " << name << endl << endl;
00821    exit(1);
00822   }
00823 
00824 
00825   if( _histos[dimensions].find(setName) == _histos[dimensions].end() )
00826   {
00827    cout << "\n\n ERROR: Cannot find SetName of TH"<< dimensions<< "D histograms for:"<< endl;
00828    cout << " Fill to "<< setName << " " << subSetName << " " << name << endl << endl;
00829    exit(1);
00830   }
00831 
00832 
00833   if( _histos[dimensions][setName].find(subSetName) == _histos[dimensions][setName].end() )
00834   {
00835    cout << "\n\n ERROR: Cannot find SubSetName of TH"<< dimensions<< "D histograms for:"<< endl;
00836    cout << " Fill to "<< setName << " " << subSetName << " " << name << endl << endl;
00837    exit(1);
00838   }
00839 
00840   if( _histos[dimensions][setName][subSetName].find(name) == _histos[dimensions][setName][subSetName].end() )
00841   {
00842    cout << "\n\n ERROR: Cannot find HistoName of TH"<< dimensions<< "D histograms for:"<< endl;
00843    cout << " Fill to "<< setName << " " << subSetName << " " << name << endl << endl;
00844    exit(1);
00845  }
00846 
00847   
00848   if( 1 == dimensions)
00849   {
00850            ((TH1D*)_histos[dimensions][setName][subSetName][name])->Fill(valX, eventWeight);
00851   }
00852   else if( 2 == dimensions)
00853   {
00854            ((TH2D*)_histos[dimensions][setName][subSetName][name])->Fill(valX, valY, eventWeight);
00855   }
00856   else if( 3 == dimensions)
00857   {
00858            ((TH3D*)_histos[dimensions][setName][subSetName][name])->Fill(valX, valY, valZ, eventWeight);
00859   }
00860   else
00861   {
00862            cout << "\n\nERROR: Don't know how to fill histogram of " << dimensions << " dimensions\n"<<endl;
00863            exit(1);
00864   }
00865 }

void NueUtilities::Histograms::FillHisto ( TString  setName,
TString  subSetName,
TString  name,
double  valX,
double  valY,
double  valZ,
double  eventWeight 
) [protected]

Definition at line 806 of file NueUtilities.cxx.

References FillHisto().

00808 {
00809  FillHisto(setName,subSetName,name,valX, valY, valZ, eventWeight, 3);
00810 }

void NueUtilities::Histograms::FillHisto ( TString  setName,
TString  subSetName,
TString  name,
double  valX,
double  valY,
double  eventWeight 
) [protected]

Definition at line 799 of file NueUtilities.cxx.

References FillHisto().

00801 {
00802  FillHisto(setName,subSetName,name,valX, valY, 0, eventWeight, 2);
00803 }

void NueUtilities::Histograms::FillHisto ( TString  setName,
TString  subSetName,
TString  name,
double  valX,
double  eventWeight 
) [protected]

Definition at line 792 of file NueUtilities.cxx.

Referenced by FillHisto(), and HistoTask().

00794 {
00795  FillHisto(setName,subSetName,name,valX, 0, 0, eventWeight, 1);
00796 }

TH1 * NueUtilities::Histograms::GetHistogram ( TString  set,
TString  subset,
TString  name 
)

Definition at line 601 of file NueUtilities.cxx.

References _histos, exit(), and find().

00602 {
00603  for( unsigned int dimensions=1; dimensions <= 3; ++dimensions)
00604  {
00605   if( _histos.find(dimensions) != _histos.end() )
00606   {
00607    if( _histos[dimensions].find(setName) != _histos[dimensions].end() )
00608    {
00609     if( _histos[dimensions][setName].find(subSetName) != _histos[dimensions][setName].end() )
00610     {
00611      if( _histos[dimensions][setName][subSetName].find(name) != _histos[dimensions][setName][subSetName].end() )
00612      {
00613        return( _histos[dimensions][setName][subSetName][name] );
00614      }//Does name exist
00615     }//Does subSetName exist
00616    }//Does setName exist
00617   }//Does Dimension exist
00618  }
00619 
00620  cout << "\n\n ERROR: Cannot find histogram for:"<< endl;
00621  cout << " "<< setName << " " << subSetName << " " << name << endl << endl;
00622  exit(1);
00623  return(NULL);
00624 }

TString NueUtilities::Histograms::GetSetName ( unsigned int  setIndex  )  const

Definition at line 1072 of file NueUtilities.cxx.

References _setNames.

01073 {
01074  if( setIndex < _setNames.size() )
01075  {
01076    return(_setNames[setIndex]);
01077  }
01078  return("");
01079 }

void NueUtilities::Histograms::HistoTask ( TString  action,
TString  setName,
TString  subSetName,
TString  name,
int  numBinsX,
double  lowValueX,
double  highValueX,
int  numBinsY,
double  lowValueY,
double  highValueY,
int  numBinsZ,
double  lowValueZ,
double  highValueZ,
int  dimensions,
double  fillValX,
double  fillValY,
double  fillValZ,
double  eventWeight 
) [protected]

Definition at line 972 of file NueUtilities.cxx.

References exit(), FillHisto(), MakeHisto(), and WriteHisto().

00977 {
00978 
00979  if( "make" == action)
00980  {
00981   MakeHisto(setName, subSetName, name,
00982             numBinsX, lowValueX, highValueX,
00983             numBinsY, lowValueY, highValueY,
00984             numBinsZ, lowValueZ, highValueZ,
00985             dimensions);
00986  }
00987  else if( "fill" == action)
00988  {
00989   FillHisto(setName, subSetName, name,
00990             fillValX, fillValY, fillValZ,
00991             eventWeight, dimensions);
00992  }
00993  else if( "write" == action)
00994  {
00995   WriteHisto(setName, subSetName, name);
00996  }
00997  else
00998  {
00999   cout << "\n\nERROR: Unknown HistoTask = " << action << "\n"<<endl;
01000   exit(1);
01001  }
01002 
01003 }

void NueUtilities::Histograms::HistoTask ( TString  action,
TString  setName,
TString  subSetName,
TString  name,
int  numBinsX,
double  lowValueX,
double  highValueX,
int  numBinsY,
double  lowValueY,
double  highValueY,
int  numBinsZ,
double  lowValueZ,
double  highValueZ,
double  fillValX,
double  fillValY,
double  fillValZ,
double  eventWeight 
) [protected]

Definition at line 959 of file NueUtilities.cxx.

References HistoTask().

00964 {
00965  HistoTask(action, setName, subSetName, name,
00966            numBinsX, lowValueX, highValueX,
00967            numBinsY, lowValueY, highValueY,
00968            numBinsZ, lowValueZ, highValueZ,
00969            3, fillValX, fillValY, fillValZ, eventWeight);
00970 }

void NueUtilities::Histograms::HistoTask ( TString  action,
TString  setName,
TString  subSetName,
TString  name,
int  numBinsX,
double  lowValueX,
double  highValueX,
int  numBinsY,
double  lowValueY,
double  highValueY,
double  fillValX,
double  fillValY,
double  eventWeight 
) [protected]

Definition at line 947 of file NueUtilities.cxx.

References HistoTask().

00951 {
00952  HistoTask(action, setName, subSetName, name,
00953            numBinsX, lowValueX, highValueX,
00954            numBinsY, lowValueY, highValueY,
00955            0,0,0,
00956            2, fillValX, fillValY, 0, eventWeight);
00957 }

void NueUtilities::Histograms::HistoTask ( TString  action,
TString  setName,
TString  subSetName,
TString  name,
int  numBinsX,
double  lowValueX,
double  highValueX,
double  fillValX,
double  eventWeight 
) [protected]

Definition at line 936 of file NueUtilities.cxx.

Referenced by HistoTask().

00939 {
00940  HistoTask(action, setName, subSetName, name,
00941            numBinsX, lowValueX, highValueX,
00942            0,0,0,
00943            0,0,0,
00944            1, fillValX, 0, 0, eventWeight);
00945 }

void NueUtilities::Histograms::MakeHisto ( TString  setName,
TString  subSetName,
TString  name,
int  numBinsX,
double  lowValueX,
double  highValueX,
int  numBinsY,
double  lowValueY,
double  highValueY,
int  numBinsZ,
double  lowValueZ,
double  highValueZ,
int  dimensions 
) [protected]

Definition at line 669 of file NueUtilities.cxx.

References _file, _histos, _setNames, DoesSetExist(), exit(), and find().

00674 {
00675  if( _histos[dimensions][setName][subSetName].find(name) != _histos[dimensions][setName][subSetName].end() )
00676  {
00677   cout << "\n\nError overwriting " << "th"<< dimensions << "d_"+setName+"_"+subSetName+"_"+name << endl;
00678   delete (_histos[dimensions][setName][subSetName][name]);
00679   exit(1);
00680  }
00681 
00682 
00683   TString currentDir = gDirectory->GetPath();
00684   _file->cd();
00685 
00686   if( 1 == dimensions)
00687   {
00688            _histos[dimensions][setName][subSetName][name] = new TH1D("th1d_"+setName+"_"+subSetName+"_"+name,"",
00689                                                             numBinsX,lowValueX,highValueX);
00690            ((TH1D*)_histos[dimensions][setName][subSetName][name])->Sumw2();  //Always do a Sumw2() to get errors right
00691   }
00692   else if( 2 == dimensions)
00693   {
00694            _histos[dimensions][setName][subSetName][name] = new TH2D("th2d_"+setName+"_"+subSetName+"_"+name,"",
00695                                                             numBinsX,lowValueX,highValueX, numBinsY,lowValueY,highValueY);
00696            ((TH2D*)_histos[dimensions][setName][subSetName][name])->Sumw2();  //Always do a Sumw2() to get errors right
00697   }
00698   else if( 3 == dimensions)
00699   {
00700            _histos[dimensions][setName][subSetName][name] = new TH3D("th3d_"+setName+"_"+subSetName+"_"+name,"",
00701                                                             numBinsX,lowValueX,highValueX, numBinsY,lowValueY,highValueY, numBinsZ,lowValueZ,highValueZ);
00702            ((TH3D*)_histos[dimensions][setName][subSetName][name])->Sumw2();  //Always do a Sumw2() to get errors right
00703   }
00704   else
00705   {
00706            cout << "\n\nERROR: Don't know how to make histogram of " << dimensions << " dimensions\n"<<endl;
00707            gROOT->cd(currentDir);
00708            exit(1);
00709   }
00710 
00711   gROOT->cd(currentDir);
00712 
00713   if( !DoesSetExist(setName) ) _setNames.push_back(setName);
00714 
00715 }

void NueUtilities::Histograms::MakeHisto ( TString  setName,
TString  subSetName,
TString  name,
int  numBinsX,
double  lowValueX,
double  highValueX,
int  numBinsY,
double  lowValueY,
double  highValueY,
int  numBinsZ,
double  lowValueZ,
double  highValueZ 
) [protected]

Definition at line 656 of file NueUtilities.cxx.

References MakeHisto().

00660 {
00661  MakeHisto(setName,subSetName,name,
00662            numBinsX, lowValueX, highValueX,
00663            numBinsY, lowValueY, highValueY,
00664            numBinsZ, lowValueZ, highValueZ,
00665            3);
00666 }

void NueUtilities::Histograms::MakeHisto ( TString  setName,
TString  subSetName,
TString  name,
int  numBinsX,
double  lowValueX,
double  highValueX,
int  numBinsY,
double  lowValueY,
double  highValueY 
) [protected]

Definition at line 644 of file NueUtilities.cxx.

References MakeHisto().

00647 {
00648  MakeHisto(setName,subSetName,name,
00649            numBinsX, lowValueX, highValueX,
00650            numBinsY, lowValueY, highValueY,
00651            0,0,0,
00652            2);
00653 }

void NueUtilities::Histograms::MakeHisto ( TString  setName,
TString  subSetName,
TString  name,
int  numBinsX,
double  lowValueX,
double  highValueX 
) [protected]

Definition at line 633 of file NueUtilities.cxx.

Referenced by HistoTask(), and MakeHisto().

00635 {
00636  MakeHisto(setName,subSetName,name,
00637            numBinsX, lowValueX, highValueX,
00638            0,0,0,
00639            0,0,0,
00640            1);
00641 }

unsigned int NueUtilities::Histograms::NumSets (  )  const

Definition at line 1067 of file NueUtilities.cxx.

References _setNames.

01068 {
01069  return (_setNames.size());
01070 }

bool NueUtilities::Histograms::OpenFile ( TString  outputFile,
TString  option = "READ" 
) [protected]

Definition at line 1037 of file NueUtilities.cxx.

References _file, AutoRetrieveHistograms(), and CloseFile().

01038 {
01039  CloseFile();
01040  _file = new TFile(outputFile, option);
01041 
01042  if(_file != NULL && _file->IsOpen())
01043  {
01044  
01045    //If reading the file, then get the histograms in the file
01046    if(
01047         (option.Data()[0] == 'r' || option.Data()[0] == 'R')
01048       &&(option.Data()[1] == 'e' || option.Data()[1] == 'E')
01049       &&(option.Data()[2] == 'a' || option.Data()[2] == 'A')
01050       &&(option.Data()[3] == 'd' || option.Data()[3] == 'D')
01051      )
01052    {
01053     AutoRetrieveHistograms();
01054    }
01055   
01056    return(true);
01057  }
01058 
01059  return( false );
01060 }

void NueUtilities::Histograms::operator= ( const Histograms  )  [inline, private]

Definition at line 298 of file NueUtilities.h.

00298 {};

void NueUtilities::Histograms::RetrieveHistogram ( TString  setName,
TString  subsetName,
TString  histoName,
unsigned int  dimensions 
) [protected]

Definition at line 770 of file NueUtilities.cxx.

References _file, _histos, _setNames, DoesSetExist(), exit(), and find().

Referenced by AutoRetrieveHistograms().

00771 {
00772  //remove "thnd_" from the histoname
00773  TString name = histoName(5,histoName.Length()-5);
00774  if( _histos[dimensions][setName][subSetName].find(name) != _histos[dimensions][setName][subSetName].end() )
00775  {
00776   cout << "\n\nError overwriting " << "th"<< dimensions << "d_"+setName+"_"+subSetName+"_"+name << endl;
00777   cout << "How does that happen when reading a file??????"<< endl;
00778   exit(1);
00779  }
00780 
00781  _histos[dimensions][setName][subSetName][name] = (TH1*) _file->Get(setName+"/"+subSetName+"/"+histoName);
00782  if( !DoesSetExist(setName) ) _setNames.push_back(setName);
00783 }

void NueUtilities::Histograms::WriteHisto ( TString  setName,
TString  subSetName,
TString  name 
) [protected]

Definition at line 877 of file NueUtilities.cxx.

References _file, _histos, exit(), find(), and Form().

Referenced by HistoTask().

00878 {
00879  bool histoNotFound = true;
00880 
00881  for( unsigned int dimensions=1; dimensions <= 3; ++dimensions)
00882  {
00883   if( _histos.find(dimensions) != _histos.end() )
00884   {
00885    if( _histos[dimensions].find(setName) != _histos[dimensions].end() )
00886    {
00887     if( _histos[dimensions][setName].find(subSetName) != _histos[dimensions][setName].end() )
00888     {
00889      if( _histos[dimensions][setName][subSetName].find(name) != _histos[dimensions][setName][subSetName].end() )
00890      {
00891       histoNotFound = false;
00892 
00893       TString currentDir = gDirectory->GetPath();
00894       _file->cd();
00895 
00896       if( !_file->Get( setName ) )
00897       {
00898        gDirectory->mkdir(setName);
00899       }
00900       gDirectory->cd(setName);
00901 
00902       if( !_file->Get( setName+"/"+subSetName)) 
00903       {
00904        gDirectory->mkdir(subSetName);
00905       }
00906       gDirectory->cd(subSetName);
00907 
00908       _histos[dimensions][setName][subSetName][name]->SetName( Form("th%dd_",dimensions)+name );
00909       _histos[dimensions][setName][subSetName][name]->Write();
00910 
00911       gROOT->cd(currentDir);
00912        
00913      }//Does name exist
00914     }//Does subSetName exist
00915    }//Does setName exist
00916   }//Dimension
00917  }
00918 
00919   if( histoNotFound )
00920   {
00921    cout << "\n\n ERROR: Cannot write histogram for:"<< endl;
00922    cout << " "<< setName << " " << subSetName << " " << name << endl << endl;
00923    exit(1);
00924   }
00925 }


Member Data Documentation

std::map< unsigned int, std::map< TString, std::map< TString, std::map< TString,TH1*> > > > NueUtilities::Histograms::_histos [private]
std::vector< TString> NueUtilities::Histograms::_setNames [private]

The documentation for this class was generated from the following files:

Generated on 15 Jul 2018 for loon by  doxygen 1.6.1