CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/CalibMuon/DTCalibration/scripts/mergeVDriftHistosByStation.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import ROOT
00004 import sys
00005 
00006 def getHistoName(wheel,station,sector):
00007     wheelStr = 'W' + str(wheel)
00008     stationStr = 'St' + str(station)
00009     sectorStr = 'Sec' + str(sector)
00010     name = "hRPhiVDriftCorr_" + wheelStr + "_" + stationStr + "_" + sectorStr
00011 
00012     return name
00013 
00014 def mergeHistosWheelSector(file, wheel, sector):
00015 
00016     histWheelSector = None 
00017     for station in range(1,5):
00018         if sector in (13,14) and station != 4: continue
00019         name = getHistoName(wheel,station,sector)
00020         hist = file.Get(name)
00021         if hist:
00022             print "Adding",hist.GetName()  
00023             if not histWheelSector: histWheelSector = hist.Clone( "h_W%d_Sec%d" % (wheel,sector) )
00024             else: histWheelSector.Add(hist)
00025 
00026     return histWheelSector
00027 
00028 def mergeHistosWheelStation(file, wheel, station):
00029 
00030     sectors = range(1,13)
00031     if station == 4: sectors.extend([13,14])
00032     histWheelStation = None
00033     for sector in sectors:
00034         name = getHistoName(wheel,station,sector)
00035         hist = file.Get(name)
00036         if hist:
00037             print "Adding",hist.GetName()
00038             if not histWheelStation: histWheelStation = hist.Clone( "h_W%d_St%d" % (wheel,station) )
00039             else: histWheelStation.Add(hist)
00040 
00041     return histWheelStation
00042 
00043 if __name__ == '__main__':
00044     import optparse
00045     parser = optparse.OptionParser ("Usage: %prog [--options]")
00046     # Options
00047     parser.add_option("-f","--file", dest="file", help="Input file name")
00048     parser.add_option("-o","--out", dest="out", default="merged.root", help="Output file name")
00049     (options, args) = parser.parse_args()
00050 
00051     if not options.file:
00052         parser.error('must set an input file')
00053     
00054     file = ROOT.TFile(options.file,"READ")
00055     ROOT.gROOT.cd()
00056  
00057     wheels = range(-2,3)
00058     stations = range(1,5)
00059     sectors = range(1,15)
00060     histos = {}
00061     for wheel in wheels:
00062         for station in stations:
00063             print "Merging histos from Wheel %d, Station %d" % (wheel,station)
00064             histos[(wheel,station)] = mergeHistosWheelStation(file,wheel,station) 
00065             
00066     file.Close()
00067   
00068     outputFile = ROOT.TFile(options.out,"RECREATE")
00069     outputFile.cd()
00070     for wheel in wheels:
00071         wheelStr = 'W' + str(wheel)
00072         for station in stations:
00073             stationStr = 'St' + str(station)
00074             for sector in sectors:
00075                 if sector in (13,14) and station != 4: continue
00076                 sectorStr = 'Sec' + str(sector)
00077                 name = "hRPhiVDriftCorr_" + wheelStr + "_" + stationStr + "_" + sectorStr
00078                 print "Writing",name 
00079                 histos[(wheel,station)].Clone(name).Write()
00080  
00081     outputFile.Close()
00082 
00083     sys.exit(0)