Hi everyone. I am stuck in a problem. The problem is as follows:
I have a root file which have a single tree and a single branch. The branch contains the information on the event no’s generated from a MC sample. As there are multiple candidates in a single event, these candidates take the same event no. What I want to do is assign the candidates with the same event no by a user defined number. An example of this would be like,
eventno user _defined_no
For this purpose, I have wriiten a macro which is shown below,
TFile *f1 = new TFile("sample1.root");
TTree* t1 = (TTree*)f1-> Get("h1");
float eventno = 0;
float count = 0;
float flag = -1;
int nentries = t1->GetEntries();
for(int i = 1; i < 10; i = i+1)
flag = eventno;
count = 1;
count = -10;
std::cout<<" eventno : "<<eventno<<" count : "<<count<<std::endl;
The following is the output for 10 entries,
eventno : 7 count : 1
eventno : 8 count : 1
eventno : 12 count : 1
eventno : 14 count : 1
eventno : 17 count : 1
eventno : 17 count : -10
eventno : 24 count : 1
eventno : 31 count : 1
eventno : 39 count : 1
The problem with the code is that at event no: 17 the user defined no which is expected to be -10 appears to be 1. The intention is to print -10 for both the events 17 as they are multiple candidates belonging to the same event. May someone help me in solving this issue?
Thanking you in advance.
Attachments : A root file containing the events.
Jyotirmoisample1.root (11.2 KB)
This is not really a ROOT issue, but one problem is that you are using the same cout whether the ‘current entry’ is repeated or not, i.e. your cout is reached independently of your if/else options. You need to print only when the “current eventno” is not equal to the previous one/s: you should only print the ‘previous’ (non-equal to the current one) eventno (with a count either 0 or -10), after you have checked that the ‘current’ eventno is different.
As for ROOT, note that the first entry is number 0, and the last one is GetEntries() - 1; thus your output is missing the first entry, with eventno = 3.
Thank you @dastudillo for your comments. I would like to know if there is any way to access the subsequent entry of a branch using GetEntry(i) i.e., GetEntry(i+1) in the same loop for (int i = 0; i < nentries; i=i+1). I tried to access the next element using GetEntry(i+1) in a for loop but did not get the desired result. I think that might solve the problem if I put that in the if condition. And of course, proper positioning of the cout statements will have to be done.
Yes, you can access any entry at any point, just keep in mind that getting an entry updates the values associated with SetBranchAddress. Also note that you cannot do GetEntry(nentries), it only goes to nenetries-1.
Hi @dastudillo, would you mind reminding me the command which will help to access any entry at any point in a branch. I tried using the GetVal(), but it does not seem to work for me. What I want to access is (i+1)th entry in the following for loop:
for (int i = 0; i< entries, i++)
ith_value = some value from eventno branch;
(i+1) value = next value from eventno branch;
Thanking you in advance.