ATLAS Offline Software
Loading...
Searching...
No Matches
ConvertOldHistosToNewHistos.py
Go to the documentation of this file.
1# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2
3
4import sys
5import re
6import array
7import math
8from ROOT import *
9def GetKeyNames(self,dir=""):
10 self.cd(dir)
11 return [key.GetName() for key in gDirectory.GetListOfKeys()]
12TFile.GetKeyNames = GetKeyNames
13
14if len(sys.argv) != 3:
15 print "Incorrect number of arguments. Expected the following:"
16 print " 1. Output root file"
17 print " 2. Input root file"
18 exit(1)
19
20outFileName = sys.argv[1]
21inFileName = sys.argv[2]
22
23
24if not outFileName.endswith(".root"):
25 print "Output file doesn't appear to be a root file:",outFile
26 print "Blocking for safety"
27 exit(2)
28if not inFileName.endswith(".root"):
29 print "Input file doesn't appear to be a root file:",inFileName
30 print "Blocking for safety"
31 exit(3)
32
33
34outFile = TFile.Open(outFileName,"RECREATE")
35inFile = TFile.Open(inFileName,"READ")
36
37for histName in inFile.GetKeyNames():
38 hist = inFile.Get(histName)
39
40 converted = hist
41 if histName.startswith("Zjet") or histName.startswith("MPF") or histName.startswith("Gjet") or histName.startswith("MJB") or histName.startswith("Effective") or histName.startswith("SingleParticle") or histName.startswith("Insitu") or histName.startswith("InSitu"):
42 # 2D(pT,eta) --> 1D(pT)
43 bins = []
44 for aBin in range(1,hist.GetNbinsX()+2):
45 bins.append(hist.GetXaxis().GetBinLowEdge(aBin))
46
47 binArray = array.array('d',bins)
48 converted = TH1D(histName+"_1D",hist.GetTitle(),len(bins)-1,binArray)
49
50 for aBin in range(1,hist.GetNbinsX()+1):
51 if math.fabs(hist.GetXaxis().GetBinLowEdge(aBin) - converted.GetXaxis().GetBinLowEdge(aBin)) > 1.e-4:
52 print "ERROR: Bin edges differ, %f vs %f"%(hist.GetXaxis().GetBinLowEdge(aBin),converted.GetXaxis().GetBinLowEdge(aBin))
53 exit(4)
54 converted.SetBinContent(aBin,hist.GetBinContent(aBin,1))
55
56 histNameConverted = None
57 if histName.endswith("AntiKt4Topo_LCJES"):
58 histNameConverted = re.sub("AntiKt4Topo_LCJES","AntiKt4LCTopo",histName)
59 elif histName.endswith("AntiKt4Topo_EMJES"):
60 histNameConverted = re.sub("AntiKt4Topo_EMJES","AntiKt4EMTopo",histName)
61 elif histName.endswith("AntiKt6Topo_LCJES"):
62 histNameConverted = re.sub("AntiKt6Topo_LCJES","AntiKt6LCTopo",histName)
63 elif histName.endswith("AntiKt6Topo_EMJES"):
64 histNameConverted = re.sub("AntiKt6Topo_EMJES","AntiKt6EMTopo",histName)
65 elif histName.endswith("AntiKt4EMTopo") or histName.endswith("AntiKt4LCTopo") or histName.endswith("AntiKt6EMTopo") or histName.endswith("AntiKt6LCTopo"):
66 histNameConverted = histName
67 else:
68 print "Unexpected hist name: ",histName
69 exit(5)
70
71 # Watch for Pythia8==MC12
72 if "Pythia8" in histNameConverted or "MC12a" in histNameConverted:
73 histNameConverted = re.sub("Pythia8","MC12",re.sub("MC12a","MC12",histNameConverted))
74
75 outFile.cd()
76 converted.Write(histNameConverted)
77
78inFile.Close()
79outFile.Close()
80