ATLAS Offline Software
MakeTH3DFromTH2Ds.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 import sys
4 import re
5 import array
6 from ROOT import *
7 
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 getJetDef(histName):
15  tokens = histName.split("_")
16  return tokens[-1]
17 
18 if len(sys.argv) < 6:
19  print "Too few arguments. Expected the following:"
20  print " 1. Input file"
21  print " 2. Output file"
22  print " 3. Output name of histogram"
23  print " 4+ Input histogram name(s) to be combined in a special format"
24  print " Format: histName,minVal,maxVal"
25  print " Example: PunchThroughCentral,0,1.3 where values are for |eta|"
26  exit(1)
27 
28 inputFile = TFile.Open(sys.argv[1],"READ")
29 outputFile = TFile.Open(sys.argv[2],"RECREATE")
30 outName = sys.argv[3]
31 inTuples = sys.argv[4:]
32 
33 inNames = []
34 inMinVals = []
35 inMaxVals = []
36 for inTuple in inTuples:
37  tokens = inTuple.split(",")
38  if len(tokens) != 3:
39  print "Unexpected input format: ",inTuple
40  exit(2)
41  inNames.append(tokens[0])
42  inMinVals.append(float(tokens[1]))
43  inMaxVals.append(float(tokens[2]))
44 
45 inHists = {}
46 outHists = {}
47 
48 # Get the input histos
49 for histName in inputFile.GetKeyNames():
50  hist = inputFile.Get(histName)
51 
52  foundHist = False
53  for inName,inMin,inMax in zip(inNames,inMinVals,inMaxVals):
54  if TString(histName).Contains(inName):
55  foundHist = True
56  jetDef = getJetDef(histName)
57  if not (jetDef in inHists):
58  inHists[jetDef] = {}
59  inHists[jetDef]["hist"] = []
60  inHists[jetDef]["min"] = []
61  inHists[jetDef]["max"] = []
62  inHists[jetDef]["hist"].append(hist)
63  inHists[jetDef]["min"].append(inMin)
64  inHists[jetDef]["max"].append(inMax)
65 
66  if not foundHist:
67  outputFile.cd()
68  hist.Write(histName)
69 
70 # Build the output histos
71 for aJetDef,aHist in inHists.iteritems():
72  hists = aHist["hist"]
73  minVals = aHist["min"]
74  maxVals = aHist["max"]
75 
76  xBins = []
77  for iX in range(1,hists[0].GetXaxis().GetNbins()+2):
78  xBins.append(hists[0].GetXaxis().GetBinLowEdge(iX))
79 
80  yBins = []
81  for iY in range(1,hists[0].GetYaxis().GetNbins()+2):
82  yBins.append(hists[0].GetYaxis().GetBinLowEdge(iY))
83 
84  # Set in dicts by minVal to sort, then ask if maxVal is next minVal
85  zDict = {}
86  zBins = []
87  for minVal,maxVal,hist in zip(minVals,maxVals,hists):
88  zDict[minVal] = maxVal
89  for minVal,maxVal in sorted(zDict.iteritems()):
90  #print "Min val: ",minVal," max val:",maxVal
91  if not zBins:
92  zBins.append(minVal)
93  zBins.append(maxVal)
94  elif fabs(zBins[-1]-minVal)<1.e-3:
95  zBins.append(maxVal)
96  else:
97  print "Min value does not correspond to previous max"
98  print "Max value: ",zBins[-1]
99  print "Min value: ",minVal
100  exit(3)
101 
102  # Make the actual histos
103  outHists[aJetDef] = TH3D(outName+"_"+aJetDef,outName+"_"+aJetDef,len(xBins)-1,array.array('d',xBins),len(yBins)-1,array.array('d',yBins),len(zBins)-1,array.array('d',zBins))
104  outHist = outHists[aJetDef]
105 
106  # Fill the histos
107  for minVal,maxVal in sorted(zDict.iteritems()):
108  index = 0
109  for minValGlobal,iVal in zip(minVals,range(0,len(minVals))):
110  if fabs(minValGlobal-minVal)<1.e-3:
111  index = iVal
112 
113  hist = hists[index]
114  for xBin in range(1,hist.GetXaxis().GetNbins()+1):
115  for yBin in range(1,hist.GetYaxis().GetNbins()+1):
116  outHist.SetBinContent(xBin,yBin,index+1,hist.GetBinContent(xBin,yBin))
117 
118  # Done filling this histo
119  # Now write
120  outputFile.cd()
121  outHist.Write(outName+"_"+aJetDef)
122 
123 outputFile.Close()
124 inputFile.Close()
125 
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
MakeTH3DFromTH2Ds.GetKeyNames
def GetKeyNames(self, dir="")
Definition: MakeTH3DFromTH2Ds.py:8
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
calibdata.exit
exit
Definition: calibdata.py:236
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
MakeTH3DFromTH2Ds.getJetDef
def getJetDef(histName)
Definition: MakeTH3DFromTH2Ds.py:14
readCCLHist.float
float
Definition: readCCLHist.py:83