ATLAS Offline Software
AddEmptyComponent.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 
19 if len(sys.argv) < 6:
20  print "Too few arguments. Expected the following:"
21  print " 1. Input file"
22  print " 2. Output file"
23  print " 3. Name of component to add"
24  print " 4. Number of dimensions"
25  print " 5. Ranges in a special format"
26  print " Format: xmin,xmax;ymin,max;zmin,zmax"
27  print " Example: 15,2500;-4.5,4.5"
28  exit(1)
29 
30 inputFile = TFile.Open(sys.argv[1],"READ")
31 outputFile = TFile.Open(sys.argv[2],"RECREATE")
32 compName = sys.argv[3]
33 numDim = int(sys.argv[4])
34 binning = sys.argv[5]
35 
36 # Parse binning
37 if numDim != 1 and numDim != 2 and numDim != 3:
38  print "Unexpected number of dimensions:",numDim
39  exit(2)
40 binTokens = binning.split(";")
41 if len(binTokens) != numDim:
42  print "Binning specifications and nDim don't match, %d vs %d"%(len(binTokens),numDim)
43  exit(3)
44 bins = []
45 for aToken in binTokens:
46  rangeTokens = aToken.split(",")
47  if len(rangeTokens) != 2:
48  print "Unexpected number of range tokens:",len(rangeTokens)
49  bins.append([float(rangeTokens[0]),float(rangeTokens[1])])
50 
51 
52 # Get the list of jet definitions and write original histos
53 jetDefs = []
54 for histName in inputFile.GetKeyNames():
55  jetDef = getJetDef(histName)
56  if not (jetDef in jetDefs):
57  jetDefs.append(jetDef)
58  hist = inputFile.Get(histName)
59  outputFile.cd()
60  hist.Write(histName)
61 
62 # Now write the new empty histos
63 for aDef in jetDefs:
64  histName = compName+"_"+aDef
65 
66  hist = None
67  if numDim == 1:
68  hist = TH1D(histName,histName,1,bins[0][0],bins[0][1])
69  hist.SetBinContent(1,0)
70  elif numDim == 2:
71  hist = TH2D(histName,histName,1,bins[0][0],bins[0][1],1,bins[1][0],bins[1][1])
72  hist.SetBinContent(1,1,0)
73  elif numDim == 3:
74  hist = TH3D(histName,histName,1,bins[0][0],bins[0][1],1,bins[1][0],bins[1][1],1,bins[2][0],bins[2][1])
75  hist.SetBinContent(1,1,1,0)
76 
77  print "Adding empty %dD histogram of name %s"%(numDim,histName)
78  outputFile.cd()
79  hist.Write(histName)
80 
81 
82 
83 outputFile.Close()
84 inputFile.Close()
85 
86 
TH3D
Definition: rootspy.cxx:505
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
AddEmptyComponent.getJetDef
def getJetDef(histName)
Definition: AddEmptyComponent.py:14
TH1D
Definition: rootspy.cxx:342
AddEmptyComponent.GetKeyNames
def GetKeyNames(self, dir="")
Definition: AddEmptyComponent.py:8
TH2D
Definition: rootspy.cxx:430
calibdata.exit
exit
Definition: calibdata.py:236
readCCLHist.float
float
Definition: readCCLHist.py:83