| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

mergeFiles.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Script to add histograms from multiple ROOT files.
00004 #
00005 # Usage:
00006 # % mergeFiles.py -o outfile.root infile1.root infile2.root [infile3.root ...]
00007 #
00008 #  This is similar to the ROOT 'hadd' function, with some key differences:
00009 #   - Only combines histograms (no graphs or trees)
00010 #   - If a histogram x-axis is 'time' (ie. TH1::GetTimeDisplay()==true ),
00011 #     then that histogram is 'merged', extending the axis range and rebinning
00012 #     if needed.
00013 
00014 import ROOT
00015 TFile = ROOT.TFile
00016 TList = ROOT.TList
00017 TH1 = ROOT.TH1
00018 TDirectory = ROOT.TDirectory
00019 TObject = ROOT.TObject
00020 ROOT.gROOT.ProcessLine(".L $RUNDIAGNOSTICSROOT/share/mergeDirectories.C+")
00021 
00022 
00023 class MergeRoot:
00024     """Merge histograms in ROOT files"""
00025     def __init__(self,inputFilenames,outputFilename):
00026         """Constructor"""
00027         self.inputFilenames = inputFilenames
00028         self.outputFilename = outputFilename
00029         self.outputFile = None
00030         return
00031 
00032     def merge(self):
00033         """Merge input paths to output path"""
00034         TH1.AddDirectory(False)
00035         self.outputFile = TFile(self.outputFilename, "RECREATE")
00036         for filename in self.inputFilenames:
00037             print "Processing input: ",filename
00038             inputFile = TFile( filename ) 
00039             ROOT.mergeDirectories("",inputFile,self.outputFile)
00040             inputFile.Close()
00041         #Close Output files
00042         self.outputFile.Write()
00043         self.outputFile.Close()
00044         return
00045 
00046 if __name__=="__main__":
00047     """Process command line, merge files"""
00048     import sys, getopt, os
00049     opts, args = getopt.getopt(sys.argv[1:],"i:o:")
00050     print "opts = ",opts
00051     inFiles = []
00052     outFile = "merge.root"
00053     for opt, arg in opts:
00054         if opt == "-o":
00055             outFile = arg
00056     inFiles += args
00057     # Check for existence of input files 
00058     for filename in inFiles:
00059         if not os.path.isfile(filename):
00060             print "Error: '",filename,"' does not exist!"
00061             sys.exit(1)
00062     # Make sure we are merging at least 2 input files         
00063     if len(inFiles)<2:
00064         print "Usage:  % mergeFiles.py -o outfile.root infile1.root infile2.root [infile3.root ...]"
00065         sys.exit(1)
00066 
00067     # Merge files    
00068     merger = MergeRoot(inFiles, outFile)
00069     merger.merge()
00070     print "Done with merge"
00071     
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:10:26 2011 for RunDiagnostics by doxygen 1.4.7