Issue Reading Information from Root File in

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:

root [0]
Processing inclphoton.cpp…
(inclphoton) @0xa99020

and I don’t get the new root file with histogram
I appreciate your time and expertise in helping me with this matter.

Cheers,

Dear @sandra_idrissi ,

Thanks for posting your question on the forum! By quickly parsing your code, if I understand correctly what you are doing is:

  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

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.

Cheers,
Vincenzo

Dear Vincenzo,

Thank you for your quick answer!

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"

Cheers,
Souad

Dear @sandra_idrissi ,

Perfect, RDataFrame can do all of that!

  1. 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
  2. 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(...);

More information in the docs. Also see the tutorials.

Cheers,
Vincenzo

Thank you Vincenzo :slight_smile: , 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?

Cheers,

Dear @sandra_idrissi ,

With RDataFrame, you can just provide a list of input files as

ROOT::RDataFrame df{
    "Events",
    {"dir1/f1.root", "dir2/f2.root", "dir2/f3.root", "dir3/f4.root"}};

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

Cheers,
Vincenzo

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.