/** * \file CbmHistManager.cxx * \brief Histogram manager. * \author Semen Lebedev * \date 2011 */ #include "CbmHistManager.h" #include "TH1.h" #include "TH2.h" #include "TNamed.h" #include "TGraph.h" #include "TGraph2D.h" #include "TProfile.h" #include "TProfile2D.h" #include "TFile.h" #include "TDirectory.h" #include "TKey.h" #include "TClass.h" #include #include #include #include #include #include #include using std::string; using std::map; using std::cout; using std::endl; using std::vector; using std::exception; using std::sort; class CompareTNamedMore: public std::binary_function< const TNamed*, const TNamed*, Bool_t> { public: Bool_t operator()(const TNamed* object1, const TNamed* object2) const { return string(object1->GetName()) > string(object2->GetName()); } }; CbmHistManager::CbmHistManager(): fMap() { } CbmHistManager::~CbmHistManager() { } template vector CbmHistManager::ObjectVector( const string& pattern) const { vector objects; try { const boost::regex e(pattern); map::const_iterator it; for (it = fMap.begin(); it != fMap.end(); it++) { if (boost::regex_match(it->first, e)) objects.push_back((T)it->second); } } catch (exception& ex) { cout << "Exception in CbmHistManager::ObjectVector: " << ex.what() << endl; } sort(objects.begin(), objects.end(), CompareTNamedMore()); return objects; } vector CbmHistManager::H1Vector( const string& pattern) const { return ObjectVector(pattern); } vector CbmHistManager::H2Vector( const string& pattern) const { return ObjectVector(pattern); } vector CbmHistManager::G1Vector( const string& pattern) const { return ObjectVector(pattern); } vector CbmHistManager::G2Vector( const string& pattern) const { return ObjectVector(pattern); } vector CbmHistManager::P1Vector( const string& pattern) const { return ObjectVector(pattern); } vector CbmHistManager::P2Vector( const string& pattern) const { return ObjectVector(pattern); } void CbmHistManager::WriteToFile() { map::iterator it; for (it = fMap.begin(); it != fMap.end(); it++){ it->second->Write(); } } void CbmHistManager::ReadFromFile( TFile* file) { assert(file != NULL); cout << "-I- CbmHistManager::ReadFromFile" << endl; TDirectory* dir = gDirectory; TIter nextkey(dir->GetListOfKeys()); TKey *key; Int_t c = 0; while (key = (TKey*) nextkey()) { TObject* obj = key->ReadObj(); if (obj->IsA()->InheritsFrom (TH1::Class()) || obj->IsA()->InheritsFrom (TGraph::Class()) || obj->IsA()->InheritsFrom (TGraph2D::Class())) { TNamed* h = (TNamed*) obj; TNamed* h1 = (TNamed*)file->Get(h->GetName()); Add(string(h->GetName()), h1); //cout << c++ << " " << h->GetName()<< endl; } } } void CbmHistManager::Clear() { map::iterator it; for (it = fMap.begin(); it != fMap.end(); it++) { delete (*it).second; } fMap.clear(); } void CbmHistManager::ShrinkEmptyBins( const string& histName) { TH1* hist = H1(histName); Int_t nofBins = hist->GetNbinsX(); Int_t shrinkBin = 0; for (Int_t iBin = nofBins; iBin > 0; iBin--) { Double_t content = hist->GetBinContent(iBin); if (content != 0.) { shrinkBin = iBin; break; } } hist->GetXaxis()->SetRange(1, shrinkBin + 1); } void CbmHistManager::ShrinkEmptyBinsByPattern( const string& pattern) { vector effHistos = H1Vector(pattern); Int_t nofEffHistos = effHistos.size(); for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) { ShrinkEmptyBins(effHistos[iHist]->GetName()); } } void CbmHistManager::Scale( const string& histName, Double_t scale) { H1(histName)->Scale(scale); } void CbmHistManager::ScaleByPattern( const string& pattern, Double_t scale) { vector effHistos = H1Vector(pattern); Int_t nofEffHistos = effHistos.size(); for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) { Scale(effHistos[iHist]->GetName(), scale); } } void CbmHistManager::Rebin( const string& histName, Int_t ngroup) { TH1* hist = H1(histName); if (ngroup > 1) { hist->Rebin(ngroup); hist->Scale(1. / (Double_t)ngroup); } } void CbmHistManager::RebinByPattern( const string& pattern, Int_t ngroup) { vector effHistos = H1Vector(pattern); Int_t nofEffHistos = effHistos.size(); for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) { Rebin(effHistos[iHist]->GetName(), ngroup); } } string CbmHistManager::ToString() const { string str = "CbmHistManager list of histograms:\n"; map::const_iterator it; for (it = fMap.begin(); it != fMap.end(); it++){ str += it->first + "\n"; } return str; } ClassImp(CbmHistManager)