ATLAS Offline Software
ConvertOldUJHistosToNewHistos.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 
4 import sys
5 import re
6 import array
7 from ROOT import *
8 def GetKeyNames(self,dir=""):
9  self.cd(dir)
10  return [key.GetName() for key in gDirectory.GetListOfKeys()]
11 TFile.GetKeyNames = GetKeyNames
12 
13 
14 def ScaleBins(self,factor,axis="x"):
15  xBins = []
16  for aBin in range(1,self.GetNbinsX()+2):
17  xBins.append(self.GetXaxis().GetBinLowEdge(aBin))
18 
19  yBins = []
20  if self.GetDimension() > 1:
21  for aBin in range(1,self.GetNbinsY()+2):
22  yBins.append(self.GetYaxis().GetBinLowEdge(aBin))
23 
24  zBins = []
25  if self.GetDimension() > 2:
26  for aBin in range(1,self.GetNbinsZ()+2):
27  zBins.append(self.GetZaxis().GetBinLowEdge(aBin))
28 
29  if "x" in axis:
30  xBins = [aVal*factor for aVal in xBins]
31  if "y" in axis:
32  yBins = [aVal*factor for aVal in yBins]
33  if "z" in axis:
34  zBins = [aVal*factor for aVal in zBins]
35 
36  histo = None
37  if self.GetDimension() == 1:
38  if isinstance(self,TH1C):
39  histo = TH1C(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins))
40  elif isinstance(self,TH1S):
41  histo = TH1S(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins))
42  elif isinstance(self,TH1I):
43  histo = TH1I(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins))
44  elif isinstance(self,TH1F):
45  histo = TH1F(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins))
46  elif isinstance(self,TH1D):
47  histo = TH1D(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins))
48  for xBin in range(1,self.GetNbinsX()+1):
49  histo.SetBinContent(xBin,self.GetBinContent(xBin))
50  elif self.GetDimension() == 2:
51  if isinstance(self,TH2C):
52  histo = TH2C(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins))
53  elif isinstance(self,TH2S):
54  histo = TH2S(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins))
55  elif isinstance(self,TH2I):
56  histo = TH2I(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins))
57  elif isinstance(self,TH2F):
58  histo = TH2F(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins))
59  elif isinstance(self,TH2D):
60  histo = TH2D(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins))
61  for xBin in range(1,self.GetNbinsX()+1):
62  for yBin in range(1,self.GetNbinsY()+1):
63  histo.SetBinContent(xBin,yBin,self.GetBinContent(xBin,yBin))
64  elif self.GetDimension() == 3:
65  if isinstance(self,TH3C):
66  histo = TH3C(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins),len(zBins)-1,array.array('d',zBins))
67  elif isinstance(self,TH3S):
68  histo = TH3S(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins),len(zBins)-1,array.array('d',zBins))
69  elif isinstance(self,TH3I):
70  histo = TH3I(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins),len(zBins)-1,array.array('d',zBins))
71  elif isinstance(self,TH3F):
72  histo = TH3F(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins),len(zBins)-1,array.array('d',zBins))
73  elif isinstance(self,TH3D):
74  histo = TH3D(self.GetName()+"_scaled",self.GetTitle(),len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins),len(zBins)-1,array.array('d',zBins))
75  for xBin in range(1,self.GetNbinsX()+1):
76  for yBin in range(1,self.GetNbinsY()+1):
77  for zBin in range(1,self.GetNbinsZ()+1):
78  histo.SetBinContent(xBin,yBin,zBin,self.GetBinContent(xBin,yBin,zBin))
79  return histo
80 
81 TH1.ScaleBins = ScaleBins
82 
83 
84 
85 if len(sys.argv) != 3:
86  print "Incorrect number of arguments. Expected the following:"
87  print " 1. Output root file"
88  print " 2. Input root file"
89  exit(1)
90 
91 outFileName = sys.argv[1]
92 inFileName = sys.argv[2]
93 
94 
95 if not outFileName.endswith(".root"):
96  print "Output file doesn't appear to be a root file:",outFile
97  print "Blocking for safety"
98  exit(2)
99 if not inFileName.endswith(".root"):
100  print "Input file doesn't appear to be a root file:",inFileName
101  print "Blocking for safety"
102  exit(3)
103 
104 
105 outFile = TFile.Open(outFileName,"RECREATE")
106 inFile = TFile.Open(inFileName,"READ")
107 
108 # Get each hist and change names
109 # Also duplicate validity a bunch
110 validHist = inFile.Get("Valid_area")
111 #if validHist.GetXaxis().GetBinLowEdge(validHist.GetNbinsX()+1) > 5.0e3:
112 # validHist = validHist.ScaleBins(1.e-3,"x")
113 #if validHist.GetYaxis().GetBinLowEdge(validHist.GetNbinsY()+1) > 5.0e2:
114 # validHist = validHist.ScaleBins(1.e-3,"y")
115 outFile.cd()
116 validHist.Write("Valid_area")
117 for histName in inFile.GetKeyNames():
118  hist = inFile.Get(histName)
119 
120  histNameConverted = None
121  jetType="AntiKt10LCTopoTrimmedPtFrac5SmallR30"
122  if histName.startswith("valid") or histName.startswith("Valid"):
123  continue # Special case, we are duplicating this histogram
124  elif histName.endswith(jetType+"_pT"):
125  histNameConverted = re.sub(jetType+"_pT","pT_"+jetType,histName)
126  elif histName.endswith(jetType+"_Pileup_NPV"):
127  histNameConverted = re.sub(jetType+"_Pileup_NPV","Pileup_NPV_"+jetType,histName)
128  elif histName.endswith(jetType+"_Pileup_Mu"):
129  histNameConverted = re.sub(jetType+"_Pileup_Mu","Pileup_Mu_"+jetType,histName)
130  elif histName.endswith(jetType):
131  histNameConverted = histName
132  else:
133  print "Unexpected hist name: ",histName
134  exit(5)
135 
136  # Stretch pileup histos from TH1 to TH2, where 2nd dim is eta [-4.5,4.5] as a single bin (for equivalent with small-R)
137  converted = hist
138  if "Pileup" in histName:
139  # 1D (pT) --> 2D(pT,eta)
140  bins = []
141  for aBin in range(1,hist.GetNbinsX()+2):
142  bins.append(hist.GetXaxis().GetBinLowEdge(aBin))
143  binArray = array.array('d',bins)
144 
145  etaBins=[-4.5,4.5]
146  etaBinArray = array.array('d',etaBins)
147 
148  converted = TH2D(histName+"_2D",hist.GetTitle(),len(bins)-1,binArray,len(etaBins)-1,etaBinArray)
149 
150  for aBin in range(1,hist.GetNbinsX()+1):
151  if fabs(hist.GetXaxis().GetBinLowEdge(aBin) - converted.GetXaxis().GetBinLowEdge(aBin)) > 1.e-4:
152  print "ERROR: Bin edges differ, %f vs %f"%(hist.GetXaxis().GetBinLowEdge(aBin),converted.GetXaxis().GetBinLowEdge(aBin))
153  exit(4)
154  converted.SetBinContent(aBin,1,hist.GetBinContent(aBin))
155 
156  # Scale if necessary from MeV to GeV
157  #if converted.GetXaxis().GetBinLowEdge(converted.GetNbinsX()+1) > 5.0e3:
158  # converted = converted.ScaleBins(1.e-3,"x")
159  #if converted.GetYaxis().GetBinLowEdge(converted.GetNbinsY()+1) > 5.0e2:
160  # converted = converted.ScaleBins(1.e-3,"y")
161 
162 
163  outFile.cd()
164  converted.Write(histNameConverted)
165  if not "Pileup" in histNameConverted:
166  validHist.Write("valid_"+histNameConverted)
167 
168 inFile.Close()
169 outFile.Close()
170 
ConvertOldUJHistosToNewHistos.ScaleBins
def ScaleBins(self, factor, axis="x")
Definition: ConvertOldUJHistosToNewHistos.py:14
python.TrigEgammaMonitorHelper.TH2F
def TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:45
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
calibdata.exit
exit
Definition: calibdata.py:236
ConvertOldUJHistosToNewHistos.GetKeyNames
def GetKeyNames(self, dir="")
Definition: ConvertOldUJHistosToNewHistos.py:8
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24