Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:30

0001 #! /usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import ROOT
0005 import sys
0006 
0007 def getHistoName(wheel,station,sector):
0008     wheelStr = 'W' + str(wheel)
0009     stationStr = 'St' + str(station)
0010     sectorStr = 'Sec' + str(sector)
0011     name = "hRPhiVDriftCorr_" + wheelStr + "_" + stationStr + "_" + sectorStr
0012 
0013     return name
0014 
0015 def mergeHistosWheelSector(file, wheel, sector):
0016 
0017     histWheelSector = None 
0018     for station in range(1,5):
0019         if sector in (13,14) and station != 4: continue
0020         name = getHistoName(wheel,station,sector)
0021         hist = file.Get(name)
0022         if hist:
0023             print("Adding",hist.GetName())  
0024             if not histWheelSector: histWheelSector = hist.Clone( "h_W%d_Sec%d" % (wheel,sector) )
0025             else: histWheelSector.Add(hist)
0026 
0027     return histWheelSector
0028 
0029 def mergeHistosWheelStation(file, wheel, station):
0030 
0031     sectors = range(1,13)
0032     if station == 4: sectors.extend([13,14])
0033     histWheelStation = None
0034     for sector in sectors:
0035         name = getHistoName(wheel,station,sector)
0036         hist = file.Get(name)
0037         if hist:
0038             print("Adding",hist.GetName())
0039             if not histWheelStation: histWheelStation = hist.Clone( "h_W%d_St%d" % (wheel,station) )
0040             else: histWheelStation.Add(hist)
0041 
0042     return histWheelStation
0043 
0044 if __name__ == '__main__':
0045     import optparse
0046     parser = optparse.OptionParser ("Usage: %prog [--options]")
0047     # Options
0048     parser.add_option("-f","--file", dest="file", help="Input file name")
0049     parser.add_option("-o","--out", dest="out", default="merged.root", help="Output file name")
0050     (options, args) = parser.parse_args()
0051 
0052     if not options.file:
0053         parser.error('must set an input file')
0054     
0055     file = ROOT.TFile(options.file,"READ")
0056     ROOT.gROOT.cd()
0057  
0058     wheels = range(-2,3)
0059     stations = range(1,5)
0060     sectors = range(1,15)
0061     histos = {}
0062     for wheel in wheels:
0063         for station in stations:
0064             print("Merging histos from Wheel %d, Station %d" % (wheel,station))
0065             histos[(wheel,station)] = mergeHistosWheelStation(file,wheel,station) 
0066             
0067     file.Close()
0068   
0069     outputFile = ROOT.TFile(options.out,"RECREATE")
0070     outputFile.cd()
0071     for wheel in wheels:
0072         wheelStr = 'W' + str(wheel)
0073         for station in stations:
0074             stationStr = 'St' + str(station)
0075             for sector in sectors:
0076                 if sector in (13,14) and station != 4: continue
0077                 sectorStr = 'Sec' + str(sector)
0078                 name = "hRPhiVDriftCorr_" + wheelStr + "_" + stationStr + "_" + sectorStr
0079                 print("Writing",name) 
0080                 histos[(wheel,station)].Clone(name).Write()
0081  
0082     outputFile.Close()
0083 
0084     sys.exit(0)