How to draw histogram from a leaf variable

I have a root files with a tree name “track” and with a branch “indexmc”. Inside the branch there are some leaf variables (pdgid, motheridx). I am getting crash every time I try to draw them with the following macro. Could you please have a look into it and suggest me a solutions.
https://drive.google.com/drive/folders/1qmHjrNP3kzS4hDwODkfIRjnfW7u3PkwM?usp=sharing
Thanks


Please read tips for efficient and successful posting and posting code

Please fill also the fields below. Note that root -b -q will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug from the ROOT prompt to pre-populate a topic.

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


Firstly, the root file name in your macro is not the one you shared. Anyway, if I correct it (to 3097_anasig_mc_002.root), I don’t get a crash, the macro finishes and I get:

If you look at the tree:

root [1] track->Print()
******************************************************************************
*Tree    :track     :                                                        *
*Entries :    11504 : Total =         9786239 bytes  File  Size =    5700944 *
*        :          : Tree compression factor =   1.71                       *
******************************************************************************
*Br    0 :indexmc   : indexmc/I                                              *
*Entries :    11504 : Total  Size=      46659 bytes  File Size  =       8926 *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=   5.17     *
*............................................................................*
*Br    1 :pdgid     : pdgid[indexmc]/I                                       *
*Entries :    11504 : Total  Size=     364898 bytes  File Size  =     107722 *
*Baskets :       13 : Basket Size=      32000 bytes  Compression=   3.38     *
*............................................................................*
*Br    2 :motheridx : motheridx[indexmc]/I                                   *
*Entries :    11504 : Total  Size=     364966 bytes  File Size  =      73308 *
*Baskets :       13 : Basket Size=      32000 bytes  Compression=   4.97     *
*............................................................................*
etc.

pdgid, motheridx, etc are not “leaf variables inside the indexmc branch”; all of them are leaves (you can also see this on a TBrowser) and in fact pdgid and motheridx are vectors/arrays with size, at any given event, given by the value of indexmc at that event.
You can search this forum for posts about ways to read arrays or vectors from trees, or check out several ideas here:

I am still getting crash ( *** Break *** segmentation violation), maybe because of the root version. I am using root 6.20 .

This seems to work now with the attached macro.

Thanks
main.C (1.2 KB)

  for (Long64_t i = 0; i < nEntries; ++i) {
    if (tree->GetEntry(i) <= 0) {
      std::cerr << "Error reading entry " << i << std::endl;
      continue;
    }
  }

  // Fill the histogram
  for (int j = 0; j < 12984; ++j) {
    hist->Fill(pdgid[j]);
  }

This is wrong. Only pdgid[0] will be filled (with the last entry).
You might have meant:

  for (Long64_t i = 0; i < nEntries; ++i) {
    if (tree->GetEntry(i) <= 0) {
      std::cerr << "Error reading entry " << i << std::endl;
      continue;
    }
    hist->Fill(pdgid[0]);
  }

However, you might actually be better serve to learn how to use RDataFrame: ROOT: RDataFrame analysis tutorials

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