Root histograms in multi-files

  • Thread starter ChrisVer
  • Start date
  • Tags
    Root
In summary, the speaker was looking for a more efficient way to merge multiple .root files in different directories. They considered using the "hadd" command in the terminal but found it tedious and were unsure if a macro or TChain could handle the task. The expert suggested writing a shell script with a for loop to generate a single line command for merging all the files. After a few attempts, the speaker successfully merged the files using the suggested method.
  • #1
ChrisVer
Gold Member
3,378
464
Suppose I have a collection of 40 directories, each containing 1, 2 ,3 ,4 or 5 .root files with histograms and trees.
What is an optimal way to merge those .root files so I would end up with 40 directories and 40 root files with the histos added together?
One way I thought of was to start typing for the 40 different directories an "hadd" command in the terminal.

"hadd sum1.root dir1/*.root"
"hadd sum2.root dir2/*.root"
...
"hadd sum40.root dir40/*.root"


However I find this kind of tiring and stupid. Do you have any better solution?
My problem with a macro is that I am not sure if it understands the *.root notation (so I cannot think of iterating).
Finally I am not sure if a TChain can, apart from Trees in different root files, merge TH1s (histograms) too.

THanks :)
 
Technology news on Phys.org
  • #2
You could write a shell script that uses iteration to generate a text file containing a single line of the form:

hadd sum1.root dir1/*.root & ; hadd sum1.root dir2/*.root & ; hadd sum3.root dir1/*.root & ; ... ; hadd sum1.root dir40/*.root​

then just copy the line to the command prompt and execute it
 
  • Like
Likes ChrisVer
  • #3
andrewkirk said:
You could write a shell script that uses iteration to generate a text file containing a single line of the form:

hadd sum1.root dir1/*.root & ; hadd sum1.root dir2/*.root & ; hadd sum3.root dir1/*.root & ; ... ; hadd sum1.root dir40/*.root​

then just copy the line to the command prompt and execute it

Thanks for the reply...
It helped me to try to do it manually... after the 3rd try, I figured out what "for loop" I had to use in the shell script...That's what I had to do:
Bash:
i=0
for folder in directory_path_of_folders/*; do
    hadd -f target_directory/sumfile$((i++)).root $folder/*.root
done
 

Related to Root histograms in multi-files

What are root histograms in multi-files?

Root histograms in multi-files are a way of visualizing and analyzing data from multiple files using the ROOT framework. They allow for efficient comparison and analysis of data from different sources.

How do I create root histograms in multi-files?

To create root histograms in multi-files, you will need to use the ROOT framework and its associated libraries. The process involves importing the data from each file, defining the histogram parameters, and plotting the data using the appropriate commands.

What are the advantages of using root histograms in multi-files?

Root histograms in multi-files allow for the comparison and analysis of large datasets from different sources in a single visual representation. This makes it easier to identify patterns and relationships between the data, as well as facilitating efficient data analysis.

Can I customize the appearance of root histograms in multi-files?

Yes, you can customize the appearance of root histograms in multi-files by changing the color, style, and labels of the histograms. This can help to make the visual representation more informative and visually appealing.

Are there any limitations to using root histograms in multi-files?

One limitation of using root histograms in multi-files is that the data must be compatible with the ROOT framework. Additionally, the accuracy of the analysis may be affected by the quality and consistency of the data from each source.

Similar threads

  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top