/* * PndMvdHistogramCollector.cxx * * Created on: April 24, 2013 * Author: s.esch */ #include "PndMvdHistogramCollector.h" #include "TList.h" #include "TCollection.h" #include "TObject.h" #include "TKey.h" #include "TClass.h" #include #include PndMvdHistogramCollector::PndMvdHistogramCollector():fCountGoodFiles(0),fVerbose(0) { // TODO Auto-generated constructor stub } PndMvdHistogramCollector::~PndMvdHistogramCollector() { // TODO Auto-generated destructor stub } void PndMvdHistogramCollector::AddFile(TFile* f) { if (f!=0) { //std::cout << "zombie " << f->IsZombie() << std::endl; if (f->IsZombie()==kTRUE) { std::cout << "Root file is a ZOMBIE! I will not analyse this file... " <0) { std::cout << "File: " << f->GetName() << " added to file list with f->IsOpen(): " << f->IsOpen() << std::endl; std::cout << "File pointer "<< f << std::endl; } } } } void PndMvdHistogramCollector::AnalyzeFiles() { for (unsigned int i = 0; i < fFileList.size(); i++){ TFile* file = fFileList[i]; if (file > 0){ if(fVerbose>0) { std::cout << "File " << file->GetName() << " open for adding "<< std::endl; std::cout << "open? " << file->IsOpen() << std::endl; std::cout << "File pointer " << file << std::endl; file->cd(); } TList* l = file->GetListOfKeys(); SumUpHistograms(l); //f->Close(); } } if(fVerbose>0) { std::cout << "Analyzing of histograms done " << std::endl; } } bool PndMvdHistogramCollector::SumUpHistograms(TList* list) { //std::cout << "SumUpHistogram called " << std::endl; TIter iter(list); TKey *key; fCountGoodFiles++; while ((key= (TKey*)iter())) { TClass *c1 = gROOT->GetClass(key->GetClassName()); if(c1->InheritsFrom("TDirectoryFile")) { gDirectory->cd(key->GetName()); //std::cout << "#################### "<< std::endl; TList* deeperlist = gDirectory->GetListOfKeys(); //std::cout << "PndMvdHistogramCollector::SumUpHistograms(TList* list)" << deeperlist->GetEntries() << std::endl; SumUpHistograms(deeperlist); gDirectory->cd("../"); } else if(c1->InheritsFrom("TH1")) { TString newName = key->GetName(); //std::cout << "key->GetName()" <GetName()<< std::endl; TH1 *h = (TH1*)key->ReadObj(); if(newName == "fAngularDistributionLambdaCMSCosTheta" ) { std::cout << "Histogram for adding found! " << newName << std::endl; gDirectory->pwd(); std::cout << "Histogram for adding found! Mean " << h->GetMean() << std::endl; std::cout << "Histogram for adding found! Mean " << h->GetRMS() << std::endl; std::cout << "Histogram for adding found! Bin1 " << h->GetBinContent(1) << std::endl; std::cout << "Histogram for adding found! Pointer " << h << std::endl; } AddHisto(h); } } //std::cout << "SumUpHistogram finished " << std::endl; return true; } void PndMvdHistogramCollector::AddHisto(TH1* histo) { TString path=gDirectory->GetPath(); path +="/"; path.Remove(0,path.First(":")+2); path.Remove(path.First("/")); TString mapName = histo->GetName(); if(path != "") { mapName += "-"; mapName += path; } // std::cout << "mapName: "<< mapName << std::endl; if(fHistoMap.count(mapName) == 0){ fHistoMap[mapName] = histo; //std::cout << "creating histogram " << mapName << std::endl; } else{ fHistoMap[mapName]->Add(histo); if(mapName=="fAngularDistributionLambdaCMSCosTheta-MC_Analysis") { std::cout<< "mean " << histo->GetMean() << std::endl; } } if(fVerbose>2) { //std::cout << "Adding histogram: " << mapName << " with type: " << histo->ClassName() << std::endl; } } //void PndMvdHistogramCollector::AddHisto(TH1F* histo) //{ // TString mapName = histo->GetName(); // //// std::cout << "histo->GetName(): "<< histo->GetName() << std::endl; //// std::cout << "fHistoMap1F.size() " << fHistoMap1F.size() << std::endl; // // mapName.Append("_sum"); // if(fHistoMap1F.count(mapName) == 0){ // TH1F* newHisto = new TH1F(*histo); //// std::cout << "histo pointer " << newHisto << std::endl; //// std::cout << "histo name >>" << mapName.Data() << "<<" << std::endl; // newHisto->SetName(mapName); // fHistoMap1F[mapName] = newHisto; // } // else{ // fHistoMap1F[mapName]->Add(histo); // } // if(fVerbose>2) // { // std::cout << "Adding histogram: " << histo->GetName() << " with type: " << histo->ClassName() << std::endl; // } //} // //void PndMvdHistogramCollector::AddHisto(TH2F* histo) //{ // TString mapName = histo->GetName(); // //// std::cout << "histo->GetName(): "<< histo->GetName() << std::endl; //// std::cout << "fHistoMap2F.size()" << fHistoMap2F.size() << std::endl; // mapName.Append("_sum"); // if(fHistoMap2F.count(mapName) == 0){ // TH2F* newHisto = new TH2F(*histo); // newHisto->SetName(mapName); // fHistoMap2F[mapName] = newHisto; // } // else{ // fHistoMap2F[mapName]->Add(histo); // } // if(fVerbose>2) // { // std::cout << "Adding histogram: " << histo->GetName() << " with type: " << histo->ClassName() << std::endl; // } //} // //void PndMvdHistogramCollector::AddHisto(TH1I* histo) //{ // TString mapName = histo->GetName(); // // std::cout << "histo->GetName(): "<< histo->GetName() << std::endl; // // mapName.Append("_sum"); // if(fHistoMap1I.count(mapName) == 0){ // TH1I* newHisto = new TH1I(*histo); // newHisto->SetName(mapName); // fHistoMap1I[mapName] = newHisto; // } // else{ // fHistoMap1I[mapName]->Add(histo); // } // if(fVerbose>2) // { // std::cout << "Adding histogram: " << histo->GetName() << " with type: " << histo->ClassName() << " to List " << std::endl; // } //} // //void PndMvdHistogramCollector::AddHisto(TH2I* histo) //{ // TString mapName = histo->GetName(); // std::cout << "histo->GetName(): "<< histo->GetName() << std::endl; // // // mapName.Append("_sum"); // if(fHistoMap2I.count(mapName) == 0){ // TH2I* newHisto = new TH2I(*histo); // newHisto->SetName(mapName); // fHistoMap2I[mapName] = newHisto; // } // else{ // fHistoMap2I[mapName]->Add(histo); // } // if(fVerbose>2) // { // std::cout << "Adding histogram: " << histo->GetName() << " with type: " << histo->ClassName() << std::endl; // } //} TH1F* PndMvdHistogramCollector::GetHisto1F(int i) { std::map::iterator iter = fHistoMap1F.begin(); for (int j = 0; j < i; j++)iter++; return iter->second; } TH1F* PndMvdHistogramCollector::GetHistoByName1F(TString name) { return fHistoMap1F[name]; } TH2F* PndMvdHistogramCollector::GetHisto2F(int i) { std::map::iterator iter = fHistoMap2F.begin(); for (int j = 0; j < i; j++)iter++; return iter->second; } TH1I* PndMvdHistogramCollector::GetHistoByName1I(TString name) { return fHistoMap1I[name]; } TH2I* PndMvdHistogramCollector::GetHisto2I(int i) { std::map::iterator iter = fHistoMap2I.begin(); for (int j = 0; j < i; j++)iter++; return iter->second; } TH2F* PndMvdHistogramCollector::GetHistoByName2F(TString name) { if (fHistoMap2F.count(name)>0) return fHistoMap2F[name]; return 0; } void PndMvdHistogramCollector::SaveHistos(TString fileName) { TFile f(fileName,"RECREATE"); if(fVerbose>0) { std::cout << "Outputfile created with name: " << f.GetName() << " Status IsOpen() "<< f.IsOpen() << std::endl; } for (std::map::const_iterator iter = fHistoMap.begin(); iter != fHistoMap.end(); iter++){ if (iter->second != 0){ // std::cout << "GetMapName " << iter->first << std::endl; // std::cout << "iter->second->GetName() " << iter->second->GetName() << std::endl; iter->second->SetName(iter->first); iter->second->Write(iter->first); } } for (std::map::const_iterator iter = fHistoMap2F.begin(); iter != fHistoMap2F.end(); iter++){ if (iter->second != 0){ //std::cout << "GetMapName " << iter->first << std::endl; iter->second->Write(iter->first); } } std::cout << "TH2F done " << std::endl; for (std::map::const_iterator iter = fHistoMap1F.begin(); iter != fHistoMap1F.end(); iter++){ if (iter->second != 0){ //std::cout << "GetMapName " << iter->first << std::endl; iter->second->Write(iter->first); } } //std::cout << "TH1F done " << std::endl; for (std::map::const_iterator iter = fHistoMap1I.begin(); iter != fHistoMap1I.end(); iter++){ if (iter->second != 0){ //std::cout << "GetMapName " << iter->first << std::endl; iter->second->Write(iter->first); //std::cout << "Histogram written " << iter->second->GetName() << std::endl; } } for (std::map::const_iterator iter = fHistoMap2I.begin(); iter != fHistoMap2I.end(); iter++){ if (iter->second != 0){ //std::cout << "GetMapName " << iter->first << std::endl; iter->second->Write(iter->first); //std::cout << "Histogram written " << iter->second->GetName() << std::endl; } } f.Close(); }