Change the order of branches in a root file

Hello to all!

I have a root file and I want to change the order of the branches an d remove some of them. How to I do this?

Thank you a lot in advance!
Kind regards


Please read tips for efficient and successful posting and posting code

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


Welcome to the ROOT Forum! Iā€™m not sure I understand what youā€™re asking for, but maybe @pcanal has an idea?

1 Like

Hello!
Thank you a lot for your response!

Basically I have what appears in the screenshot attached and I want to change the order. I.e the E to be in second instead of nfsp. And are leafs not branches sorry for the confusion :sweat_smile:
And if this is too messy I have the option also to create a restructured csv file instead, which I also have no idea how to do it.

Thank you a lot in advance again!
root

Well, AFAIK, the leaves are not ā€œorderedā€. I think they keep the order of when they have been created. What is the output of tree->Print() in the ROOT prompt?

Itā€™s the same order.

OK, so the only way is to re-create the tree with branches in the the order you want. Maybe @pcanal knows an easy way to to thatā€¦

And BTW, that would be interesting to know why you want to re-order themā€¦

Thank you a lot, hope @pcanal will help.
I want to run a simulation and the tool I want to use only uses the info with specific order. I.e first the event then the energy, then the position etc

Thank you

But then canā€™t you change the order in the analysis code (reading the tree)? One can access the tree data in a random order. I.e. if you ask first energy and then position is the same than first position and then energy. The order doesnā€™t matter

Hmm I do not think I can do that. The tool reads the tree in a specific order and the only input I can give is the name of the branchesā€¦

Please read again my previous comment, it has been edited

The tool reads the tree in a specific order

This is quite an unusual of a limitation when using TTree unless we are misunderstanding what you mean.

Can you be more specific on:

  • What is the tool that is behaving like this.
  • How the tool is connecting to the TTree
  • How the author/maintainer of the tool reacted when your presented your issue to them.

Cheers,
Philippe.

1 Like

Yes I read it. Thank you for the answer.
ā€œand that the order of the names has to reflect the order of the branches as listed above to allow for correct assignmentā€ this is in the documentation of the framework I want to import the root file, and the branches above are in different order from my root file

I want to use the deposition reader from allpix. And in the root tree is written:

" ROOT Trees

Data in ROOT trees are interpreted as follows. The tree with name tree_name is opened from the provided ROOT file, and information of energy deposits is read from its individual branches. By default the expected branch names and types are:

  • event (integer): Branch for the event number.
  • energy (double): Branch for the energy deposition information.
  • time (double): Branch for the time information when the energy deposition took place, calculated from the start of the event.
  • position.x (double): Leaf of the branch for the x position of the energy deposit in global coordinates of the setup.
  • position.y (double): Leaf of the branch for the y position of the energy deposit in global coordinates of the setup.
  • position.z (double): Leaf of the branch for the z position of the energy deposit in global coordinates of the setup.
  • detector (char array): Branch for the detector or volume name in which the energy was deposited.
  • pdg_code (integer): Branch for the PDG code particle id if the Monte Carlo particle producing this energy deposition.
  • track_id (integer): Branch for the track id of the current Monte Carlo particle.
  • parent_id (integer): Branch for the id of the parent Monte Carlo particle which created the current one.

Entries are read from all branches synchronously and accumulated in the same event until the event id read from the event branch changes.

By default, the event numbers need to be sorted with ascending order. This can be disabled by setting require_sequential_events to false. This is useful when running simulations in mutli-threading mode and merging datasets in the end. Currently only supported in ROOT files.

If the parameters assign_timestamps or create_mcparticles are set to false, no attempt is made in reading the respective branches, independently whether they are present or not.

Different branch names can be configured using the branch_names parameter. It should be noted that new names have to be provided for all branches, i.e. ten names, and that the order of the names has to reflect the order of the branches as listed above to allow for correct assignment. If assign_timestamps or create_mcparticles are set to false, their branch names (time and track_id, parent_id, respectively) should be omitted from the branch name list. Individual leafs of branches can be assigned using the dot notation, e.g. energy.Edep to access a leaf of the branch energy to retrieve the energy deposit information."

I guessed it was easier to change the root file rather than to change the source code of the program.

Also the root file was created by default from the program GENIE and it was given to me I cannot produce it differently even if I wanted

I think you need to ask Allpix and/or GENIE people.
They may know how to read GENIE files in Allpix directly, or they may have some GENIE -> Allpix converter.

Thank you a lot for your answers. Unfortunately they do not. I asked them and they said that the way to do it is the deposition reader. I also asked geant people, but I got no answer (since allpix uses geant and I thought I could import the root file from initial macro from geant).

The way I read this, the comment is NOT about the order of the branches in the TTree but instead in the order of ā€˜branch nameā€™ in a string passed to the referred to function to enact an aliasing. So I assumed that there need to be a call like:

allpix::ConfigureBrachNames("your_name_for_the_event_branch:your_name_for_the_energy_branch:etc...."...)

(note that I do not know what the delimiter for the branch is or that I guessed right on the parameter type).