CMS 3D CMS Logo

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