-
Notifications
You must be signed in to change notification settings - Fork 0
/
makePlots.py
executable file
·1016 lines (941 loc) · 56 KB
/
makePlots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
from __future__ import annotations
from collections import defaultdict
from collections.abc import Sequence
import functools
import numpy as np
from typing import Any
import ROOT
from plotFitResults import (
getAxisInfoForBinningVar,
makeEquidistantBinning,
plotGraphs1D,
)
from plotTools import (
callMemberFunctionsWithArgs,
drawZeroLine,
getCbFriendlyRootColor,
makeDirPath,
printGitInfo,
setupPlotStyle,
)
# always flush print() to reduce garbling of log files due to buffering
print = functools.partial(print, flush = True)
# simplified versions of Paul's criteria
UNUSED_TRACK_FOUND_CONDITION: str = "(" \
+ "(NmbUnusedTracks == 1)" \
+ " and ((MissingProtonTheta < 5) or (abs(UnusedDeltaPhi[0]) <= 30))" \
+ " and (abs(UnusedDeltaTheta[0]) <= 30)" \
+ " and (abs(UnusedDeltaPOverP[0]) <= 0.6)" \
+ ")"
UNUSED_TRACK_FOUND_CONDITION_MEASURED: str = "(" \
+ "(NmbUnusedTracks == 1)" \
+ " and ((MissingProtonTheta_Measured < 5) or (abs(UnusedDeltaPhi_Measured[0]) <= 30))" \
+ " and (abs(UnusedDeltaTheta_Measured[0]) <= 30)" \
+ " and (abs(UnusedDeltaPOverP_Measured[0]) <= 0.6)" \
+ ")"
# filter expressions for track-found cases
FILTER_CASES: dict[str, str] = {
"Total" : "(true)",
"Found" : "(TrackFound == true)",
"Missing" : "(TrackFound == false)",
}
COLOR_CASES = {
"Total" : ROOT.kGray,
"Found" : ROOT.kGreen + 2,
"Missing" : ROOT.kRed + 1,
}
def drawHistogram(
inFileName: str,
histName: str,
rebinFactor: int | Sequence[int] = 1, # if integer -> rebin x axis; if sequence of 2 integers -> rebin x and y axes
drawOption: str = "HIST",
pdfFileNamePrefix: str = "",
pdfFileNameSuffix: str = "",
pdfDirName: str = "./",
) -> None:
"""Plots histogram with given name in ROOT file with given name"""
# get histogram
inFile = ROOT.TFile(inFileName)
hist = inFile.Get(histName)
if isinstance(hist, ROOT.TH2):
if isinstance(rebinFactor, int):
hist.RebinX(rebinFactor)
elif isinstance(rebinFactor, Sequence):
hist.Rebin2D(rebinFactor[0], rebinFactor[1])
elif isinstance(hist, ROOT.TH1):
hist.Rebin(rebinFactor)
# draw histogram
canv = ROOT.TCanvas(f"{pdfFileNamePrefix}{hist.GetName()}{pdfFileNameSuffix}")
hist.Draw(drawOption)
canv.SaveAs(f"{pdfDirName}/{canv.GetName()}.pdf")
def getHistND(
inputData: ROOT.RDataFrame,
variables: tuple[str | tuple[str, str], ...], # variable(s) to plot; is tuple of either column names or tuples with new column definitions; defines dimension of histogram
axisTitles: str, # semicolon-separated list
binning: tuple[int, float, float] | tuple[int, float, float, int, float, float], # tuple with 1D or 2D binning definitions
weightVariable: str | tuple[str, str] | None = None, # may be None (= no weighting), string with column name, or tuple with new column definition
filterExpression: str | None = None,
histNameSuffix: str = "",
histTitle: str = "",
) -> ROOT.TH1D | ROOT.TH2D:
"""Creates histogram from given variables in RDataFrame, applying optional weighting and filtering"""
histDim = len(variables)
assert 1 <= histDim <= 2, "currently, only 1D and 2D histograms are supported"
# apply additional filters, if defined
data = inputData.Filter(filterExpression) if filterExpression else inputData
columnNames: list[str] = [""] * len(variables)
for index, variable in enumerate(variables):
if isinstance(variable, str):
# use existing variable column
columnNames[index] = variable
elif isinstance(variable, Sequence):
# create new variable column
data = data.Define(variable[0], variable[1])
columnNames[index] = variable[0]
assert columnNames[index] != "", f"failed to get column name for variable '{variable}'"
if isinstance(weightVariable, Sequence) and not isinstance(weightVariable, str):
# create new weight column
data = data.Define(weightVariable[0], weightVariable[1])
# create histogram
hist = None
histDef = ( # histogram definition
"_".join(columnNames + ([histNameSuffix] if histNameSuffix else [])),
f"{histTitle};{axisTitles}",
*binning,
)
# get member function to create histogram
HistoND = getattr(data, "Histo1D") if histDim == 1 else getattr(data, "Histo2D")
if not weightVariable:
hist = HistoND(histDef, *columnNames)
elif isinstance(weightVariable, str):
# use existing weight column
hist = HistoND(histDef, *columnNames, weightVariable)
elif isinstance(weightVariable, Sequence):
# use new weight column
hist = HistoND(histDef, *columnNames, weightVariable[0])
assert hist is not None, f"failed to create histogram for weight variable '{weightVariable}'"
return hist
def setDefaultYAxisTitle(
axisTitles: str | None, # semicolon-separated list
defaultYTitle: str = "Number of Combos (RF-subtracted)",
) -> str:
"""Sets default y-axis title if not provided by `axisTitles`"""
if (axisTitles is None):
return ";" + defaultYTitle
titles = axisTitles.split(";")
if (len(titles) == 1):
return titles[0] + ";" + defaultYTitle
elif (len(titles) == 0):
return ";" + defaultYTitle
else:
return axisTitles
def plot1D(
inputData: ROOT.RDataFrame,
variable: str | tuple[str, str], # variable to plot; may be column name, or tuple with new column definition
axisTitles: str, # semicolon-separated list
binning: tuple[int, float, float], # tuple with binning definition
weightVariable: str | tuple[str, str] | None = "AccidWeightFactor", # may be None (= no weighting), string with column name, or tuple with new column definition
pdfFileNamePrefix: str = "",
pdfFileNameSuffix: str = "",
pdfDirName: str = "./",
additionalFilter: str | None = None,
) -> None:
"""Plots 1D distribution for given variable, applying optional weighting and filtering"""
hist: ROOT.TH1D = getHistND(
inputData = inputData,
variables = (variable,),
axisTitles = setDefaultYAxisTitle(axisTitles),
binning = binning,
weightVariable = weightVariable,
filterExpression = additionalFilter,
)
# draw distributions
canv = ROOT.TCanvas(f"{pdfFileNamePrefix}{hist.GetName()}{pdfFileNameSuffix}")
hist.Draw("HIST")
drawZeroLine(hist)
canv.SaveAs(f"{pdfDirName}/{canv.GetName()}.pdf")
def plot2D(
inputData: ROOT.RDataFrame,
xVariable: str | tuple[str, str], # x variable to plot; may be column name, or tuple with new column definition
yVariable: str | tuple[str, str], # y variable to plot; may be column name, or tuple with new column definition
axisTitles: str, # semicolon-separated list
binning: tuple[int, float, float, int, float, float], # tuple with binning definition
weightVariable: str | tuple[str, str] | None = "AccidWeightFactor", # may be None (= no weighting), string with column name, or tuple with new column definition
pdfFileNamePrefix: str = "",
pdfFileNameSuffix: str = "",
pdfDirName: str = "./",
additionalFilter: str | None = None,
) -> None:
"""Plots 2D distribution for given x and y variables, applying optional weighting and filtering"""
hist: ROOT.TH2D = getHistND(
inputData = inputData,
variables = (xVariable, yVariable),
axisTitles = axisTitles,
binning = binning,
weightVariable = weightVariable,
filterExpression = additionalFilter,
)
# draw distributions
canv = ROOT.TCanvas(f"{pdfFileNamePrefix}{hist.GetName()}{pdfFileNameSuffix}")
hist.Draw("COLZ")
canv.SaveAs(f"{pdfDirName}/{canv.GetName()}.pdf")
def overlayDataSamples1D(
dataSamples: dict[str, dict[str, Any]], # TFile or RDataFrame and style definitions for each data-set label
histName: str | None = None, # name of histogram to plot; required for TFile
variable: str | tuple[str, str] | None = None, # variable to plot; may be column name, or tuple with new column definition; required for RDataFrame
axisTitles: str | None = None, # semicolon-separated list; required for RDataFrame
binning: tuple[int, float, float] | None = None, # tuple with binning definition; required for RDataFrame
weightVariable: str | tuple[str, str] | None = "AccidWeightFactor", # may be None (= no weighting), string with column name, or tuple with new column definition
pdfFileNamePrefix: str = "",
pdfFileNameSuffix: str = "",
pdfDirName: str = "./",
additionalFilter: str | None = None,
histTitle: str | None = None,
) -> None:
"""Overlays 1D histograms generated from given trees or read from given files"""
print("Overlaying " + (f"histograms '{histName}'" if histName else f"distributions for '{variable}'") + f" for data samples '{', '.join(dataSamples.keys())}'")
pdfFileBaseName = histName if histName is not None else variable
hStack = ROOT.THStack(pdfFileBaseName, ("" if histTitle is None else histTitle) + f";{setDefaultYAxisTitle(axisTitles)}")
hists: list[ROOT.TH1D] = [] # keep histograms in memory
normIntegral = None # index of histogram to normalize to
for dataLabel, dataSample in dataSamples.items():
hist: ROOT.TH1D | None = None
if "TFile" in dataSample:
# read histogram from file
assert histName is not None, f"Name of histogram to read from file '{dataSample['TFile'].GetPath()}' required."
hist = dataSample["TFile"].Get(histName)
hist.SetTitle(dataLabel)
elif "RDataFrame" in dataSample:
assert variable is not None and axisTitles is not None and binning is not None, f"Need variable name (={variable}), axis titles (={axisTitles}), and binning (={binning})."
hist = getHistND(
inputData = dataSample["RDataFrame"],
variables = (variable,),
axisTitles = setDefaultYAxisTitle(axisTitles),
binning = binning,
weightVariable = weightVariable,
filterExpression = additionalFilter,
histNameSuffix = dataLabel,
histTitle = dataLabel,
).GetPtr()
else:
raise KeyError(f"Data sample must contain either 'TFile' or 'RDataFrame' key: {dataSample}")
assert hist is not None, "Could not create histogram"
callMemberFunctionsWithArgs(hist, dataSample)
if dataSample.get("normToThis", False):
print(f"Normalizing all histograms to '{dataLabel}'")
normIntegral = hist.Integral()
hists.append(hist)
hStack.Add(hist)
# normalize histograms
if normIntegral is not None:
for hist in hists:
hist.Scale(normIntegral / hist.Integral())
# draw distributions
canv = ROOT.TCanvas(f"{pdfFileNamePrefix}{pdfFileBaseName}_overlay_{'_'.join(dataSamples.keys())}{pdfFileNameSuffix}")
hStack.Draw("NOSTACK HIST")
# add legend
# canv.BuildLegend() # automatic placement with width 0.3 and height 0.21
canv.BuildLegend(0.3, 0.15, 0.3, 0.15) # automatic placement with width 0.3 and height 0.15
# canv.BuildLegend(0.7, 0.65, 0.99, 0.99)
canv.SaveAs(f"{pdfDirName}/{canv.GetName()}.pdf")
def getResolutionGraph(
inputData: ROOT.RDataFrame,
diffVariable: str, # residual from which resolution is measured
diffVariableBinning: tuple[int, float, float], # tuple with binning definition for diffVariable
resBinningVariable: str | tuple[str, str], # variable to bin resolution in; may be column name, or tuple with new column definition
resBinning: tuple[int, float, float], # tuple with binning definition for resBinningVariable
weightVariable: str | tuple[str, str] | None = "AccidWeightFactor", # may be None (= no weighting), string with column name, or tuple with new column definition
additionalFilter: str | None = None,
histTitle: str | None = None,
histPdfFileName: str | None = None,
) -> ROOT.TGraphErrors:
"""Plots resolution of given variable in the in bins of `resBinningVariable`"""
# !Note! resolution variables are arrays with length NmbTruthTracks, need to define dummy column for first element
hist: ROOT.TH2D = getHistND(
inputData = inputData,
variables = ((f"{diffVariable}_", f"{diffVariable}[0]"), resBinningVariable),
axisTitles = "",
binning = (*diffVariableBinning, *resBinning),
weightVariable = weightVariable,
filterExpression = additionalFilter,
)
# draw distributions
if histPdfFileName is not None:
canv = ROOT.TCanvas(histPdfFileName)
canv.SetLogz()
if histTitle is not None:
hist.SetTitle(histTitle)
hist.Draw("COLZ")
canv.SaveAs(histPdfFileName)
# construct resolution graph
resBinningAxis = hist.GetYaxis()
xVals = np.array([resBinningAxis.GetBinCenter(resBinIndex) for resBinIndex in range(1, resBinningAxis.GetNbins() + 1)], dtype = np.float64)
xErrs = np.array([resBinningAxis.GetBinWidth (resBinIndex) / 2.0 for resBinIndex in range(1, resBinningAxis.GetNbins() + 1)], dtype = np.float64)
yVals = np.array([], dtype = np.float64)
yErrs = np.array([], dtype = np.float64)
for resBinIndex in range(1, resBinningAxis.GetNbins() + 1):
proj = hist.ProjectionX("_px", resBinIndex, resBinIndex, "E")
yVals = np.append(yVals, (proj.GetStdDev(),))
yErrs = np.append(yErrs, (proj.GetStdDevError(),))
return ROOT.TGraphErrors(len(xVals), xVals, yVals, xErrs, yErrs)
def overlayResolutions(
inputData: Sequence[tuple[str, ROOT.RDataFrame]],
resVariableName: str, # name of variable for which resolution is estimated
diffVariable: str, # residual from which resolution is measured
diffVariableBinning: tuple[int, float, float], # tuple with binning definition for diffVariable
resBinningVariable: str | tuple[str, str], # variable to bin resolution in; may be a column name, or a tuple with new column definition
resBinning: tuple[int, float, float], # tuple with binning definition for resBinningVariable
weightVariable: str | tuple[str, str] | None = "AccidWeightFactor", # may be None (= no weighting), string with column name, or tuple with new column definition
pdfFileNamePrefix: str = "",
pdfFileNameSuffix: str = "",
pdfDirName: str = "./",
diffVariableAxisTitle: str | None = None,
additionalFilter: str | None = None,
resPlotRange: tuple[float | None, float | None] = (None, None),
) -> None:
"""Plots resolution of given variable in the in bins of `resBinningVariable`"""
resBinningVariableName, resBinningVariableLabel, resBinningVariableUnit = getAxisInfoForBinningVar(resBinningVariable)
resGraphs: list[tuple[str, ROOT.TGraphErrors]] = []
for label, data in inputData:
resGraphs.append((label,
getResolutionGraph(
inputData = data,
diffVariable = diffVariable,
diffVariableBinning = diffVariableBinning,
resBinningVariable = resBinningVariable,
resBinning = resBinning,
weightVariable = weightVariable,
additionalFilter = additionalFilter,
histTitle = f";{'' if diffVariableAxisTitle is None else diffVariableAxisTitle};{resBinningVariableLabel} ({resBinningVariableUnit})",
histPdfFileName = f"{pdfDirName}/{pdfFileNamePrefix}resolution2D_{diffVariable}_{resBinningVariable}_{label}{pdfFileNameSuffix}.pdf",
)))
_, resVariableLabel, resVariableUnit = getAxisInfoForBinningVar(resVariableName)
binningInfo = makeEquidistantBinning([(resBinningVariableName, *resBinning)])
plotGraphs1D(
graphOrGraphs = resGraphs,
binningInfo = binningInfo,
yAxisTitle = f"#it{{#sigma}}_{{{resVariableLabel}}} ({resVariableUnit})",
pdfDirName = pdfDirName,
pdfFileBaseName = f"resolution_{resVariableName}",
pdfFileNamePrefix = pdfFileNamePrefix,
pdfFileNameSuffix = pdfFileNameSuffix,
graphMinimum = resPlotRange[0],
graphMaximum = resPlotRange[1],
skipBlack = True if len(resGraphs) > 1 else False,
)
def overlayCases(
inputData: ROOT.RDataFrame,
variable: str | tuple[str, str], # variable to plot; may be column name, or tuple with new column definition
axisTitles: str, # semicolon-separated list
binning: tuple[int, float, float], # tuple with binning definition
weightVariable: str | tuple[str, str] | None = "AccidWeightFactor", # may be None (= no weighting), string with column name, or tuple with new column definition
pdfFileNamePrefix: str = "",
pdfFileNameSuffix: str = "",
pdfDirName: str = "./",
additionalFilter: str | None = None,
) -> None:
"""Overlays 1D distributions of given variable for "Total", "Found", and "Missing" cases"""
data = inputData.Filter(additionalFilter) if additionalFilter else inputData
# overlay distributions for cases
hStack = ROOT.THStack(f"{variable}", ";" + setDefaultYAxisTitle(axisTitles))
hists = []
for case, caseFilter in FILTER_CASES.items():
hist: ROOT.TH1D = getHistND(
inputData = data,
variables = (variable,),
axisTitles = setDefaultYAxisTitle(axisTitles),
binning = binning,
weightVariable = weightVariable,
filterExpression = caseFilter,
histNameSuffix = case,
histTitle = case,
)
hist.SetLineColor(COLOR_CASES[case])
if case == "Total":
hist.SetFillColor(COLOR_CASES[case])
hists.append(hist)
hStack.Add(hist.GetPtr())
# draw distributions
canv = ROOT.TCanvas(f"{pdfFileNamePrefix}{variable}_cases{pdfFileNameSuffix}")
hStack.Draw("NOSTACK HIST")
# add legend
canv.BuildLegend(0.7, 0.65, 0.99, 0.99)
drawZeroLine(hStack)
canv.SaveAs(f"{pdfDirName}/{canv.GetName()}.pdf")
# C++ helper functors that fill TObjString into TH1
# workaround because RDataFrame cannot fill TObjSTring into histogram
# !Note! the code is not thread-safe; RDataFrame needs to run in single-threaded mode
# see https://sft.its.cern.ch/jira/browse/ROOT-10246
#TODO make this code thread-safe
# see https://root-forum.cern.ch/t/filling-histograms-in-parallel/35460/3
# and https://root.cern/doc/master/mt201__parallelHistoFill_8C.html
CPP_CODE = """
struct fillHistWithTObjString {
fillHistWithTObjString(TH1& hist)
: _hist(hist)
{ }
void
operator ()(const TObjString& s)
{
_hist.Fill(s.GetString().Data(), 1);
return;
}
TH1& _hist;
};
"""
ROOT.gInterpreter.Declare(CPP_CODE)
CPP_CODE = """
struct fillHistWithTObjStringWeighted {
fillHistWithTObjStringWeighted(TH1& hist)
: _hist(hist)
{ }
void
operator ()(
const TObjString& s,
const Double_t w
) {
_hist.Fill(s.GetString().Data(), w);
return;
}
TH1& _hist;
};
"""
ROOT.gInterpreter.Declare(CPP_CODE)
def getTopologyHist(
inputData: ROOT.RDataFrame,
weightVariable: str | tuple[str, str] | None = None, # may be None (= no weighting), string with column name, or tuple with new column definition
filterExpression: str | None = None,
histNameSuffix: str = "",
) -> tuple[list[str], ROOT.TH1F]:
"""Fills categorical histogram with counts for each generated topology, applying optional weighting and filtering, and returns list of topology strings and histogram"""
# apply additional filters, if defined
data = inputData.Filter(filterExpression) if filterExpression else inputData
if not isinstance(weightVariable, str) and isinstance(weightVariable, Sequence):
# create new weight column
data = data.Define(weightVariable[0], weightVariable[1])
# create histogram
variable = "ThrownTopology"
histName = variable
if isinstance(weightVariable, str):
histName += f"_{weightVariable}"
elif isinstance(weightVariable, Sequence):
histName += f"_{weightVariable[0]}"
if filterExpression:
histName += f"_{filterExpression}"
if histNameSuffix:
histName += f"_{histNameSuffix}"
hist = ROOT.TH1F(histName, "", 1, 0, 1)
# fill histogram
fillHistWithTObjString = ROOT.fillHistWithTObjString (hist)
fillHistWithTObjStringWeighted = ROOT.fillHistWithTObjStringWeighted(hist)
if not weightVariable:
data.Foreach(fillHistWithTObjString, [variable])
elif isinstance(weightVariable, str):
# use existing weight column
data.Foreach(fillHistWithTObjStringWeighted, [variable, weightVariable])
elif isinstance(weightVariable, Sequence):
# use new weight column
data.Foreach(fillHistWithTObjStringWeighted, [variable, weightVariable[0]])
hist.LabelsDeflate("X")
hist.LabelsOption(">", "X") # sort topologies by number od combos
# get ordered list of topology names
xAxis = hist.GetXaxis()
topoNames = [xAxis.GetBinLabel(binIndex) for binIndex in range(1, xAxis.GetNbins() + 1)]
return (topoNames, hist)
def getCategoricalTH1AsDict(hist: ROOT.TH1) -> dict[str, float]:
"""Returns categorical histogram as dict { bin label : bin content }"""
xAxis = hist.GetXaxis()
return {xAxis.GetBinLabel(binIndex) : hist.GetBinContent(binIndex)
for binIndex in range(1, xAxis.GetNbins() + 1)}
def plotTopologyHist(
inputData: ROOT.RDataFrame,
normalize: bool = False,
maxNmbTopologies: int = 10,
additionalFilter: str | None = None,
pdfFileNamePrefix: str = "",
pdfFileNameSuffix: str = "",
pdfDirName: str = "./",
) -> None:
"""Plots categorical histogram with counts or fraction for each generated topology, applying optional weighting and filtering"""
# get histogram data
topoNames: dict[str, list[str]] = {} # dictionary of ordered list of topology names { case : [ topologyName ] }
topoHists: dict[str, ROOT.TH1F] = {} # dictionary of topology histograms { case : topologyHist }
for case, caseFilter in FILTER_CASES.items():
caseData = inputData.Filter(caseFilter)
topoNames[case], topoHists[case] = getTopologyHist(
inputData = caseData,
weightVariable = "AccidWeightFactor",
filterExpression = additionalFilter,
histNameSuffix = case + ("_norm" if normalize else ""),
)
# overlay distributions for cases
hStack = ROOT.THStack(f"topologies", ";;" + ("Fraction" if normalize else "Number") + " of Combos (RF-subtracted)" + (" [%]" if normalize else ""))
topoLabels = topoNames["Total"]
hists: dict[str, ROOT.TH1] = {} # memorize plots to print
for case in FILTER_CASES.keys():
# ensure that bin labels in all histograms have same order as defined by the "Total" histogram
hist = ROOT.TH1F(f"{pdfFileNamePrefix}topologies_{case}{'_norm' if normalize else ''}{pdfFileNameSuffix}", case, len(topoLabels), 0, len(topoLabels))
xAxis = hist.GetXaxis()
for binIndex, binLabel in enumerate(topoLabels):
xAxis.SetBinLabel(binIndex + 1, binLabel)
# get histogram values as dictionary, i.e. { topology : count }
histValues = getCategoricalTH1AsDict(topoHists[case])
# set bin content of histogram
for binLabel in topoLabels:
hist.Fill(binLabel, histValues[binLabel] if binLabel in histValues else 0)
integral = hist.Integral()
if normalize and integral != 0:
hist.Scale(100 / integral)
hist.SetLineColor(COLOR_CASES[case])
if case == "Total":
hist.SetFillColor(COLOR_CASES[case])
hists[case] = hist
hStack.Add(hist)
print(f"plotTopologyHist(): {case} " + (f"purity = {hist.GetBinContent(1)}%" if normalize else f"signal = {hist.GetBinContent(1)} of {integral} combos"))
canv = ROOT.TCanvas(f"{pdfFileNamePrefix}topologies{'_norm' if normalize else ''}{pdfFileNameSuffix}")
hStack.Draw("NOSTACK HIST")
hStack.SetMinimum(0)
if normalize:
hStack.SetMaximum(100)
hStack.GetXaxis().SetRangeUser(0, maxNmbTopologies)
# add legend
legend = canv.BuildLegend(0.7, 0.65, 0.99, 0.99)
# add labels that show number or fraction outside of plot range
legend.AddEntry(ROOT.MakeNullPointer(ROOT.TObject), "Other topologies:", "")
for case in FILTER_CASES.keys():
integralOtherTopos = hists[case].Integral(maxNmbTopologies, hists[case].GetNbinsX())
legendEntry = legend.AddEntry(ROOT.MakeNullPointer(ROOT.TObject), " " + str(round(integralOtherTopos)) + ("%" if normalize else " Combos"), "")
legendEntry.SetTextColor(ROOT.kBlack if case == "Total" else COLOR_CASES[case])
canv.SaveAs(f"{pdfDirName}/{canv.GetName()}.pdf")
def overlayTopologies(
inputData: ROOT.RDataFrame,
variable: str | tuple[str, str], # variable to plot; may be column name, or tuple with new column definition
axisTitles: str, # semicolon-separated list
binning: tuple[int, float, float], # tuple with binning definition
toposToPlot: dict[str, list[str]], # topologies to plot for each case
additionalFilter: str | None = None,
pdfFileNamePrefix: str = "",
pdfFileNameSuffix: str = "_MCbggen_topologies",
pdfDirName: str = "./",
) -> None:
"""Overlays 1D distributions for given variable from overall data sample and distributions for the `maxNmbTopologies` topologies with the largest number of combos from the bggen MC sample"""
data = inputData.Filter(additionalFilter) if additionalFilter else inputData
for case, caseFilter in FILTER_CASES.items():
caseData = data.Filter(caseFilter)
# get topologies with largest number of combos for given case
hStack = ROOT.THStack(f"{variable}_{case}", f"{case};{setDefaultYAxisTitle(axisTitles)}")
hists = []
# overlay distributions for topologies
for index, topo in enumerate(toposToPlot[case]):
hist: ROOT.TH1D = getHistND(
inputData = caseData,
variables = (variable,),
axisTitles = setDefaultYAxisTitle(axisTitles),
binning = binning,
weightVariable = "AccidWeightFactor",
filterExpression = (f'ThrownTopology.GetString() == "{topo}"' if topo != "Total" else "true"),
histNameSuffix = f"{case}_{topo}",
histTitle = topo,
)
if topo == "Total":
hist.SetLineColor(ROOT.kGray)
hist.SetFillColor(ROOT.kGray)
else:
hist.SetLineColor(index)
hists.append(hist)
hStack.Add(hist.GetPtr())
# draw distributions
canv = ROOT.TCanvas(f"{pdfFileNamePrefix}{variable}{pdfFileNameSuffix}_{case}")
hStack.Draw("NOSTACK HIST")
# add legend
canv.BuildLegend(0.7, 0.65, 0.99, 0.99)
drawZeroLine(hStack)
canv.SaveAs(f"{pdfDirName}/{canv.GetName()}.pdf")
def makeKinematicPlotsOverlays(
dataSamples: dict[str, dict[str, Any]], # RDataFrame and style definitions for each data-set label
channel: str, # label for the reaction channel
mesonSystemMassVarName: str, # name of mass variable of meson system recoiling against the missing proton
mesonSystemMassVarTLatex: str, # TLatex string for mass variable of meson system recoiling against the missing proton
pdfDirName: str = "./",
) -> None:
"""Overlays kinematic distributions of given data samples"""
# overlayDataSamples1D(dataSamples, variable = "NmbUnusedShowers", axisTitles = "Number of Unused Showers", binning = (11, -0.5, 10.5), pdfDirName = pdfDirName)
kwargss = (
{
"additionalFilter" : "(NmbUnusedShowers == 0)",
"pdfFileNameSuffix" : "_noUnusedShowers",
},
{
"additionalFilter" : "(NmbUnusedShowers == 0) && ((5.5 < BeamEnergy) && (BeamEnergy < 11.0))",
"pdfFileNameSuffix" : "_noUnusedShowers_beamEnergy_5.5_11.0",
},
{
"additionalFilter" : "(NmbUnusedShowers == 0) && ((7.5 < BeamEnergy) && (BeamEnergy < 9.0))",
"pdfFileNameSuffix" : "_noUnusedShowers_beamEnergy_7.5_9.0",
},
)
for kwargs in kwargss:
kwargs.update({"pdfDirName" : pdfDirName})
kwargs.update({"pdfFileNamePrefix" : f"{channel}_"})
for kwargs in kwargss:
overlayDataSamples1D(dataSamples, variable = "BeamEnergy", axisTitles = "#it{E}_{beam} (GeV)", binning = (180, 3, 12), **kwargs)
overlayDataSamples1D(dataSamples, variable = "KinFitPVal", axisTitles = "#it{#chi}^{2}_{kin. fit} #it{P}-value", binning = (150, 0, 1), **kwargs)
overlayDataSamples1D(dataSamples, variable = "MissingProtonP", axisTitles = "#it{p}_{miss}^{kin. fit} (GeV/#it{c})", binning = (250, 0, 5), **kwargs)
overlayDataSamples1D(dataSamples, variable = "MissingProtonTheta", axisTitles = "#it{#theta}_{miss}^{kin. fit} (deg)", binning = (200, 0, 100), **kwargs)
overlayDataSamples1D(dataSamples, variable = "MissingProtonPhi", axisTitles = "#it{#phi}_{miss}^{kin. fit} (deg)", binning = (180, -180, 180), **kwargs)
overlayDataSamples1D(dataSamples, variable = mesonSystemMassVarName, axisTitles = mesonSystemMassVarTLatex + "^{kin. fit} (GeV/#it{c}^{2})", binning = (400, 0, 5), **kwargs)
# unused track
overlayDataSamples1D(dataSamples, variable = "UnusedDeltaPOverP", axisTitles = "(#it{p}_{miss}^{unused} #minus #it{p}_{miss}^{kin. fit}) / #it{p}_{miss}^{kin. fit}", binning = (375, -1.5, +1.5), **kwargss[0])
overlayDataSamples1D(dataSamples, variable = "UnusedDeltaTheta", axisTitles = "#it{#theta}_{miss}^{unused} #minus #it{#theta}_{miss}^{kin. fit} (deg)", binning = (100, -50, +50), **kwargss[0])
overlayDataSamples1D(dataSamples, variable = "UnusedDeltaPhi", axisTitles = "#it{#phi}_{miss}^{unused} #minus #it{#phi}_{miss}^{kin. fit} (deg)", binning = (200, -100, +100), **kwargss[0])
# missing mass squared
for case, caseFilter in FILTER_CASES.items():
kwargs = {
"additionalFilter" : f"((NmbUnusedShowers == 0) and {caseFilter})",
"pdfFileNamePrefix" : f"{channel}_",
"pdfFileNameSuffix" : f"_{case}_noUnusedShowers",
"pdfDirName" : pdfDirName,
}
overlayDataSamples1D(dataSamples, variable = "MissingMassSquared_Measured",
axisTitles = "(#it{m}_{miss}^{meas.})^{2} (GeV/#it{c}^{2})^{2}", binning = (125, -0.5, 4.5), **kwargs)
def makeKinematicPlotsMc(
dataSample: ROOT.RDataFrame,
channel: str, # label for the reaction channel
isMcBggen: bool, # indicates whether MC data are from bggen
trueTopology: str, # reaction string for true reaction
pdfDirName: str = "./",
) -> None:
"""Plots kinematic distributions for given Monte Carlo data"""
filterTopologies = {
"" : None,
"_sig" : f'(ThrownTopology.GetString() == "{trueTopology}")',
"_bkg" : f'(ThrownTopology.GetString() != "{trueTopology}")',
}
for suffix, filter in filterTopologies.items():
kwargs = {
"additionalFilter" : "(NmbTruthTracks == 1)" + ("" if filter is None else f" && {filter}"),
"pdfFileNamePrefix" : f"{channel}_",
"pdfFileNameSuffix" : suffix,
"pdfDirName" : pdfDirName,
}
overlayCases(dataSample, "TruthDeltaP", axisTitles = "#it{p}_{miss}^{truth} #minus #it{p}_{miss}^{kin. fit} (GeV/#it{c})", binning = (600, -4, +4), **kwargs)
overlayCases(dataSample, "TruthDeltaPOverP", axisTitles = "(#it{p}_{miss}^{truth} #minus #it{p}_{miss}^{kin. fit}) / #it{p}_{miss}^{kin. fit}", binning = (500, -2, +2), **kwargs)
overlayCases(dataSample, "TruthDeltaTheta", axisTitles = "#it{#theta}_{miss}^{truth} #minus #it{#theta}_{miss}^{kin. fit} (deg)", binning = (200, -60, +60), **kwargs)
overlayCases(dataSample, "TruthDeltaPhi", axisTitles = "#it{#phi}_{miss}^{truth} #minus #it{#phi}_{miss}^{kin. fit} (deg)", binning = (360, -180, +180), **kwargs)
if isMcBggen: # fill plots for bggen Monte Carlo
cutsArgs: list[dict[str, Any]] = [
{}, # no extra cut
{"additionalFilter" : "(NmbUnusedShowers == 0)", "pdfFileNameSuffix" : f"_noUnusedShowers"}, # no unused showers
# {"additionalFilter" : "((NmbUnusedShowers == 0) and (BestMissingMatchDistTOF < 40))", "pdfFileNameSuffix" : f"_noUnusedShowersMatchToF"}, # no unused showers and ToF hit within certain distance
]
for kwargs in cutsArgs:
kwargs.update({"pdfDirName" : pdfDirName})
kwargs.update({"pdfFileNamePrefix" : f"{channel}_"})
plotTopologyHist(dataSample, normalize = False, **kwargs)
plotTopologyHist(dataSample, normalize = True, **kwargs)
cutsArgs = [
{}, # no extra cut
{"additionalFilter" : "(NmbUnusedShowers == 0)", "pdfFileNameSuffix" : "_noUnusedShowers"}, # no unused showers
# # the two cuts below are equivalent to the one above
# {"additionalFilter" : "(EnergyUnusedShowers == 0)", "pdfFileNameSuffix" : "_noEnergyUnusedShowers"},
# {"additionalFilter" : "(NmbUnusedShowers == 0) and (EnergyUnusedShowers == 0)", "pdfFileNameSuffix" : "_noShowers"}
# {"additionalFilter" : "((NmbUnusedShowers == 0) and (BestMissingMatchDistTOF < 40))", "pdfFileNameSuffix" : "_noUnusedShowersMatchToF"}, # no unused showers and ToF hit within certain distance
]
for kwargs in cutsArgs:
kwargs.update({"pdfDirName" : pdfDirName})
kwargs.update({"pdfFileNamePrefix" : f"{channel}_"})
# get topologies with the largest number of combos for given case
toposToPlot: dict[str, list[str]] = {}
for case, caseFilter in FILTER_CASES.items():
caseData = dataSample.Filter(caseFilter)
# get all topology strings
toposToPlot[case], _ = getTopologyHist(caseData, filterExpression = kwargs.get("additionalFilter", None))
# take first, i.e. largest, maxNmbTopologies topologies and add "Total" topology
toposToPlot[case] = ["Total"] + toposToPlot[case][:maxNmbTopologies]
overlayTopologies(dataSample, "NmbUnusedShowers", axisTitles = "Number of Unused Showers", binning = (11, -0.5, 10.5), toposToPlot = toposToPlot, **kwargs)
# overlayTopologies(dataSample, "EnergyUnusedShowers", axisTitles = "Unused Shower Energy (GeV)", binning = (60, 0, 6), toposToPlot = toposToPlot, **kwargs)
# overlayTopologies(dataSample, "BestMissingMatchDistTOF", axisTitles = "Distance to best ToF match (cm)", binning = (25, 0, 250), toposToPlot = toposToPlot, **kwargs)
# overlayTopologies(dataSample, "BestMissingMatchDistBCAL", axisTitles = "Distance to best BCAL match (cm)", binning = (20, 0, 200), toposToPlot = toposToPlot, **kwargs)
# overlayTopologies(dataSample, "MissingMassSquared", axisTitles = "(#it{m}_{miss}^{kin. fit})^{2} (GeV/#it{c}^{2})^{2}", binning = (125, -0.5, 4.5), toposToPlot = toposToPlot, **kwargs)
overlayTopologies(dataSample, "MissingMassSquared_Measured", axisTitles = "(#it{m}_{miss}^{meas.})^{2} (GeV/#it{c}^{2})^{2}", binning = (125, -0.5, 4.5), toposToPlot = toposToPlot, **kwargs)
def makeKinematicPlotsData(
dataSample: ROOT.RDataFrame,
channel: str, # label for the reaction channel
mesonSystemMassVarName: str, # name of mass variable of meson system recoiling against the missing proton
mesonSystemMassVarTLatex: str, # TLatex string for mass variable of meson system recoiling against the missing proton
pdfDirName: str = "./",
) -> None:
"""Plots kinematic distributions for given Monte Carlo data"""
cutsArgs: list[dict[str, Any]] = [
{}, # no extra cut
{"additionalFilter" : "(NmbUnusedShowers == 0)", "pdfFileNameSuffix" : "_noUnusedShowers"},
]
for kwargs in cutsArgs:
kwargs.update({"pdfDirName" : pdfDirName})
kwargs.update({"pdfFileNamePrefix" : f"{channel}_"})
plot1D(dataSample, "AccidWeightFactor", axisTitles = "RF Weight", binning = (1000, -2, 2), **kwargs, weightVariable = None)
plot1D(dataSample, "KinFitPVal", axisTitles = "#it{#chi}^{2}_{kin. fit} #it{P}-value", binning = (150, 0, 1), **kwargs)
plot1D(dataSample, "NmbUnusedShowers", axisTitles = "Number of Unused Showers", binning = (11, -0.5, 10.5), **kwargs)
plot1D(dataSample, "BeamEnergy", axisTitles = "#it{E}_{beam} (GeV)", binning = (180, 3, 12), **kwargs)
# plot1D(dataSample, "BestMissingMatchDistTOF", axisTitles = "Distance to best ToF match (cm)", binning = (25, 0, 250), **kwargs)
# plot1D(dataSample, "BestMissingMatchDistBCAL", axisTitles = "Distance to best BCAL match (cm)", binning = (20, 0, 200), **kwargs)
plot1D(dataSample, mesonSystemMassVarName, axisTitles = mesonSystemMassVarTLatex + "^{kin. fit} (GeV/#it{c}^{2})", binning = (400, 0, 5), **kwargs)
sideBandYTitle = "Number of Combos (RF-Sideband)"
# sideBandArgs: dict[str, Any] = {
# "weightVariable" : ("AccidWeightFactorSb", "1 - AccidWeightFactor"),
# "additionalFilter" : kwargs.get("additionalFilter", None),
# "pdfFileNamePrefix" : f"{channel}_",
# "pdfFileNameSuffix" : "_Sb" + kwargs.get("pdfFileNameSuffix", ""),
# "pdfDirName" : pdfDirName,
# }
# plot1D(dataSample, ("MissingMass", "sqrt(MissingMassSquared)"), axisTitles = "#it{m}_{miss}^{kin. fit} (GeV/#it{c}^{2})", binning = (100, 0, 2), **kwargs)
# plot1D(dataSample, ("MissingMass", "sqrt(MissingMassSquared)"), axisTitles = "#it{m}_{miss}^{kin. fit} (GeV/#it{c}^{2});" + sideBandYTitle, binning = (100, 0, 2), **sideBandArgs)
# plot1D(dataSample, "MissingMassSquared", axisTitles = "(#it{m}_{miss}^{kin. fit})^{2} (GeV/#it{c}^{2})^{2}", binning = (225, -0.5, 4), **kwargs)
# plot1D(dataSample, "MissingMassSquared", axisTitles = "(#it{m}_{miss}^{kin. fit})^{2} (GeV/#it{c}^{2})^{2};" + sideBandYTitle, binning = (225, -0.5, 4), **sideBandArgs)
# plot1D(dataSample, ("MissingMass_Measured", "sqrt(MissingMassSquared_Measured)"), axisTitles = "#it{m}_{miss}^{meas.} (GeV/#it{c}^{2})", binning = (100, 0, 2), **kwargs)
# plot1D(dataSample, ("MissingMass_Measured", "sqrt(MissingMassSquared_Measured)"), axisTitles = "#it{m}_{miss}^{meas.} (GeV/#it{c}^{2});" + sideBandYTitle, binning = (100, 0, 2), **sideBandArgs)
# missing-mass squared distributions
mm2HistDef: dict[str, Any] = {"variable" : "MissingMassSquared_Measured", "axisTitles" : "(#it{m}_{miss}^{meas.})^{2} (GeV/#it{c}^{2})^{2}", "binning" : (125, -0.5, 4.5)}
mm2HistDefSideBand: dict[str, Any] = {"variable" : "MissingMassSquared_Measured", "axisTitles" : "(#it{m}_{miss}^{meas.})^{2} (GeV/#it{c}^{2})^{2};" + sideBandYTitle, "binning" : (125, -0.5, 4.5), "weightVariable" : ("AccidWeightFactorSb", "1 - AccidWeightFactor")}
kwargsNoSuffix = dict(kwargs)
kwargsNoSuffix.pop("pdfFileNameSuffix", None)
overlayCases(dataSample, **mm2HistDef, **kwargs)
overlayCases(dataSample, **mm2HistDefSideBand, pdfFileNameSuffix = f"_Sb" + kwargs.get("pdfFileNameSuffix", ""), **kwargsNoSuffix)
# plot overall distributions for each case
for case, caseFilter in FILTER_CASES.items():
caseData = dataSample.Filter(caseFilter)
plot1D(caseData, **mm2HistDef, pdfFileNameSuffix = f"_{case}" + kwargs.get("pdfFileNameSuffix", ""), **kwargsNoSuffix)
plot1D(caseData, **mm2HistDefSideBand, pdfFileNameSuffix = f"_{case}_Sb" + kwargs.get("pdfFileNameSuffix", ""), **kwargsNoSuffix)
# kinematicBinnings = [
# # beam energy
# # {"variable" : "BeamEnergy", "label" : "Beam Energy", "unit" : "GeV", "nmbBins" : 9, "range" : (3.0, 12.0)}, # spring 2017
# {"variable" : "BeamEnergy", "label" : "Beam Energy", "unit" : "GeV", "nmbBins" : 10, "range" : (5.5, 11.5)}, # spring 2018
# # momentum of missing proton
# {"variable" : "MissingProtonP", "label" : "#it{p}_{miss}^{kin. fit}", "unit" : "GeV/#it{c}", "nmbBins" : 10, "range" : (0, 3.5)},
# # polar angle of missing proton
# {"variable" : "MissingProtonTheta", "label" : "#it{#theta}_{miss}^{kin. fit}", "unit" : "deg", "nmbBins" : 10, "range" : (0, 65)},
# # azimuthal angle of missing proton
# {"variable" : "MissingProtonPhi", "label" : "#it{#phi}_{miss}^{kin. fit}", "unit" : "deg", "nmbBins" : 10, "range" : (-180, +180)},
# ]
# for kinematicBinning in kinematicBinnings:
# kinBinVariable = kinematicBinning["variable"]
# nmbKinBins = kinematicBinning["nmbBins"]
# kinBinRange = kinematicBinning["range"]
# kinBinWidth = (kinBinRange[1] - kinBinRange[0]) / float(nmbKinBins)
# # plot distributions for kinematic bins
# for kinBinIndex in range(nmbKinBins):
# kinBinMin = kinBinRange[0] + kinBinIndex * kinBinWidth
# kinBinMax = kinBinMin + kinBinWidth
# kinBinFilter = f"(({kinBinMin} < {kinBinVariable}) and ({kinBinVariable} < {kinBinMax}))"
# kinBinData = dataSample.Filter(kinBinFilter)
# overlayCases(kinBinData, **mm2HistDef, pdfFileNameSuffix = f"_{kinBinVariable}_{kinBinMin}_{kinBinMax}" + kwargs.get("pdfFileNameSuffix", ""), **kwargsNoSuffix)
plot1D(dataSample, "MissingProtonP", axisTitles = "#it{p}_{miss}^{kin. fit} (GeV/#it{c})", binning = (250, 0, 5), **kwargs)
plot1D(dataSample, "MissingProtonTheta", axisTitles = "#it{#theta}_{miss}^{kin. fit} (deg)", binning = (200, 0, 100), **kwargs)
plot1D(dataSample, "MissingProtonPhi", axisTitles = "#it{#phi}_{miss}^{kin. fit} (deg)", binning = (180, -180, 180), **kwargs)
plot2D(dataSample, xVariable = "MissingProtonTheta", yVariable = "MissingProtonP", axisTitles = "#it{#theta}_{miss}^{kin. fit} (deg);#it{p}_{miss}^{kin. fit} (GeV/#it{c})", binning = (180, 0, 90, 400, 0, 9), **kwargs)
plot2D(dataSample, xVariable = "MissingProtonTheta", yVariable = "MissingProtonPhi", axisTitles = "#it{#theta}_{miss}^{kin. fit} (deg);#it{#phi}_{miss}^{kin. fit} (deg)", binning = (180, 0, 90, 180, -180, 180), **kwargs)
# plot2D(dataSample, xVariable = "MissingProtonTheta_Measured", yVariable = "MissingProtonP_Measured", axisTitles = "#it{#theta}_{miss}^{meas.} (deg);#it{p}_{miss}^{meas.} (GeV/#it{c})", binning = (180, 0, 90, 400, 0, 9), **kwargs)
# plot2D(dataSample, xVariable = "MissingProtonTheta_Measured", yVariable = "MissingProtonPhi_Measured", axisTitles = "#it{#theta}_{miss}^{meas.} (deg);#it{#phi}_{miss}^{meas.} (deg)", binning = (180, 0, 90, 360, -180, 180), **kwargs)
# plot1D(dataSample, "UnusedDeltaP", axisTitles = "#it{p}_{miss}^{unused} #minus #it{p}_{miss}^{kin. fit} (GeV/#it{c})", binning = (600, -6, 6), **kwargs)
plot1D(dataSample, "UnusedDeltaPOverP", axisTitles = "(#it{p}_{miss}^{unused} #minus #it{p}_{miss}^{kin. fit}) / #it{p}_{miss}^{kin. fit}", binning = (500, -2, 2), **kwargs)
plot1D(dataSample, "UnusedDeltaTheta", axisTitles = "#it{#theta}_{miss}^{unused} #minus #it{#theta}_{miss}^{kin. fit} (deg)", binning = (200, -100, 100), **kwargs)
plot1D(dataSample, "UnusedDeltaPhi", axisTitles = "#it{#phi}_{miss}^{unused} #minus #it{#phi}_{miss}^{kin. fit} (deg)", binning = (360, -180, 180), **kwargs)
# overlayCases(dataSample, "UnusedDeltaP", axisTitles = "#it{p}_{miss}^{unused} #minus #it{p}_{miss}^{kin. fit} (GeV/#it{c})", binning = (600, -6, 6), **kwargs)
overlayCases(dataSample, "UnusedDeltaPOverP", axisTitles = "(#it{p}_{miss}^{unused} #minus #it{p}_{miss}^{kin. fit}) / #it{p}_{miss}^{kin. fit}", binning = (500, -2, 2), **kwargs)
overlayCases(dataSample, "UnusedDeltaTheta", axisTitles = "#it{#theta}_{miss}^{unused} #minus #it{#theta}_{miss}^{kin. fit} (deg)", binning = (200, -100, 100), **kwargs)
overlayCases(dataSample, "UnusedDeltaPhi", axisTitles = "#it{#phi}_{miss}^{unused} #minus #it{#phi}_{miss}^{kin. fit} (deg)", binning = (360, -180, 180), **kwargs)
# unusedTrackData = dataSample.Filter("(NmbUnusedTracks == 1)") # make sure unused track info exists; NOTE! this assumes that there is maximum 1 unused track
# plot2D(unusedTrackData, xVariable = ("UnusedP_", "UnusedP[0]"), yVariable = "MissingProtonP", axisTitles = "#it{p}_{miss}^{unused} (GeV/#it{c});#it{p}_{miss}^{kin. fit} (GeV/#it{c})", binning = (400, 0, 9, 400, 0, 9), **kwargs)
# plot2D(unusedTrackData, xVariable = ("UnusedTheta_", "UnusedTheta[0]"), yVariable = "MissingProtonTheta", axisTitles = "#it{#theta}_{miss}^{unused} (deg);#it{#theta}_{miss}^{kin. fit} (deg)", binning = (360, 0, 180, 360, 0, 180), **kwargs)
# plot2D(unusedTrackData, xVariable = ("UnusedPhi_", "UnusedPhi[0]"), yVariable = "MissingProtonPhi", axisTitles = "#it{#phi}_{miss}^{unused} (deg);#it{#phi}_{miss}^{kin. fit} (deg)", binning = (360, -180, 180, 360, -180, 180), **kwargs)
if __name__ == "__main__":
printGitInfo()
ROOT.gROOT.SetBatch(True)
setupPlotStyle()
dataPeriods = [
"2017_01-ver03",
# "2018_01-ver02",
# "2018_08-ver02",
# "2019_11-ver01",
]
# # pi+pi+pi-pi-(p)
# treeName = "pippippimpimpmiss"
# trueTopology = "2#pi^{#plus}2#pi^{#minus}p"
# mesonSystemMassVarName = "FourPionMass"
# mesonSystemMassVarTLatex = "#it{m}_{#it{#pi}^{#plus}#it{#pi}^{#minus}#it{#pi}^{#plus}#it{#pi}^{#minus}}"
# omega(p)
treeName = "omegapmiss"
trueTopology = "2#gamma#pi^{#plus}#pi^{#minus}p[#pi^{0},#omega]"
mesonSystemMassVarName = "ThreePionMass"
mesonSystemMassVarTLatex = "#it{m}_{#it{#pi}^{#plus}#it{#pi}^{#minus}#it{#pi}^{0}}"
pdfBaseDirName = "./plots"
maxNmbTopologies = 10
maxNmbEventsToProcess = 0 # maximum number of events to process; 0 means all events
# plot generated MC truth histograms for signal process
if True:
# open input files with histograms
dataSamplesToOverlay: dict[str, dict[str, Any]] = {}
for index, dataPeriod in enumerate(dataPeriods):
inputFileName = f"./data/MCbggen/{dataPeriod}/{treeName}.MCbggen_{dataPeriod}.root"
print(f"Reading generated MC histograms from file {inputFileName}")
dataSamplesToOverlay[dataPeriod] = {
"TFile" : ROOT.TFile.Open(inputFileName, "READ"),
"normToThis" : True if index == 0 else False,
# define plot style
"SetLineColor" : getCbFriendlyRootColor(index, skipBlack = True),
"SetLineWidth" : 2,
}
histInfos: tuple[dict[str, str], ...] = (
{
"histNameSuffix" : "",
"histTitle" : "",
},
{
"histNameSuffix" : "_BeamEnergyRange1",
"histTitle" : "5.5 < #it{E}_{beam} < 11.0 GeV",
},
{
"histNameSuffix" : "_BeamEnergyRange2",
"histTitle" : "7.5 < #it{E}_{beam} < 9.0 GeV",
},
)
for histInfo in histInfos:
kwargs = {
"pdfFileNamePrefix" : f"{treeName}_",
"pdfFileNameSuffix" : "_SigMcTruth",
"pdfDirName" : makeDirPath(f"{pdfBaseDirName}/MCbggen"),
"histTitle" : histInfo["histTitle"],
}
overlayDataSamples1D(dataSamplesToOverlay, histName = f"SignalTruthBeamEnergy{ histInfo['histNameSuffix']}", axisTitles = "#it{E}_{beam}^{truth} (GeV)", **kwargs)
overlayDataSamples1D(dataSamplesToOverlay, histName = f"SignalTruthProtonP{ histInfo['histNameSuffix']}", axisTitles = "#it{p}_{#it{p}}^{truth} (GeV/#it{c})", **kwargs)
overlayDataSamples1D(dataSamplesToOverlay, histName = f"SignalTruthProtonTheta{ histInfo['histNameSuffix']}", axisTitles = "#it{#theta}_{#it{p}}^{truth} (deg)", **kwargs)
overlayDataSamples1D(dataSamplesToOverlay, histName = f"SignalTruthProtonPhi{ histInfo['histNameSuffix']}", axisTitles = "#it{#phi}_{#it{p}}^{truth} (deg)", **kwargs)
overlayDataSamples1D(dataSamplesToOverlay, histName = f"SignalTruth{mesonSystemMassVarName}{histInfo['histNameSuffix']}", axisTitles = mesonSystemMassVarTLatex + "^{truth} (GeV/#it{c}^{2})", **kwargs)
# open input files with trees
inputData: dict[str, dict[str, ROOT.RDataFrame]] = defaultdict(dict) # dict[<data period>][<data type>]
for dataPeriod in dataPeriods:
inputFileNames = {
"MCbggen" : f"./data/MCbggen/{dataPeriod}/{treeName}_flatTree.MCbggen_{dataPeriod}.root",
"RD" : f"./data/RD/{dataPeriod}/{treeName}_flatTree.RD_{dataPeriod}_*.root",
}
print(f"Reading tree '{treeName}' from files {inputFileNames}")
for dataType, inputFileName in inputFileNames.items():
inputData[dataPeriod][dataType] = ROOT.RDataFrame(treeName, inputFileName) \
.Range(maxNmbEventsToProcess) \
.Define("TrackFound", UNUSED_TRACK_FOUND_CONDITION) \
.Filter("(-0.25 < MissingMassSquared_Measured) and (MissingMassSquared_Measured < 3.75)") # limit data to fit range
# overlay resolutions of kinematic variables estimated using MC truth
diffVariableInfos: tuple[tuple[str, str, tuple[int, float, float], tuple[float, float], str], ...] = ()
if True:
diffVariableInfos = (
("MissingProtonP", "TruthDeltaP", (400, -4, +4), (0, 0.7), "#it{p}_{miss}^{truth} #minus #it{p}_{miss}^{kin. fit} (GeV/#it{c})"),
("MissingProtonTheta", "TruthDeltaTheta", (200, -60, +60), (0, 20 ), "#it{#theta}_{miss}^{truth} #minus #it{#theta}_{miss}^{kin. fit} (deg)"),
("MissingProtonPhi", "TruthDeltaPhi", (360, -180, +180), (0, 70 ), "#it{#phi}_{miss}^{truth} #minus #it{#phi}_{miss}^{kin. fit} (deg)"),
)
for diffVariableInfo in diffVariableInfos:
args = diffVariableInfo[:3]
resPlotRange = diffVariableInfo[3]
diffVariableAxisTitle = diffVariableInfo[4]
dataToOverlay: list[tuple[str, ROOT.RDataFrame]] = []
for dataPeriod, data in inputData.items():
dataToOverlay.append((dataPeriod, data["MCbggen"]))
kwargs = {
"pdfDirName" : makeDirPath("./plots/MCbggen"),
"additionalFilter" : f'(NmbUnusedShowers == 0) && (NmbTruthTracks == 1) && (ThrownTopology.GetString() == "{trueTopology}")',
"diffVariableAxisTitle" : diffVariableAxisTitle,
"pdfFileNamePrefix" : f"{treeName}_",
}
overlayResolutions(dataToOverlay, *args, resBinningVariable = "BeamEnergy", resBinning = ( 90, 2.9, 11.9), resPlotRange = resPlotRange, **kwargs)
overlayResolutions(dataToOverlay, *args, resBinningVariable = "MissingProtonP", resBinning = (100, 0, 5 ), resPlotRange = resPlotRange, **kwargs)
overlayResolutions(dataToOverlay, *args, resBinningVariable = "MissingProtonTheta", resBinning = ( 56, 0, 70 ), resPlotRange = resPlotRange, **kwargs)
overlayResolutions(dataToOverlay, *args, resBinningVariable = "MissingProtonPhi", resBinning = ( 72, -180, +180 ), resPlotRange = resPlotRange, **kwargs)
# overlay resolutions of kinematic variables estimated using unused tracks
if True:
diffVariableInfos = (
("MissingProtonP", "UnusedDeltaP", (400, -4, +4), (0, 1), "#it{p}_{miss}^{unused} #minus #it{p}_{miss}^{kin. fit} (GeV/#it{c})"),
("MissingProtonTheta", "UnusedDeltaTheta", (200, -60, +60), (0, 30), "#it{#theta}_{miss}^{unused} #minus #it{#theta}_{miss}^{kin. fit} (deg)"),
("MissingProtonPhi", "UnusedDeltaPhi", (360, -180, +180), (0, 110), "#it{#phi}_{miss}^{unused} #minus #it{#phi}_{miss}^{kin. fit} (deg)"),
)
for diffVariableInfo in diffVariableInfos:
args = diffVariableInfo[:3]
resPlotRange = diffVariableInfo[3]
diffVariableAxisTitle = diffVariableInfo[4]
for dataType in ("MCbggen", "RD"):
dataToOverlay = []
for dataPeriod, data in inputData.items():
dataToOverlay.append((dataPeriod, data[dataType]))
kwargs = {
"pdfDirName" : makeDirPath(f"./plots/{dataType}"),
"additionalFilter" : "(NmbUnusedShowers == 0) && (NmbUnusedTracks == 1)",
"diffVariableAxisTitle" : diffVariableAxisTitle,
"pdfFileNamePrefix" : f"{treeName}_",
"pdfFileNameSuffix" : "_noUnusedShowers",
}
overlayResolutions(dataToOverlay, *args, resBinningVariable = "BeamEnergy", resBinning = ( 90, 2.9, 11.9), resPlotRange = resPlotRange, **kwargs)
overlayResolutions(dataToOverlay, *args, resBinningVariable = "MissingProtonP", resBinning = (100, 0, 5 ), resPlotRange = resPlotRange, **kwargs)
overlayResolutions(dataToOverlay, *args, resBinningVariable = "MissingProtonTheta", resBinning = ( 56, 0, 70 ), resPlotRange = resPlotRange, **kwargs)
overlayResolutions(dataToOverlay, *args, resBinningVariable = "MissingProtonPhi", resBinning = ( 72, -180, +180 ), resPlotRange = resPlotRange, **kwargs)
if True:
# overlay all periods for bggen MC and real data
dataSamplesToOverlay: dict[str, dict[str, Any]] = {}
if len(inputData) > 1:
for dataType in ("MCbggen", "RD"):
dataSamplesToOverlay = {}
for index, dataPeriod in enumerate(inputData.keys()):
dataSamplesToOverlay[dataPeriod] = {
"RDataFrame" : inputData[dataPeriod][dataType],
"normToThis" : True if index == 0 else False,
# define plot style
"SetLineColor" : getCbFriendlyRootColor(index, skipBlack = True),
"SetLineWidth" : 2,
}
makeKinematicPlotsOverlays(
dataSamples = dataSamplesToOverlay,
channel = treeName,
mesonSystemMassVarName = mesonSystemMassVarName,
mesonSystemMassVarTLatex = mesonSystemMassVarTLatex,
pdfDirName = makeDirPath(f"{pdfBaseDirName}/{dataType}"),
)
# overlay bggen MC and real data for each period
for dataPeriod in inputData.keys():
dataSamplesToOverlay = {
"bggen MC (scaled)" : {
"RDataFrame" : inputData[dataPeriod]["MCbggen"],
# define plot style
"SetLineColor" : ROOT.kGray,
"SetFillColor" : ROOT.kGray,
},
"Real Data" : {
"RDataFrame" : inputData[dataPeriod]["RD"],
"normToThis" : True,
},
}
makeKinematicPlotsOverlays(
dataSamples = dataSamplesToOverlay,
channel = treeName,
mesonSystemMassVarName = mesonSystemMassVarName,
mesonSystemMassVarTLatex = mesonSystemMassVarTLatex,
pdfDirName = makeDirPath(f"{pdfBaseDirName}/{dataPeriod}"),
)
# make Monte Carlo plots for each period
if True:
for dataPeriod in inputData.keys():
makeKinematicPlotsMc(
dataSample = inputData[dataPeriod]["MCbggen"],
channel = treeName,