#include #include #include #include #include "TFile.h" #include "TH2F.h" #include "PDGHistos.h" #include "TObjArray.h" #include "TCanvas.h" #include "PDGMap.h" #include "FitServices.h" using namespace std; HistogramSet::HistogramSet() { fIterator=fHistoMap.begin(); fSummary=NULL; fVon_x=0.; fBis_x=0.; fNbin_x=0; fVon_y=0.; fBis_y=0.; fNbin_y=0; ReturnSummary=false; } HistogramSet::~HistogramSet() { for(fIterator=fHistoMap.begin(); fIterator!=fHistoMap.end(); ++fIterator) { delete fIterator->second; fIterator->second = NULL; } if(fSummary) { delete fSummary; } } bool HistogramSet::Add(TH2F *pHisto,const std::string &HistogramName) { if(fHistoMap.find(HistogramName)!=fHistoMap.end()) { return false; } if(fHistoMap.empty()) { cout << "HistogramSet: Adding first histogram." << endl; GetHistoProperties(pHisto, fNbin_x, fVon_x, fBis_x, fNbin_y, fVon_y, fBis_y); } else { if( !IsAllowed(pHisto)) { return false; } } cout << "HistogramSet: Adding histogram" << HistogramName << endl; fHistoMap[HistogramName]=pHisto; fIterator=fHistoMap.begin(); assert(fIterator!=fHistoMap.end()); return true; } bool HistogramSet::IsAllowed(TH2F *pHisto) { int nbin_x=0; double von_x=0.; double bis_x=0.; int nbin_y=0; double von_y=0.; double bis_y=0.; GetHistoProperties(pHisto, nbin_x, von_x, bis_x,nbin_y, von_y, bis_y); if( fNbin_x == nbin_x && fVon_x == von_x && fBis_x == bis_x && fNbin_y == nbin_y && fVon_y == von_y && fBis_y == bis_y ) { return true; } return false; } bool HistogramSet::Has(const std::string &HistogramName) { if(fHistoMap.find(HistogramName)!=fHistoMap.end()) { cout << "HistogramSet::Has " << HistogramName << endl; return true; } cout << "HistogramSet::Has not " << HistogramName << endl; return false; } TH2F *HistogramSet::Get(const std::string &HistogramName) { if(Has(HistogramName)) { return (fHistoMap.find(HistogramName))->second; } return NULL; } void HistogramSet::GetHistoProperties(TH2F *histo, int &nbin_x, double &von_x, double &bis_x, int &nbin_y, double &von_y, double &bis_y ) { assert(histo); von_x=histo->GetBinLowEdge(1); bis_x=histo->GetBinLowEdge(histo->GetNbinsX())+histo->GetXaxis()->GetBinWidth(histo->GetNbinsX()); nbin_x=histo->GetNbinsX(); von_y=histo->GetYaxis()->GetBinLowEdge(1); bis_y=histo->GetYaxis()->GetBinLowEdge(histo->GetNbinsY()) + histo->GetYaxis()->GetBinWidth(histo->GetNbinsY()); nbin_y=histo->GetNbinsY(); } void HistogramSet::CreateSummary() { if( fSummary ) { delete fSummary; } string SummaryName="Summary"+fNameAddOn; fSummary = new TH2F(SummaryName.c_str(),SummaryName.c_str(), fNbin_x , fVon_x ,fBis_x, fNbin_y, fVon_y ,fBis_y); assert(fSummary); std::map::const_iterator cit; for(cit=fHistoMap.begin();cit!=fHistoMap.end();++cit) { TH2F *particleHisto=cit->second; if(particleHisto) { cout << "Adding Histogram " << particleHisto->GetName() << endl; fSummary->Add( particleHisto ); } } } bool HistogramSet::GetNextParticleHistogram(TH2F *&pHisto) { cout << "Size of fHistoMap: " << fHistoMap.size() << endl; if(fIterator==fHistoMap.end()) { fIterator=fHistoMap.begin(); cout << "no particles left" << endl; return false; } pHisto=fIterator->second; assert(pHisto); ++fIterator; return true; } HistogramSet *HistogramSet::GetScaledHistogramSet(double ScalingFactor) const { HistogramSet *newSet=new HistogramSet; std::map::const_iterator cit; for(cit=fHistoMap.begin();cit!=fHistoMap.end();++cit) { TH2F *pCurHisto=cit->second; double von_x=fVon_x; double bis_x=fBis_x; int nbin_x=fNbin_x; double von_y=fVon_y; double bis_y=fBis_y; int nbin_y=fNbin_y; string name=pCurHisto->GetName(); string title=pCurHisto->GetTitle(); cout << name << ": von_x(" << von_x << ") bis_x(" << bis_x << ") von_y(" << von_y << ") bis_y(" << bis_y << ")" << endl; name=name+"scaled"; title=name+"scaled"; von_y/=ScalingFactor; bis_y/=ScalingFactor; cout << " ===> " << name << ": von_x(" << von_x << ") bis_x(" << bis_x << ") von_y(" << von_y << ") bis_y(" << bis_y << ")" << endl; TH2F *pScaledHisto = new TH2F(name.c_str(),title.c_str(), nbin_x , von_x ,bis_x, nbin_y, von_y ,bis_y); //i have to get every bin in the old histo, and get it to the new that should be all for(int i=0; iGetBinContent(i,j); pScaledHisto->SetBinContent(i,j,content); } } newSet->Add(pScaledHisto, name); newSet->SetNameAddOn("scaled"); cout << endl; } return newSet; } /* bool HistogramSet::GetNext(TH2F *pHisto) { if(ReturnSummary) { } if( !GetNextParticleHistogram(pHisto) ) { if(fSummary) { pHisto=fSummary; ReturnSummary=true; return true; } } return false; } */ bool HistogramSet::GetSummary(TH2F *&pHisto) { if(fSummary) { pHisto=fSummary; return true; } return false; } PDGHistos::PDGHistos():p_PDGMap(new PDGMap) { HistogramSet* standardSet=new HistogramSet; fHistoSets.push_back(standardSet); CurrentHistograms=standardSet; } PDGHistos::~PDGHistos() { vector::iterator it; for(it=fHistoSets.begin(); it!=fHistoSets.end(); it++) { assert(*it); delete (*it); (*it)=NULL; } delete p_PDGMap; } bool PDGHistos::Add(TH2F *pHisto,const string &HistogramName) { if(!pHisto) { cout << "PDGHistos::Add - Error: Histogram Pointer is NULL Pointer" << endl; return false; } if(!CurrentHistograms->Add( pHisto, HistogramName ) ) { cout << "PDGHistos::Add - Error: Can' t add a Histogram that is already managed!" << endl; return false; } //cout << "Setting Name of " << pHisto->GetName() << " to " << HistogramName << endl; pHisto->SetNameTitle(HistogramName.c_str(), HistogramName.c_str()); //m_vpHistos.push_back(pHisto); //m_HistoMap[HistogramName]=pHisto; return true; } std::string PDGHistos::GetParticleName(Int_t PDGCode) { if(p_PDGMap->Particles.find(PDGCode)!=p_PDGMap->Particles.end()) { return (p_PDGMap->Particles)[PDGCode]; } else { return "UnknownParticle"; } } void PDGHistos::Save(const string &strPathFilename) { TObjArray Histos(0); //map::const_iterator cit; std::vector::const_iterator cit; for(cit=fHistoSets.begin(); cit!=fHistoSets.end(); ++cit) { cout << "Try to save histogram!" << endl; TH2F *pCurHisto=NULL; while((*cit)->GetNextParticleHistogram(pCurHisto)) { cout << "Got next Particle Histo" << endl; if(pCurHisto) { Histos.Add(pCurHisto); printf("Saving HistoName: %s\n", pCurHisto->GetName()); } assert(pCurHisto); } (*cit)->CreateSummary(); if((*cit)->GetSummary(pCurHisto)) { if(pCurHisto) { Histos.Add(pCurHisto); printf("Saving HistoName: %s\n", pCurHisto->GetName()); } } } TFile f(strPathFilename.c_str(),"recreate"); Histos.Write(); f.Close(); } //i don' t know, but Roots TH2::Scale function does just nothing //so i have to do it myself :-( void PDGHistos::Scale() { double ScalingFactor=1.; FitServices *PionFit = new FitServices; //absturz erst am Ende if( CurrentHistograms->Has(GetParticleName(211)) ) { PionFit->SetParticleHistogram( CurrentHistograms->Get(GetParticleName(211)) ); ScalingFactor=PionFit->GetBBMinimum(); cout << "Got " << ScalingFactor << " from PionFit" << endl; } PionFit->DrawBBFit(); delete PionFit; Scale(ScalingFactor); } void PDGHistos::Scale(double ScalingFactor) { HistogramSet* scaledSet=CurrentHistograms->GetScaledHistogramSet(ScalingFactor); fHistoSets.push_back(scaledSet); }