ATLAS Offline Software
Loading...
Searching...
No Matches
CaloMonTransforms.py
Go to the documentation of this file.
2# Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3#
4#
5# Author: Margherita Spalla (margherita.spalla@cern.ch)
6# Function for post-processing of histograms from CaloMonitoring
7#
8# The functions in this file are being implemented through the yaml config file inside the DataQuality package:
9# DataQuality/DataQualityUtils/data/postprocessing/CaloMonPostProc.yaml
10#
11
12#from ROOT import TMath
13import cppyy
14import ROOT as R
15
16def cloneHistFloat(toClone):
17 """ If we want to clone the bins from an integer-type histogram, but then wish to fill it with floats (for efficiencies) """
18 histype = str(type(toClone))
19 newhist = None
20 if "TH1I" in histype:
21 newhist = R.TH1F()
22 toClone.Copy(newhist)
23 elif "TH2I" in histype:
24 newhist = R.TH2F()
25 toClone.Copy(newhist)
26 else:
27 newhist = toClone.Clone()
28 return newhist
29
30
31def divideHistCaloMon(inputs,doPercentage=False):
32 """ This function returns the ratio of two ROOT histograms """
33 assert len(inputs) == 1
34 assert len(inputs[0][1]) == 3
35
36 cl = cloneHistFloat(inputs[0][1][0])
37 cl.Divide(inputs[0][1][1])
38
39 label = inputs[0][0]['thr']
40
41 hnorm = inputs[0][1][2].Clone()
42 n0 = hnorm.GetNbinsX()
43 fbin = hnorm.GetXaxis().FindBin(label)
44
45 if fbin>=n0 or fbin<=0:
46 return [cl]
47
48 thenorm = hnorm.GetBinContent(fbin)
49 cl.SetEntries(thenorm)
50
51 if not doPercentage or thenorm==0:
52 return [cl]
53
54 cl.Scale(100.0/thenorm)
55
56 return [cl]
57
58
59cppyy.cppdef("""
60void binbybinDiv(size_t nCells,TH2* fraction, const TH2* total, const TH2* occupancy) {
61for (size_t i=0;i<nCells;++i) {
62 const double t = total->GetBinContent(i);
63 const double o = occupancy->GetBinContent(i);
64 if (o>0) {
65 fraction->SetBinContent(i,t/o);
66 fraction->SetBinError(i,std::sqrt(1./o));
67 }
68 }
69}
70""")
71
72
73def divideByOccupancy(inputs,titleToReplace="",replaceTitWith=""):
74 """ This function returns the ratio of two ROOT histograms """
75 assert len(inputs) == 1
76 assert len(inputs[0][1]) == 2
77
78 cl = cloneHistFloat(inputs[0][1][0])
79 cl.Reset()
80
81 if titleToReplace!="":
82 tit = cl.GetTitle()
83 tit = tit.replace(titleToReplace,replaceTitWith)
84 cl.SetTitle(tit)
85
86 nCells = cl.GetNcells()
87 assert(nCells==inputs[0][1][1].GetNcells())
88
89 cppyy.gbl.binbybinDiv(nCells,cl,inputs[0][1][0],inputs[0][1][1])
90 # for i in range(nCells):
91 # t = inputs[0][1][0].GetBinContent(i);
92 # o = inputs[0][1][1].GetBinContent(i);
93 # if o>0:
94 # cl.SetBinContent(i,t/o);
95 # cl.SetBinError(i,TMath.Sqrt(1./o));
96 # pass
97 #pass
98
99 cl.SetEntries(inputs[0][1][0].GetEntries())
100
101 return [cl]
102
103
105 """ This function divides the input histogram by the number of events that passed a given selection"""
106 assert len(inputs) == 1
107 assert len(inputs[0][1]) == 2
108
109 cl = inputs[0][1][0].Clone()
110
111 label = inputs[0][0]['thr']
112
113 hnorm = inputs[0][1][1].Clone()
114 n0 = hnorm.GetNbinsX()
115 fbin = hnorm.GetXaxis().FindBin(label)
116
117 if fbin>=n0 or fbin<=0: #threshold not found
118 return [cl]
119
120 thenorm = hnorm.GetBinContent(fbin)
121
122 if thenorm==0:
123 return [cl]
124 cl.Scale(100.0/thenorm)
125
126 return [cl]
127
128
129def simpleDivideByAcceptedEvts(inputs,titleToReplace="",replaceTitWith=""):
130 """ This function divides the input histogram by the number of accepted events"""
131 assert len(inputs) == 1 #Expect only one match
132 assert len(inputs[0][1]) == 2 # pair of (regex-match,list-of-hists)
133
134 cl = cloneHistFloat(inputs[0][1][0]) # First histogram of the list of histgrams in the pair
135
136 hnorm = inputs[0][1][1] #Second histogram in the list of histograms is the number of events
137 theNorm=hnorm.GetBinContent(1)
138 if theNorm>1:
139 cl.Scale(100.0/theNorm)
140
141 if titleToReplace!="":
142 tit = cl.GetTitle()
143 tit = tit.replace(titleToReplace,replaceTitWith)
144 cl.SetTitle(tit)
145
146 return [cl]
TGraphErrors * GetEntries(TH2F *histo)
divideHistCaloMon(inputs, doPercentage=False)
divideByOccupancy(inputs, titleToReplace="", replaceTitWith="")
simpleDivideByAcceptedEvts(inputs, titleToReplace="", replaceTitWith="")