// general #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); void ReBin(TH1F * &h, TAxis * &ax); void ReBin(TH1F * &h, TAxis * &ax,int entries_); void ReBinTTbar(TH1F * &h); void rebin(TH1F * &h); void Rebin2() { using namespace std; //CLASS , CLASS // setTDRStyle(); gStyle->SetOptStat(0); TString fileName = "filein.root"; TFile *target = new TFile("fileout_Bin.root","recreate" ); TFile *file = TFile::Open(fileName); if (!file) abort(); TFile *first_source = TFile::Open(fileName); TString path ((char *) strstr (target->GetPath (), ":")); path.Remove (0, 2); TAxis *axis ; TH1F * ht = (TH1F*) file->Get("MVA_MLP_TT"); int entries = ht->GetEntries(); ReBinTTbar(ht); cout<<" OK, done with the TTJets......"<GetXaxis(); taxisFile->cd (); axis->Write(); taxisFile->Write (); ht->Write(); cout<<" before "<GetNbins()<<" "<GetNbinsX()<Close (); target->cd(); ht->Write(); 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 (name!="MVA_MLP_TT"){ ReBin(h1,axis,entries); target->cd(); h1->Write(); } } } target->Write(); target->Close(); } void ReBinTTbar(TH1F * &h){ float binContent[100]; binContent{100}=0; TH1F *histo; histo=h; int count_new_bins=0; int last_big_bin=0; int merge_up_to_this_bin=0; for (int nbbs=0;nbbsGetNbinsX());nbbs++){ } for (unsigned int nb = 0;nbGetNbinsX());nb++){ //if more than 10 entries, is just fine 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; } if (h->GetBinContent(nb)<10 && h->GetBinLowEdge(nb)>0){ last_big_bin=nb-1; break; } } for (int k=0;kGetBinContent(k); } 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)); } TH1F *histo_binned; histo_binned = new TH1F("","",count_new_bins-1,0,2); histo_binned->SetName(h->GetName()); histo_binned->SetTitle(h->GetTitle()); //merge the 1-rst 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; } void ReBin(TH1F * &h, TAxis * &axis_,int entries){ TH1F *histo; histo=h; cout<<" now "<GetNbins()<GetName(),h->GetTitle(),axis_->GetNbins(), axis_->GetXbins ()->fArray ); vector values; values.clear(); Double_t *xbins = new Double_t[entries+1]; Int_t k=0; for (Int_t i=1;i<=axis_->GetNbins();i++) { Int_t y = (Int_t)h->GetBinContent(i); if (y <=0) continue; Double_t dx = axis_->GetBinWidth(i)/y; Double_t xmin = axis_->GetBinLowEdge(i); for (Int_t j=0;jFill(values[k]); } h=histo_binned; cout<GetName()<<" "<GetNbinsX()<<" "<GetNbinsX()<<" "<GetNbins()<<" "<GetXbins ()->fArray<FillRandom("gaus",10*nentries); // hnew->Draw(); }