elio#include "TH1.h" #include "TH2F.h" #include "TFile.h" #include "TTree.h" #include "TCanvas.h" #include "TSystem.h" #include "TF1.h" #include "TKey.h" #include "TH1F.h" #include "TStyle.h" #include "TProfile.h" #include "TLegend.h" #include "TLine.h" #include "TArrow.h" #include "TLatex.h" #include "TGraph.h" #include "TGraphErrors.h" #include "TFormula.h" #include "TAxis.h" #include "TRandom3.h" #include "TMath.h" #include "THStack.h" #include #include #include #include #include #include #include void ReBin(TH1F * &h, TAxis * &ax,int entries_); void ReBinTTbar(TH1F * &h , TTree * &t); void ReBinTree(TH1F * &h, TTree * &t ); void ReBin2a() { using namespace std; gStyle->SetOptStat(0); TString fileName = "filein.root"; TFile *target = new TFile("fileout_Bin.root","recreate" ); TFile *file = TFile::Open(fileName); if (!file) abort(); //// Find in the file the TH1 = MVA_MLP_TT , make the custom binning and save a TAxis TH1F * ht = (TH1F*) file->Get("MVA_MLP_TT"); int entries = ht->GetEntries(); char treename[100]; sprintf(treename,"Tree_%s",ht->GetName()); TTree *trT = (TTree*)file->Get(treename); /////////make the binning cout<<" before rebinning"<GetNbinsX()<cd(); ht->Write(); target2->cd(); ht->Write(); target2->Write(); target2->Close(); delete target2; TH1F *ht_norm; ht_norm=ht; target->cd(); ht->Write(); //Now, you should have a file TT_axis.root with the TAxis and the TH1 of the Binned template coming from the the MVA_MLP_TT TFile *first_source = TFile::Open(fileName); TString path ((char *) strstr (target->GetPath (), ":")); path.Remove (0, 2); ///////Loop in the source for the rest of TH1 first_source->cd (path); TDirectory *current_sourcedir = gDirectory; //gain time, do not add the objects in the list in memory Bool_t status = TH1::AddDirectoryStatus (); TH1::AddDirectory (kFALSE); ///////// loop over all keys in this directory TChain *globChain = 0; TIter nextkey (current_sourcedir->GetListOfKeys ()); TKey *key, *oldkey = 0; while ((key = (TKey *) nextkey ())) { //keep only the highest cycle number for each key if (oldkey && !strcmp (oldkey->GetName (), key->GetName ())) continue; first_source->cd (path); TObject *obj = key->ReadObj (); TLegend *legend_c1 = new TLegend (0.35, 0.90, 0.80, 0.70); if (obj->IsA ()->InheritsFrom ("TH1F")) { TH1F *h1 = (TH1F *) obj; string name=h1->GetName(); /////////If TH1 is not MVA_MLP_TT rebin it by using the TAxis obtained from the previous step file->cd(); char treename[100]; sprintf(treename,"Tree_%s",h1->GetName()); string str2 ("Normalized"); if (name.find(str2) == string::npos) { //if( name.find("Normalized")==0){ TTree *t = (TTree*)file->Get(treename); cout<< " READY FOR THE REBIN "<cd(); h1->Write(); h1->Scale(1/h1->Integral()); char hnormname[100]; sprintf(hnormname,"%s_Normalized",h1->GetName()); h1->SetName(hnormname); h1->SetTitle(hnormname); h1->Write(); } } } target->cd(); target->Write(); target->Close(); } //////////////// This will make a binning of the TT_MVA Classification- Default range is -05 to 1.5 . ///We will first merge all bins below 0 and then scan the bins where x>0 - If one bin // is found to have less than 10 entries all bins from this to the end will be merged to one bin. void ReBinTTbar(TH1F * &h,TTree * &trTT){ float binContent[100]; binContent{100}=0; TH1F *histo = new TH1F(h->GetName(),h->GetTitle(),h->GetNbinsX(),-0.5,1.5); vector *values; Double_t weight; vector values_;values_.clear(); TBranch *bEvaluation = 0; TBranch *bWeight = 0; trTT->SetBranchAddress("Evaluation",&values,&bEvaluation); trTT->SetBranchAddress("Weight",&weight,&bWeight); Long64_t tentry = trTT->LoadTree(0); bEvaluation->GetEntry(tentry); bWeight->GetEntry(tentry); for (UInt_t j = 0; j < values->size(); ++j) { values_.push_back(values->at(j)); } sort (values_.begin (), values_.end ()); for (UInt_t kk = 0; kk < values_.size(); kk++) { histo->Fill(values_[kk],weight); // cout<GetName()<< " "<GetNbinsX());nb++){ ///////First find the first bin above x>0 which has more than 10 entries - merge_up_to_this_bin refers to this exact bin float small_bins=0; if ( h->GetBinContent(nb)>10 && h->GetBinContent(nb-1)<10 && h->GetBinLowEdge(nb-1)<0){ merge_up_to_this_bin=nb-1; } //////// the last_big_bin counts for the last bin with more than 10 entries if (h->GetBinContent(nb)<10 && h->GetBinContent(nb-1)>10 && h->GetBinLowEdge(nb)>0){ last_big_bin=nb-1; break; } } ///////merge the bin contents of the x<0 area for (int k=0;kGetBinContent(k); } ////////assign the new bin content for (unsigned int n_big = merge_up_to_this_bin+1;n_bigGetBinContent(n_big)); } binContent[1]+=small_bins; for (unsigned int n_small = last_big_bin+1;n_smallGetNbinsX());n_small++){ binContent[count_new_bins+1]+= float(h->GetBinContent(n_small)); } ///now make a new TH1 which will hold the new "binned" distribution TH1F *histo_binned; //histo_binned = new TH1F("","",count_new_bins-1,0,1.5); histo_binned = new TH1F("","",count_new_bins,0,1.5); /////get the same name,title from the input TH1 histo_binned->SetName(h->GetName()); histo_binned->SetTitle(h->GetTitle()); ////now, make the new binning and return the new histogram histo_binned->SetBinContent(merge_up_to_this_bin+1,small_bins); for (unsigned int nbb=1;nbbSetBinContent(nbb,binContent[nbb]); cout<<" Filling the new histo "<GetBinLowEdge(nbb)<Sumw2(); h=histo_binned; TFile *target2 = new TFile("test.root","recreate" ); target2->cd(); h->Write(); target2->Write(); target2->Close(); delete target2; // for (unsigned int nbb=1;nbbGetNbinsX()+1;nbb++){ //cout<<" From the histo_binned "<GetBinContent(nbb)<<" "<GetBinLowEdge(nbb)<GetXaxis(); taxisFile->cd (); axiss = histo_binned->GetXaxis(); axiss->Write(); taxisFile->Write (); //histo_binned->Write(); histo_binned->Write(); taxisFile->Close (); cout<<" ============================================================== end of RebinTTbar ============================================="<cd(); TH1F * httt = (TH1F*) taxisF->Get("MVA_MLP_TT"); TAxis *axis; taxisF->cd(); axis = httt->GetXaxis(); for (unsigned int nbbs=1;nbbsGetNbinsX()+1;nbbs++){ cout<<" From the histo_binned in ReBinTree class "<GetBinContent(nbbs)<<" BinLowEdge "<GetBinLowEdge(nbbs)<<" #Bin "<GetBinWidth(nbbs)<<" Integral "<Integral())<GetName(),h->GetTitle(),axis->GetNbins(), axis->GetXmin(), axis->GetXmax() ); vector *values; Double_t weight=0; vector values_;values_.clear(); TBranch *bEvaluation = 0; TBranch *bWeight = 0; tr->SetBranchAddress("Evaluation",&values,&bEvaluation); tr->SetBranchAddress("Weight",&weight,&bWeight); Long64_t tentry = tr->LoadTree(0); bEvaluation->GetEntry(tentry); bWeight->GetEntry(tentry); for (UInt_t j = 0; j < values->size(); ++j) { values_.push_back(values->at(j)); } //cout<<" after making the histo "<GetName()<<" bin0 "<GetBinLowEdge(0)<<" bin1 "<GetBinLowEdge(1)<Fill(values_[k],weight); //cout<GetName()<< " "<GetBinContent(0); float bin_one=h->GetBinContent(1); h->SetBinContent(1,bin_zero+bin_one); h->SetBinContent(0,0); for (unsigned int bn=1;bnGetNbinsX()+1);bn++){ cout<<" The binned histogram is "<GetName()<<" "<GetBinContent(bn)<<" "<GetBinLowEdge(bn)<<" axis_min "<GetXmin()<<" axis_max "<GetXmax()<<" Integral "<Integral())<