Dear Expert,
I have a specific problem related to reading information from my input root file and I was hoping you could provide some guidance. I’ve attached a snippet of my code below: inclphoton.h (24.0 KB) inclphoton.cpp (1.2 KB)
:
the result I have obtained when I run the code is:
Thanks for posting your question on the forum! By quickly parsing your code, if I understand correctly what you are doing is:
Opening a file called MyxAODAnalysis.root with a tree called analysis
Creating an histogram
Filling the histogram with the values of the branch ttdata_presel_photonPt from that tree
If this is all you need to do, I would suggest you use RDataFrame:
ROOT::RDataFrame df{"analysis", "MyxAODAnalysis.root"};
auto h = df.Histo1D(
{"PhotonpT", "Photon_{p_{T}} (GeV)", 50, 0.0, 500.0},
"ttdata_presel_photonPt");
// This is your TH1D now, you can do whatever with it
const auto &h_val = h.GetValue();
// For example writing it to file
TFile f_out{"inclphoton_Plots.root","RECREATE"};
f_out.WriteObject(&h_val, h_val.Getname());
For more operations and features of RDataFrame, see the linked docs above. For instance, if you need a chain rather than just one file, you can just write all the file names as a vector instead of the second argument to the constructor.
Yes I would like to do as you mention below (*) but before to fill the histogram I want to apply some condition for " ttdata_presel_photonPt" and add other Branches which are a vector float type ( vector * …) and then I need to loop for this Branch.
" 1. Opening a file called MyxAODAnalysis.root with a tree called analysis
2. Creating an histogram
3. Filling the histogram with the values of the branch ttdata_presel_photonPt from that tree"
You don’t need to “add other branches” as with SetBranchAddress, if you want to use branch mybranch in the tree, just use it in the functions you call on the RDataFrame object
The conditions can be applied before the call to Histo1D with the Filter operation, something that could look like this
auto df_1 = df.Filter([](const std::vector<float> &pts) {
// run some code to apply the condition here
return pts[0] > 30;
}, {"ttdata_presel_photonPt"});
// use the df_1 variable to get fill the histogram with
// the values that pass the filter
auto h = df_1.Histo1D(...);
Thank you Vincenzo , I will check and read the docs.
I have another question regarding the code. Currently, I am working with just one input file in a directory. However, I need to modify the code to handle multiple directories, each containing several input files.
Could you please provide me a guidance for What changes should I make to the code to process several input files from different directories?
Or you could also write down all your dataset specification in a separate json file and then use that as the input information for the dataframe, this is explained here