Storing data event by event in ROOT Tree

ROOT Version: 6.18/00
Platform: Ubuntu 20.04
Compiler: Not Provided


Hi root experts,

I would like to ask a questions on reading .dat file and storing them in tree event by event.
I attached the text file here
data.txt (13.6 KB) .

The columns are as follows; pdg Energy Px Py Px Enu;
I want to store this data as event by event in the tree in such a way that the first event starts with pdg = 13 and ends before the next pdg = 13. This is will be the first event .

And for the second event to start again from the next pdg = 13 and so on.

Can anyone please help me?

I tried below the code but It doesn’t gives me what i want.

#include<iostream>
#include<TTree.h>
#include<TFile.h>
#include<TCanvas.h>
#include<TLegend.h>

void tree()
{ 
  TFile* file = new TFile("data.root","RECREATE");
  TTree* t    = new TTree("event","data");
  Long64_t nlines = t->ReadFile("data.txt","pdg/I:E/F:px/F:py/F:pz/F:Ev/F");
  
  t->Write();
  file->Close(); 
  
}  

What do you do with the lines with pdg != 13?

If all the entries (lines) from one pdg=13 to the next will belong to one same “event”, you can have an additional column (branch in the tree) for “event number”. You can read the file as you did but after that you have to add one extra column (branch), then loop over the tree and fill the new branch with an “event number” starting from 1 at the first entry, increasing it every time you find a new pdg=13.

1 Like

Hi,

Thanks alot.

I have tried but was unable to do exactly as you have suggested.

I just counted the number of pdg=13 (as that will give me the number of events I want since only one pdg =13 is obeserved in each event ) but not exactly correct.

{
  TFile *f = TFile::Open("data.root", "RECREATE");
  TTree *t = new TTree("event", "data");
  t->ReadFile("data.txt", "pdg/I:E/F:px/F:py/F:pz/F:Ev/F");
  
  // Create a new branch with the event "id"
  Long64_t id = 0; // -1 or 0 (whatever one likes more)
  TBranch *id_p = t->Branch("id", &id);
  
  // fill the new branch with the event "id"
  Int_t pdg;
  TBranch *pdg_p;
  t->SetBranchAddress("pdg", &pdg, &pdg_p);
  for (Long64_t i = 0; i < pdg_p->GetEntries(); i++) {
    pdg_p->GetEntry(i);
    if (pdg == 13) id++; // increment the event "id"
    id_p->Fill();
  }
  
  t->ResetBranchAddresses(); // disconnect from local variables
  
  t->Write();
  delete f; // automatically deletes "t", too
}
1 Like

Hi. Thank you very much.

I do really appreciate for your help.

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