i have few MC root files, like TT.root, ST.root, DY.root and each file a TTree having some branches like, lep_pt, eta, phi etc. also this TTree has genWeight and XS stored for respective sample. i have also another file which represents Data, Data.root. the structure of MC and data file is same like TTree and same branches.
now i want to apply weight to for example lep_pt of each MC file, and add all weighted lep_pt, and then subtract this added_weighted_lep_pt from lep_pt in Data file.
Next i want to store this resulted lep_pt (Data_lep_pt - MC_add_weighted_lep_pt) to a new ttree in a new root file having same structure as the othe MC, Data files since i will need this new root file in my later analysis along with MC and data files.
adding this, i can do the above operations in terms of histograms, like define one histogram for each MC, apply weight, add all MC and the subtract from Data and write the resulted histogram to a root file. but i want to have result in a ttree, to have same structure with other files.
your issue is not crystal-clear to me.
As a general fact, you can easily run your analysis with RDataFrame. Histo1D method returns a (smart) pointer to TH1D (a RResultPtr<TH1D> to be more precise). You can do anything you would do with a pointer to TH1D with it, including calling histo_result->Write() to save the histogram in a file. You may also want to have a look at this tutorial for creating a Tree with a few branches of type histogram.
actually i want to write this hpt to a ttree in the output file, to have same structure like as input file. is it possible? now hpt is the weighted_lep_pt
If I understand correctly, you don’t want to save the histogram itself, rather you want to save your weighted data to a new ROOT file.
In this case, you can use RDataFrame to create a new column with your weighted data with a Define action, and then use Snapshot to save your new data to a file.
Your code will look similar to the following snippet.
df.Define("weighted_lep_pt", "let_pt * XS * {Lumi} / {sum_gen_weight}".format(Lumi, sum_gen_weight)); #here Lumi and sum_gen_weight have been defined above
df.Snapshot("outputTree", "outputFile.root", {"weighted_lep_pt", "eta","phi"}); #list branches you want to be saved