Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#! /usr/bin/env python3

import ROOT
import sys

def getHistoName(wheel,station,sector):
    wheelStr = 'W' + str(wheel)
    stationStr = 'St' + str(station)
    sectorStr = 'Sec' + str(sector)
    name = "hRPhiVDriftCorr_" + wheelStr + "_" + stationStr + "_" + sectorStr

    return name

def mergeHistosWheelSector(file, wheel, sector):

    histWheelSector = None 
    for station in range(1,5):
        if sector in (13,14) and station != 4: continue
        name = getHistoName(wheel,station,sector)
        hist = file.Get(name)
        if hist:
            print("Adding",hist.GetName())  
            if not histWheelSector: histWheelSector = hist.Clone( "h_W%d_Sec%d" % (wheel,sector) )
            else: histWheelSector.Add(hist)

    return histWheelSector

def mergeHistosWheelStation(file, wheel, station):

    sectors = range(1,13)
    if station == 4: sectors.extend([13,14])
    histWheelStation = None
    for sector in sectors:
        name = getHistoName(wheel,station,sector)
        hist = file.Get(name)
        if hist:
            print("Adding",hist.GetName())
            if not histWheelStation: histWheelStation = hist.Clone( "h_W%d_St%d" % (wheel,station) )
            else: histWheelStation.Add(hist)

    return histWheelStation

if __name__ == '__main__':
    import optparse
    parser = optparse.OptionParser ("Usage: %prog [--options]")
    # Options
    parser.add_option("-f","--file", dest="file", help="Input file name")
    parser.add_option("-o","--out", dest="out", default="merged.root", help="Output file name")
    (options, args) = parser.parse_args()

    if not options.file:
        parser.error('must set an input file')
    
    file = ROOT.TFile(options.file,"READ")
    ROOT.gROOT.cd()
 
    wheels = range(-2,3)
    stations = range(1,5)
    sectors = range(1,15)
    histos = {}
    for wheel in wheels:
        for station in stations:
            print("Merging histos from Wheel %d, Station %d" % (wheel,station))
            histos[(wheel,station)] = mergeHistosWheelStation(file,wheel,station) 
            
    file.Close()
  
    outputFile = ROOT.TFile(options.out,"RECREATE")
    outputFile.cd()
    for wheel in wheels:
        wheelStr = 'W' + str(wheel)
        for station in stations:
            stationStr = 'St' + str(station)
            for sector in sectors:
                if sector in (13,14) and station != 4: continue
                sectorStr = 'Sec' + str(sector)
                name = "hRPhiVDriftCorr_" + wheelStr + "_" + stationStr + "_" + sectorStr
                print("Writing",name) 
                histos[(wheel,station)].Clone(name).Write()
 
    outputFile.Close()

    sys.exit(0)