CMS 3D CMS Logo

mergeVDriftHistosByStation.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import ROOT
4 import sys
5 
6 def getHistoName(wheel,station,sector):
7  wheelStr = 'W' + str(wheel)
8  stationStr = 'St' + str(station)
9  sectorStr = 'Sec' + str(sector)
10  name = "hRPhiVDriftCorr_" + wheelStr + "_" + stationStr + "_" + sectorStr
11 
12  return name
13 
14 def mergeHistosWheelSector(file, wheel, sector):
15 
16  histWheelSector = None
17  for station in range(1,5):
18  if sector in (13,14) and station != 4: continue
19  name = getHistoName(wheel,station,sector)
20  hist = file.Get(name)
21  if hist:
22  print "Adding",hist.GetName()
23  if not histWheelSector: histWheelSector = hist.Clone( "h_W%d_Sec%d" % (wheel,sector) )
24  else: histWheelSector.Add(hist)
25 
26  return histWheelSector
27 
28 def mergeHistosWheelStation(file, wheel, station):
29 
30  sectors = range(1,13)
31  if station == 4: sectors.extend([13,14])
32  histWheelStation = None
33  for sector in sectors:
34  name = getHistoName(wheel,station,sector)
35  hist = file.Get(name)
36  if hist:
37  print "Adding",hist.GetName()
38  if not histWheelStation: histWheelStation = hist.Clone( "h_W%d_St%d" % (wheel,station) )
39  else: histWheelStation.Add(hist)
40 
41  return histWheelStation
42 
43 if __name__ == '__main__':
44  import optparse
45  parser = optparse.OptionParser ("Usage: %prog [--options]")
46  # Options
47  parser.add_option("-f","--file", dest="file", help="Input file name")
48  parser.add_option("-o","--out", dest="out", default="merged.root", help="Output file name")
49  (options, args) = parser.parse_args()
50 
51  if not options.file:
52  parser.error('must set an input file')
53 
54  file = ROOT.TFile(options.file,"READ")
55  ROOT.gROOT.cd()
56 
57  wheels = range(-2,3)
58  stations = range(1,5)
59  sectors = range(1,15)
60  histos = {}
61  for wheel in wheels:
62  for station in stations:
63  print "Merging histos from Wheel %d, Station %d" % (wheel,station)
64  histos[(wheel,station)] = mergeHistosWheelStation(file,wheel,station)
65 
66  file.Close()
67 
68  outputFile = ROOT.TFile(options.out,"RECREATE")
69  outputFile.cd()
70  for wheel in wheels:
71  wheelStr = 'W' + str(wheel)
72  for station in stations:
73  stationStr = 'St' + str(station)
74  for sector in sectors:
75  if sector in (13,14) and station != 4: continue
76  sectorStr = 'Sec' + str(sector)
77  name = "hRPhiVDriftCorr_" + wheelStr + "_" + stationStr + "_" + sectorStr
78  print "Writing",name
79  histos[(wheel,station)].Clone(name).Write()
80 
81  outputFile.Close()
82 
83  sys.exit(0)
def mergeHistosWheelStation(file, wheel, station)
def getHistoName(wheel, station, sector)
def mergeHistosWheelSector(file, wheel, sector)