SetBranchAddress issue

Hello everyone ! I’m French so if I say something wrong or unclear, I apologize…

I’m working on a ROOT file that present a TTree called “output” and about 20 branches.
I apologize but I don’t know if I’m allowed to provide my ROOT.file so If you need some further information, just ask me :slight_smile: !
I am actually writing a Macro file called “PlotEnergy.C” which will plot the energy deposed in the detector I’m working on. So if I open ROOT and my branch “Edep” which contains the informations, I find what I expect, everything is working for the moment.

But this calculus (the graph of the energy deposit) has been slice in nearly 5700 steps of slight deposit.
My goal is to get the value of all the deposit for each step and to sum them in order to find the value I’m looking for.

Here is my problem, it’s been 2 days at least I’m trying to get those data contains in my Histogram but I have failed …

Another information, I have 10 event so I trying to calculate the energy for each event.

Here is my code :

void PlotEnergy(string Filename){

TFile* f = new TFile(Filename,"-b","MonFichierRoot",0);
TTree* output = (TTree*)f->Get("output");
double NbEvt = output->GetEntries();
 double TotEdep = 0;

for(Int_t i=0;i<NbEvent;i++){
 TotEdep = 0;
 output->GetEntry(i);
max = GetNumStep(i,output);
double Edep [max];
output->SetBranchAddress("Edep",&Edep);
 output->GetEntry(i);
for(Int_t j=0; j<max; j++){
  TotEdep = TotEdep + Edep[j];                                                                                                                     
   if(j%1000 == 0){cout<<"tot edep inside loop = "<<TotEdep<<endl;}
 }
 cout << " TotEdep = " << TotEdep << endl;
 H1->Fill(TotEdep);
 H1->Draw();                                                                           
 }
}

The fonctions GetNumStep just return the number of step inside the histogram.
I don’t have any error at the compilation nor at the execution, but my variable just doesn’t contain the data I’m looking for so my result is something like 10e-300 which is totally wrong.
I don’t understand because normally SetBranchAddress should put my data inside my variable Edep…
I’m desperate so any clue would be greatly appreciate !

Thank you, Sincerely,
Just a student in internship who is about to take an appointment with the psychologist…

__
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,

I am a bit lost, but if you want to fill a histogram with the sum of the elements of the array stored in the Edep branch event by event, the solution could be as simple as:

ROOT::RDataFrame df("output", "TonFichierRoot");
auto h = df.Define("EdepSum", "Sum(Edep)").Histo1D("EdepSum");
h->Draw();

By the way, what ROOT version are you using?

Cheers,
D

Hi !

I think I am using ROOT version 6.08, here is a message I get when I open it :

 Welcome to ROOT 6.08/06                http://root.cern.ch |
  |                               (c) 1995-2016, The ROOT Team |
  | Built for linuxx8664gcc                                    |
  | From tag v6-08-06, 2 March 2017                          

I tried your way, but I needed to include

#include<TRInterface.h>

right ? Because I tried and my terminal failed at the compilation saying me that it can’t find this file.
Sorry if I’m unclear, I’ll try to re-explain in a better way:

TTree - > Edep
-> Ohter Branches

If I do : output->Draw(“Edep”,“EventNum==1”)
I see the energy deposed in my detector for each step of the trajectory of my particle inside, considering the first particle emitted.
In order to confirm this particle has been emitted with an energy of 4.69Mev I want to sum all the energy deposed at each step which should give me around 4.69 Mev. My problem is getting the data for each step contains in my histogram


The data is the little black dot on this screen. My supervisor told me that if I use the SetBranchAddress then normally my array should contains all this data. So I do, I don’t have any error but my array just do not contain the data instead it contains values like 10e-300 (aka prefilled variable)

If I get the address of my variable Edep and of the Branch Edep I get :

 cout<<" Addresse de Edep = " << &Edep << endl;
  cout<<" Adresse de la BEdep = " << output->GetListOfBranches()->FindObject("Edep")<<endl;
  output->SetBranchAddress("Edep",&Edep);

  cout<< " Adresse de BEdep apres SetBranchAddress = "<<output->GetListOfBranches()->FindObject("\
Edep")<<endl;
  cout<< " Adresse de Edep apres le setbranche = "<<&Edep <<endl;

---------------------------------------------------------------------------------------------------------------------------
Addresse de Edep = 0x7ffefecab030
 Adresse de la BEdep = 0x2dfd900
 Adresse de BEdep apres SetBranchAddress = 0x2dfd900
 Adresse de Edep apres le setbranche = 0x7ffefecab030

Which is not normal in my opinion, after the SetBranchAddress the two address should be the same …
I hope it’s clearer now,

Cheers, Loic

Try to print:

output->GetListOfBranches()->FindObject("Edep")
((TBranch*)(output->GetListOfBranches()->FindObject("Edep")))->GetAddress()
output->GetBranch("Edep")
output->GetBranch("Edep")->GetAddress()

Your code is incorrect. The main problem is the array Edep and the two GetEntry calls.

In the first iteration (i=0), the first GetEntry is ok. Then you create a local array, set the branch address and call GetEntry again (inefficient, but ok). At the closing } of the i loop, the variable Edep goes out of scope, yet the branch address still points to the now invalid address (you didn’t run ResetBranchAddress). The next call to GetEntry with (i=1) causes undefined behaviour! Boom!

To solve the problem, define the Edep array outside (i.e. before) the i loop (it needs to be large enough for your largest event) and set the branch address once, also before the loop. Call GetEntry only once in the loop. The variable max (shouldn’t it better be called edepCount?) works as before in the j loop.

Pro tip: to get the sum, you can use an algorithm:

auto totalEdep = std::accumulate(std::begin(Edep), std::begin(Edep) + max, 0.0);

Note: the scope of the totalEdep variable can be reduced, please only define it inside the loop, not outside of the loop (exactly the opposite of the Edep array).

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