//____________________________________________________________________ // // $Id: makeVarBinSpectra.C,v 1.2 2003/09/23 10:27:21 zbyin Exp $ // $Author: zbyin $ // $Date: 2003/09/23 10:27:21 $ // $Copyright: (C) 2003 BRAHMS Collaboration // void makeVarBinSpectra(TString fileName, TString histoName = "spectraSum1D") { //Build for MRS at the moment. But should work for FS as well. //Convert a fixed bin size histogram to a variable bin size histogram //Remove the first non-zero bin and last non-zero bin TDirectory* saveDir = gDirectory; TFile *inFile = TFile::Open(fileName.Data()); TH1F* hOld = 0; if (inFile || inFile->IsOpen()) { TH1F * hTemp = (TH1F*) inFile->Get(histoName.Data()); if(!hTemp){ cout<<"No histogram " << histoName.Data() << " in file " <Clone(); hOld->SetDirectory(0); inFile->Close(); delete inFile; } if (!hOld) { Error("makeVarBinSpectra", "Could not access input histogram hOld"); return; } saveDir->cd(); //Define an output file TString dir = GetDir(fileName); TString inFileName = GetFileName(fileName); Char_t outFileName[256]; sprintf(outFileName, "%s/Niced_%s", dir.Data(), inFileName.Data() ); TFile *outFile = new TFile(outFileName, "RECREATE"); Int_t i, j; //Define a new variable histogram Float_t binEdge[] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.2, 1.4, 1.6, 1.9, 2.2, 2.5, 3.0, 3.5, 4., 5., 6.}; Int_t nBinsNew = sizeof(binEdge)/sizeof(Float_t) - 1; // NB nbins-1 Int_t nBinsOld = hOld->GetXaxis()->GetNbins(); TH1F * hNew = new TH1F("VarBinSpectra", "", nBinsNew, binEdge); hNew->SetTitle(hOld->GetTitle()); hNew->SetStats(kFALSE); hNew->GetXaxis()->SetTitle(hOld->GetXaxis()->GetTitle()); hNew->GetYaxis()->SetTitle(hOld->GetYaxis()->GetTitle()); hNew->GetXaxis()->SetRangeUser(0.0, 6.0); hNew->SetMinimum(1.e-7); hNew->SetMaximum(1000); hNew->SetTitleOffset(1.3, "Y"); hNew->SetTitleSize(0.03, "Y"); hNew->SetLabelSize(0.03, "Y"); hNew->SetLabelOffset(0.002, "Y"); Double_t * err = new Double_t[nBinsNew]; Int_t * binScale = new Int_t[nBinsNew]; for(i = 0; i <= nBinsNew; i++) { err[i] = 0; binScale[i] = 0; } for(i = 0; i <= nBinsOld; i++){ Double_t binCenter = hOld->GetXaxis()->GetBinCenter(i); Double_t errOld = hOld->GetBinError(i)*binCenter; j = hNew->Fill(binCenter, hOld->GetBinContent(i)*binCenter); err[j] += errOld * errOld * binCenter * binCenter; binScale[j] ++; } //loop over the old histogram for(i = 0; i <= nBinsNew; i++) { Double_t newBinCenter = hNew->GetXaxis()->GetBinCenter(i); if(binScale[i] != 0) { hNew->SetBinContent(i, hNew->GetBinContent(i)/binScale[i]/newBinCenter); hNew->SetBinError(i, TMath::Sqrt(err[i])/binScale[i]/newBinCenter); } } //Remove the first non-zero bin for(i = 0; i <= nBinsNew; i++){ if(hNew->GetBinContent(i) != 0.) { hNew->SetBinContent(i, 0.); hNew->SetBinError(i, 0.); break; } } //Remove the last non-zero bin for(i = nBinsNew; i >0 ; i--){ if(hNew->GetBinContent(i) != 0.){ hNew->SetBinContent(i, 0.); hNew->SetBinError(i, 0.); break; } } outFile->cd(); hNew->Write(); delete [] err; delete [] binScale; outFile->Close(); delete outFile; // delete hOld; } TString GetDir(TString base) { Int_t size = base.Sizeof()-2; Int_t slash = 0; for (Int_t idx = size; idx >= 0; idx--) if (base.Data()[idx] == '/') { slash = idx; break; } if (slash) { base.Remove(slash); return base; } return TString("."); } TString GetFileName(TString base){ Int_t size = base.Sizeof()-2; Int_t slash = 0; for (Int_t idx = size; idx >= 0; idx--) if (base.Data()[idx] == '/') { slash = idx; break; } if (slash) base.Remove(0, slash + 1); return base; } //_________________________________________________ // // $Log: makeVarBinSpectra.C,v $ // Revision 1.2 2003/09/23 10:27:21 zbyin // Add logs //