ATLAS Offline Software
Loading...
Searching...
No Matches
AddEmptyComponent.py
Go to the documentation of this file.
1# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2
3import sys
4import re
5import array
6from ROOT import *
7
8def GetKeyNames(self,dir=""):
9 self.cd(dir)
10 return [key.GetName() for key in gDirectory.GetListOfKeys()]
11TFile.GetKeyNames = GetKeyNames
12
13
14def getJetDef(histName):
15 tokens = histName.split("_")
16 return tokens[-1]
17
18
19if 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
30inputFile = TFile.Open(sys.argv[1],"READ")
31outputFile = TFile.Open(sys.argv[2],"RECREATE")
32compName = sys.argv[3]
33numDim = int(sys.argv[4])
34binning = sys.argv[5]
35
36# Parse binning
37if numDim != 1 and numDim != 2 and numDim != 3:
38 print "Unexpected number of dimensions:",numDim
39 exit(2)
40binTokens = binning.split(";")
41if len(binTokens) != numDim:
42 print "Binning specifications and nDim don't match, %d vs %d"%(len(binTokens),numDim)
43 exit(3)
44bins = []
45for 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
53jetDefs = []
54for 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
63for 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
83outputFile.Close()
84inputFile.Close()
85
86