Home | News | Documentation | Download

Using TTree Draw as we're filling?


In my Geant4 application, I have a bunch of my own classes that I store in a root file. I setup a tree and SetBranchAddress to all these local objects. For a lot of our analysis we get away with TTree::Draw and conventionally this is done when looping over the file by a separate program / macro on a finished file.

I’d really like to fill all my classes as usual for an event, but then also use TTree::Draw on just this event to fill a histogram. I would then append this histogram to the event. I want to do this as it means I retain the flexibility to use TTree::Draw for whatever variables we want (could be a single number or a vector, etc) and use cuts for per-event histograms that I store.

Is this at all possible?

I could, write to the file, then after all events are simulated loop over them, producing the histograms then put them into a copy of that event in a new file. But this is cumbersome and involves 2x the data volume temporarily for the same of adding a small histogram (sub % contribution to data).

Any advice or possible options would be greatly appreciated.

Many thanks,

Hi Laurie,
TTree::Draw internally performs a loop over all events in a TTree. As you want one histogram per event (do I understand correctly?) you have two options that I can think of:

  • after writing all data to a file, read it back and produce the histograms, saving only the histograms to a new TTree that will have the same number of entries as the original TTree. You can use it as a friend TTree of the first if you need to manipulate both data and histograms together (friend TTrees are basically TTrees “pasted together” horizontally)
  • while producing the data, for every event, you can fill a TTree with the data from that event and call TTree::Draw on it, without ever writing that TTree to file

Out of curiosity, what’s the physics usecase for creating one histogram per event?

Hello Enrico,

Thanks for the suggestions - they’re quite useful. I hadn’t considered TTree friends - I’ll look into this to see how it’ll work for us.

Can you have an in memory only TTree, or a ‘virtual’ file with a tree? If so, is it allowed link more than one TTree to the same local class objects (e.g. my event class) for writing?

With TTree::Draw you can choose the start and end index in the tree for the loop (nentries & firstentry), so I had envisioned drawing just one event.

Physics case:
I look at energy deposition throughout an accelerator and magnets and detector backgrounds. For a given event we make an energy deposition histogram (anywhere from 10 to 250k bins!) following the axis of an accelerator. The model is done in Geant4 and for a given event we can have say 10^4 to 10^7 energy deposition hits that are filled into the histogram, so this is a massive reduction. We also typically need to simulate 10^4 to 10^7 events for the result to converge.

Later, we want to investigate the origin of losses and perform some analysis - e.g. what’s the energy deposition caused by this bit of initial phase space in the beam. So, we can simply combine the histograms while performing a cut. We don’t use a “profile” so we can perform cuts, the variance and errors are calculated correctly and are also calculated in a numerically stable way. We also need to combine means from multiple runs with the correct variance.

We may also do cuts based on the integral of the histograms etc. Storing all the original energy deposition hits is impractical in data volume and highly inefficient to histogram each time we do an analysis.

One upcoming example is using a TH2Poly with say just a few bins in a calculated phase space set of variables that makes it very easy to classify events for later analysis.

We don’t have one model - our same application (BDSIM) is used to model many accelerators and beam lines so the specific analysis really varies from user to user, which is why I don’t want to hard code any loops etc for any specific variables. I want to be able to flexibly define a histogram (e.g. line for TTree::Draw) that I can make at simulation time and fill into the events to reduce data and reduce the weight of later analysis.

For a lot of our simulations we usually run close to memory limits (avg GB / cpu) so I’m trying to avoid having anything accumulate in memory per event.


Hi Laurie,
Wouldn’t just storing (in the TTree) the histogram per event / TTree entry be enough?
Cheers, Axel.

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