Rebin a histo by using TAxis from another histo

Hi

I am trying to rebin a histogram by using the TAxis from another histo , but my code does not work…

Thanks in advance

alex

[code]
{
TH1F * h_1 = (TH1F*) file->Get(“histo1”);
TH1F * h_2 = (TH1F*) file->Get(“histo2”);

TAxis *axis;
axis = h_1->GetXaxis();
Rebin(h_2,axis)
}

void Rebin(TH1F * &h, TAxis * &axis_){

TH1F *histo;
histo=h;

TH1F *histo_binned;

histo_binned = new TH1F(h->GetName(),h->GetTitle(),axis_->GetNbins(), axis_->GetXbins ()->fArray );
h=histo_binned;

}[/code]

Hi

any change to this gets an answer?

thanks in advance

-a

What is the problem ?
Can you pot something we can run ?

Hi

I am attaching a working example of the code.

First opens the filein.root and makes a custom binning based on the TT TH1 (ReBinTTbar) and then a TAxis is saved. My problem is when I am trying to loop for the rest of the histos and inherit the binning from the previous step.

thanks in advance for your help
Rebin2.C (4.95 KB)
filein.root (15 KB)

With your macro I get this:

Processing Rebin2.C...
 Filling the new histo  1 content 211.067  low edge   -0.5
 Filling the new histo  2 content 8325.05  low edge   -0.4
 Filling the new histo  3 content 1304.25  low edge   -0.3
 Filling the new histo  4 content 273.659  low edge   -0.2
 Filling the new histo  5 content 105.534  low edge   -0.1
 Filling the new histo  6 content 117.907  low edge   0
 Filling the new histo  7 content 64.7761  low edge   0.1
 Filling the new histo  8 content 10.9173  low edge   0.2
 OK, done with the TTJets......
  before  7  7
  now  7
MVA_MLP_T2tt_2j_300_100  7  7  7  0
  now  7
MVA_MLP_T2tt_2j_300_100  7  7  7  0
  now  7
MVA_MLP_T2tt_2j_300_100_Normalized  7  7  7  0
  now  7
MVA_MLP_T2tt_2j_300_100_Normalized  7  7  7  0
  now  7
MVA_MLP_TT_Normalized  7  7  7  0
  now  7
MVA_MLP_WJets  7  7  7  0
  now  7
MVA_MLP_WJets_Normalized  7  7  7  0
  now  7
MVA_MLP_ZJets  7  7  7  0
  now  7
MVA_MLP_ZJets_Normalized  7  7  7  0
  now  7
MVA_MLP_ST_s_Ch_t  7  7  7  0
  now  7
MVA_MLP_ST_s_Ch_t_Normalized  7  7  7  0
  now  7
MVA_MLP_ST_s_Ch_tbar  7  7  7  0
  now  7
MVA_MLP_ST_s_Ch_tbar_Normalized  7  7  7  0
  now  7
MVA_MLP_ST_tW_Ch_t  7  7  7  0
  now  7
MVA_MLP_ST_tW_Ch_t_Normalized  7  7  7  0
  now  7
MVA_MLP_ST_tW_Ch_tbar  7  7  7  0
  now  7
MVA_MLP_ST_tW_Ch_tbar_Normalized  7  7  7  0
  now  7
MVA_MLP_ST_t_Ch_t  7  7  7  0
  now  7
MVA_MLP_ST_t_Ch_t_Normalized  7  7  7  0
  now  7
MVA_MLP_ST_t_Ch_tbar  7  7  7  0
  now  7
MVA_MLP_ST_t_Ch_tbar_Normalized  7  7  7  0
  now  7
MVA_MLP_Data  7  7  7  0
  now  7
MVA_MLP_Data_Normalized  7  7  7  0
root [1] 

what is wrong ?

Hi

did you try to open the created fileout.root ? Only the MVA_MLP_TT has the correct binning, everything else is empty… So, although looks like that the axis is properly written and read out from the 1rst step (ReBinTTBar) the rest of the TH1 plots are not how are supposed to be…In other words, I decide the binning on the TT ,write a TH1 &TAxis out of it, but the last step ie making all rest of TH1 inherit the same binning does not work…

thanks

ok …
I need to cut down your example to understand it …

ok, thanks

Btw, there is not need to really understand the ReBinTTbar, since I am using a custom binning with some kind of minimum events per bin. The real problem is when I am trying to use the TAxis already.

thanks

Alex

Yes but right now your macro is very difficult to read … no indent… lot of code not related to the question …
would have been easy for me to have a show case just showing the issue … as it is not the case I have to clean up and understand what it does.

ok – Let me clean it a bit and repost it here

thanks

Ok thanks .

Ok, here you are – Hopefully this is better now

I am convinced that I am doing something wrong in the

void ReBin(TH1F * &h, TAxis * &axis_,int entries)

part though…

Cheers

Alex
ReBin2.C (6.33 KB)

you new version gives:

root [0] .x ReBin2.C
Error: Function ReBin2() is not defined in current scope  :0:
*** Interpreter error recovered ***
root [1] 

arg…soryy Please cahnge

to

inside the file, around line 50.

thanks

try this one.
ReBin2.C (6.42 KB)

Hi again

So, I modified a bit my code - Now, what it does is this

1- In advance, I have saved in TBranches all the “raw” values for each TH1 object, along with its weight- Now, specifically for my needs and based on the “MVA_MLP_TT” histo, I need to have at least 10 entries per bin , and then it saves a files (TT_axis.root) which contains the template histo and the TAxis out of it. (Class RebinTTbar)

2-Then, it loops in all TH1-objects and uses the TTree info for each object (input is filein.root) and in parallel opens the TT_axis.root - Now, I make a new TH1 which inherits the binning from the TT_axis , and fill the entries from the TTree for each object matching the same naming of course.

So, while making the custom binning of the template based on the TH1 “MVA_MLP_TT” , (step 1) everything looks ok , as I get the desired >=10 / bin

 Filling the new histo  1 content 66.2317  low edge   0
 Filling the new histo  2 content 6018.75  low edge   0.15
 Filling the new histo  3 content 3290.37  low edge   0.3
 Filling the new histo  4 content 525.483  low edge   0.45
 Filling the new histo  5 content 201.606  low edge   0.6
 Filling the new histo  6 content 107.718  low edge   0.75
 Filling the new histo  7 content 87.3386  low edge   0.9
 Filling the new histo  8 content 72.0543  low edge   1.05
 Filling the new histo  9 content 31.2963  low edge   1.2
 Filling the new histo  10 content 10.1895  low edge   1.35

so, each bin with at least 10 entries.

My problem is while trying to fill the TH1 for the objects in the file,(step 2) since even for the very exact TH1 “MVA_MLP_TT” , now looks like it fill different number of entries per bin… Remember, that in step 1, I just open the filein.root, make the binning and save a TAxis ; step2 is to fill a TH1 using the TAxis combined with the TTree “raw” entries for each TH1 object.

So, that means, that even for the very same “MVA_MLP_TT” since step2 uses the TAxis , I should get the very exact binning info as in step1. So, comparing in step2 what it reads from the TAxis

From the histo_binned in ReBinTree class 66.2317 BinLowEdge 0 #Bin 1 BinWidth 0.15 Integral 10411 From the histo_binned in ReBinTree class 6018.75 BinLowEdge 0.15 #Bin 2 BinWidth 0.15 Integral 10411 From the histo_binned in ReBinTree class 3290.37 BinLowEdge 0.3 #Bin 3 BinWidth 0.15 Integral 10411 From the histo_binned in ReBinTree class 525.483 BinLowEdge 0.45 #Bin 4 BinWidth 0.15 Integral 10411 From the histo_binned in ReBinTree class 201.606 BinLowEdge 0.6 #Bin 5 BinWidth 0.15 Integral 10411 From the histo_binned in ReBinTree class 107.718 BinLowEdge 0.75 #Bin 6 BinWidth 0.15 Integral 10411 From the histo_binned in ReBinTree class 87.3386 BinLowEdge 0.9 #Bin 7 BinWidth 0.15 Integral 10411 From the histo_binned in ReBinTree class 72.0543 BinLowEdge 1.05 #Bin 8 BinWidth 0.15 Integral 10411 From the histo_binned in ReBinTree class 31.2963 BinLowEdge 1.2 #Bin 9 BinWidth 0.15 Integral 10411 From the histo_binned in ReBinTree class 10.1895 BinLowEdge 1.35 #Bin 10 BinWidth 0.15 Integral 10411

with what it now gives

  The binned histogram is  MVA_MLP_TT  1  9477.62  0 axis_min 0 axis_max  1.5 Integral  10416.5
  The binned histogram is  MVA_MLP_TT  2  625.198  0.15 axis_min 0 axis_max  1.5 Integral  10416.5
  The binned histogram is  MVA_MLP_TT  3  186.322  0.3 axis_min 0 axis_max  1.5 Integral  10416.5
  The binned histogram is  MVA_MLP_TT  4  107.718  0.45 axis_min 0 axis_max  1.5 Integral  10416.5
  The binned histogram is  MVA_MLP_TT  5  15.2842  0.6 axis_min 0 axis_max  1.5 Integral  10416.5
  The binned histogram is  MVA_MLP_TT  6  3.63911  0.75 axis_min 0 axis_max  1.5 Integral  10416.5
  The binned histogram is  MVA_MLP_TT  7  0  0.9 axis_min 0 axis_max  1.5 Integral  10416.5
  The binned histogram is  MVA_MLP_TT  8  0  1.05 axis_min 0 axis_max  1.5 Integral  10416.5
  The binned histogram is  MVA_MLP_TT  9  0.727821  1.2 axis_min 0 axis_max  1.5 Integral  10416.5
  The binned histogram is  MVA_MLP_TT  10  0  1.35 axis_min 0 axis_max  1.5 Integral  10416.5

something is wrong… The bin width is correct, but the entries per bin are completely wrong and the Integral as well for the very same MVA_MLP_TT TH1…

I attach the code to reproduce all these -

Thanks in advance!

Alex
ReBin2a.C (9.25 KB)
filein.root (180 KB)

Hi,
It is true ou get different values, but how are you sure the values stored in the tree and the stored histogram are the same ? The fact that you get different values means they are not the same. You should check this carefully.
We cannot debug each single line of your complex code

Best Regards

Lorenzo