-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfillHistosScouting.py
2000 lines (1943 loc) · 94.6 KB
/
fillHistosScouting.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
import os,sys,json
import argparse
from datetime import date
import ROOT
import numpy as np
from DataFormats.FWLite import Events, Handle
sys.path.append('utils')
import histDefinition
import math
import csv
ROOT.EnableImplicitMT(2)
user = os.environ.get("USER")
today= date.today().strftime("%b-%d-%Y")
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("--inDir", default="/ceph/cms/store/user/"+user+"/Run3ScoutingOutput/looperOutput_"+today, help="Choose input directory. Default: '/ceph/cms/store/user/"+user+"/Run3ScoutingOutput/looperOutput_"+today+"'")
parser.add_argument("--inSample", default="*", help="Choose sample; for all samples in input directory, choose '*'")
parser.add_argument("--inFile", default="*", help="Choose input file by index (for debug); for all files in input directory, choose '*'")
parser.add_argument("--outDir", default=os.environ.get("PWD")+"/outputHistograms_"+today, help="Choose output directory. Default: '"+os.environ.get("PWD")+"/outputHistograms_"+today+"'")
parser.add_argument("--outSuffix", default="", help="Choose output directory. Default: ''")
parser.add_argument("--condor", default=False, action="store_true", help="Run on condor")
parser.add_argument("--data", default=False, action="store_true", help="Process data")
parser.add_argument("--signal", default=False, action="store_true", help="Process signal")
parser.add_argument("--unblind", default=False, action="store_true", help="Unblind data")
parser.add_argument("--year", default="2022", help="Year to be processed. Default: 2022")
parser.add_argument("--weightMC", default=True, help="Indicate if MC is weighted")
parser.add_argument("--reweightFrom", default=-1, help="Indicate ctau of the sample")
parser.add_argument("--reweightTo", default=-1, help="Indicate ctau to reweight to")
parser.add_argument("--rooWeight", default="1.00", help="Weight to be used for RooDatasets and Signal Regions (It doesn't weight other histograms)")
parser.add_argument("--partialUnblinding", default=False, action="store_true", help="Process x% (default: x=50) of available data")
parser.add_argument("--partialUnblindingFraction", default="0.5", help="Fraction of available data to be processed")
parser.add_argument("--removeDuplicates", default=False, action="store_true", help="Check for and remove duplicates")
parser.add_argument("--splitIndex", default="-1", help="Split index")
parser.add_argument("--splitPace", default="250000", help="Split pace")
parser.add_argument("--dimuonMassSel", default=[], nargs="+", help="Selection on dimuon mass: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--dimuonMassSidebandSel", default=[], nargs="+", help="Selection on dimuon mass sidebands: first pair of values is left sideband, second (optional) is right sideband")
parser.add_argument("--dimuonPtSel", default=[], nargs="+", help="Selection on dimuon pT: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--dimuonIsoCatSel", default=-99, help="Selection on dimuon isolation category: 0 (non-iso), 1 (part-iso) or 2 (iso)")
parser.add_argument("--fourmuonMassSel", default=[], nargs="+", help="Selection on four-muon mass: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--fourmuonPtSel", default=[], nargs="+", help="Selection on four-muon pT: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--dimuonMassSelForFourMuon", default=[], nargs="+", help="Selection on dimuon mass in four-muon system: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--dimuonMassDiffSelForFourMuon", default=[], nargs="+", help="Selection on dimuon mass difference / mean in four-muon system: first (or only) value is *upper* cut, second (optional) value is *lower* cut")
parser.add_argument("--dimuonPtSelForFourMuon", default=[], nargs="+", help="Selection on dimuon pT in four-muon system: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--dimuonMassSelForFourMuonOSV", default=[], nargs="+", help="Selection on dimuon mass in four-muon system from overlapping SV: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--dimuonMassDiffSelForFourMuonOSV", default=[], nargs="+", help="Selection on dimuon mass difference / mean in four-muon system from overlapping SV: first (or only) value is *upper* cut, second (optional) value is *lower* cut")
parser.add_argument("--dimuonPtSelForFourMuonOSV", default=[], nargs="+", help="Selection on dimuon pT in four-muon system from overlapping SV: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--lxySel", default=[], nargs="+", help="Selection on lxy: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--lzSel", default=[], nargs="+", help="Selection on lz: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--lxySelForFourMuon", default=[], nargs="+", help="Selection on lxy: first (or only) value is lower cut, second (optional) value is upper cut")
parser.add_argument("--noMaterialVeto", default=False, action="store_true", help="Do not apply material vertex veto")
parser.add_argument("--noMuonIPSel", default=False, action="store_true", help="Do not apply selection on muon IP (Not applied at four-muon level)")
parser.add_argument("--noMuonHitSel", default=False, action="store_true", help="Do not apply selection on muon hits (Not applied at four-muon level)")
parser.add_argument("--noDiMuonAngularSel", default=False, action="store_true", help="Do not apply selection on dimuon angular variables")
parser.add_argument("--noFourMuonAngularSel", default=False, action="store_true", help="Do not apply selection on fourmuon angular variables")
parser.add_argument("--noFourMuonMassDiffSel", default=False, action="store_true", help="Do not apply selection on fourmuon invariant mass difference")
parser.add_argument("--noPreSel", default=False, action="store_true", help="Do not fill pre-selection/association histograms")
parser.add_argument("--noDiMuon", default=False, action="store_true", help="Do not fill dimuon histograms")
parser.add_argument("--noFourMuon", default=False, action="store_true", help="Do not fill four-muon histograms for four-muon systems")
parser.add_argument("--noFourMuonOSV", default=False, action="store_true", help="Do not fill four-muon histograms for four-muon systems from overlapping SVs")
parser.add_argument("--noSeed", default=[], nargs="+", help="Exclude L1 seeds from the acceptance")
parser.add_argument("--noHistos", default=False, action="store_true", help="Skip histogram filling")
parser.add_argument("--doGen", default=False, action="store_true", help="Fill generation information histograms")
args = parser.parse_args()
# Functions for selection
def applyDiMuonSelection(vec):
selected = True
if len(args.dimuonMassSel)>0:
selected = selected and (vec.M() > float(args.dimuonMassSel[0]))
if len(args.dimuonMassSel)>1:
selected = selected and (vec.M() < float(args.dimuonMassSel[1]))
if len(args.dimuonPtSel)>0:
selected = selected and (vec.Pt() > float(args.dimuonPtSel[0]))
if len(args.dimuonPtSel)>1:
selected = selected and (vec.Pt() < float(args.dimuonPtSel[1]))
if len(args.dimuonMassSidebandSel)>3:
selected = selected and ((vec.M() > float(args.dimuonMassSidebandSel[0]) and vec.M() < float(args.dimuonMassSidebandSel[1])) or
(vec.M() > float(args.dimuonMassSidebandSel[2]) and vec.M() < float(args.dimuonMassSidebandSel[3])))
return selected
def applyFourMuonSelection(vec):
selected = True
if len(args.fourmuonMassSel)>0:
selected = selected and (vec.M() > float(args.fourmuonMassSel[0]))
if len(args.fourmuonMassSel)>1:
selected = selected and (vec.M() < float(args.fourmuonMassSel[1]))
if len(args.fourmuonPtSel)>0:
selected = selected and (vec.Pt() > float(args.fourmuonPtSel[0]))
if len(args.fourmuonPtSel)>1:
selected = selected and (vec.Pt() < float(args.fourmuonPtSel[1]))
return selected
def applyDiMuonSelectionForFourMuon(vecf,vecs):
selected = True
if len(args.dimuonMassSelForFourMuon)>0:
selected = selected and (vecf.M() > float(args.dimuonMassSelForFourMuon[0])) and (vecs.M() > float(args.dimuonMassSelForFourMuon[0]))
if len(args.dimuonMassSelForFourMuon)>1:
selected = selected and (vecf.M() < float(args.dimuonMassSelForFourMuon[1])) and (vecs.M() < float(args.dimuonMassSelForFourMuon[1]))
if len(args.dimuonPtSelForFourMuon)>0:
selected = selected and (vecf.Pt() > float(args.dimuonPtSelForFourMuon[0])) and (vecs.Pt() > float(args.dimuonPtSelForFourMuon[0]))
if len(args.dimuonPtSelForFourMuon)>1:
selected = selected and (vecf.Pt() < float(args.dimuonPtSelForFourMuon[1])) and (vecs.Pt() < float(args.dimuonPtSelForFourMuon[1]))
if len(args.dimuonMassDiffSelForFourMuon)>0:
selected = selected and (abs(vecf.M()-vecs.M())*2.0/(vecf.M()+vecs.M()) < float(args.dimuonMassDiffSelForFourMuon[0]))
if len(args.dimuonMassDiffSelForFourMuon)>1:
selected = selected and (abs(vecf.M()-vecs.M())*2.0/(vecf.M()+vecs.M()) > float(args.dimuonMassDiffSelForFourMuon[1]))
return selected
def applyDiMuonSelectionForFourMuonOSV(vecf,vecs):
selected = True
if len(args.dimuonMassSelForFourMuonOSV)>0:
selected = selected and (vecf.M() > float(args.dimuonMassSelForFourMuonOSV[0])) and (vecs.M() > float(args.dimuonMassSelForFourMuonOSV[0]))
if len(args.dimuonMassSelForFourMuonOSV)>1:
selected = selected and (vecf.M() < float(args.dimuonMassSelForFourMuonOSV[1])) and (vecs.M() < float(args.dimuonMassSelForFourMuonOSV[1]))
if len(args.dimuonPtSelForFourMuonOSV)>0:
selected = selected and (vecf.Pt() > float(args.dimuonPtSelForFourMuonOSV[0])) and (vecs.Pt() > float(args.dimuonPtSelForFourMuonOSV[0]))
if len(args.dimuonPtSelForFourMuonOSV)>1:
selected = selected and (vecf.Pt() < float(args.dimuonPtSelForFourMuonOSV[1])) and (vecs.Pt() < float(args.dimuonPtSelForFourMuonOSV[1]))
if len(args.dimuonMassDiffSelForFourMuonOSV)>0:
selected = selected and (abs(vecf.M()-vecs.M())*2.0/(vecf.M()+vecs.M()) < float(args.dimuonMassDiffSelForFourMuonOSV[0]))
if len(args.dimuonMassDiffSelForFourMuonOSV)>1:
selected = selected and (abs(vecf.M()-vecs.M())*2.0/(vecf.M()+vecs.M()) > float(args.dimuonMassDiffSelForFourMuonOSV[1]))
return selected
def applyLxySelection(lxy):
selected = True
if len(args.lxySel)>0:
selected = selected and (lxy > float(args.lxySel[0]))
if len(args.lxySel)>1:
selected = selected and (lxy < float(args.lxySel[1]))
return selected
def applyLzSelection(lz):
selected = True
if len(args.lzSel)>0:
selected = selected and (lxy > float(args.lzSel[0]))
if len(args.lzSel)>1:
selected = selected and (lxy < float(args.lzSel[1]))
return selected
def applyFourMuonLxySelection(lxymin,lxymax):
selected = True
if len(args.lxySelForFourMuon)>0:
selected = selected and (lxymin > float(args.lxySel[0]))
if len(args.lxySelForFourMuon)>1:
selected = selected and (lxymax < float(args.lxySel[1]))
return selected
# Muon type
def muonType(isGlobal, isTracker, isStandAlone):
if isGlobal and isTracker:
return 0.5
elif isGlobal and not isTracker:
return 1.5
elif not isGlobal and isTracker:
return 2.5
elif not isGlobal and not isTracker and isStandAlone:
return 3.5
elif not isGlobal and not isTracker and not isStandAlone:
return 4.5
# Isolation category
def dimuonIsoCategory(iso1, pt1, iso2, pt2):
isocat = -99
#if (iso1>8.0 and iso2>8.0 ):
if ((iso1>8.0 and iso1/pt1 > 0.2) and (iso2>8.0 and iso2/pt2 > 0.2) ):
isocat = 0
elif ((iso1<8.0 or iso1/pt1 < 0.2) and (iso2<8.0 or iso2/pt2 < 0.2) ):
isocat = 2
else:
isocat = 1
return isocat
# Evaluate L1 with the possibility of excluding one
def evaluateSeeds(tree, seedList):
for seed in seedList:
if eval('tree.'+seed):
return True
return False
# Muon IP sign
def getIPSign(mphi, dmphi):
dxydir = ROOT.TVector3(-ROOT.TMath.Sin(mphi), ROOT.TMath.Cos(mphi), 0.0)
dmudir = ROOT.TVector3(ROOT.TMath.Cos(dmphi), ROOT.TMath.Sin(dmphi), 0.0)
if (dxydir*dmudir > 0):
return 1.0
else:
return -1.0
# Get Signal normalization weight
def getweight(era, ngen, frac=1.0, xsec=1000):
if era=="2022":
return frac*8.077046947*xsec/ngen
if era=="2022postEE":
return frac*26.982330931*xsec/ngen
if era=="2023":
return frac*17.060484313*xsec/ngen
if era=="2023BPix":
return frac*9.525199061*xsec/ngen
# Get closest from list
def getClosest(val, values):
iv = 0
for v,vv in enumerate(values):
if abs(val-vv) < abs(val-values[iv]):
iv = v
return iv, abs(val-values[iv])
#
def getClosestAngular(vals = [0,0,0], values_lxy = [], values_eta = [], values_phi = []):
iv = 0
ivt = ROOT.TVector3()
ivt.SetPtEtaPhi(values_lxy[0], values_eta[0], values_phi[0])
v1 = ROOT.TVector3()
v1.SetPtEtaPhi(vals[0], vals[1], vals[2])
for v in range(0, len(values_lxy)):
v2 = ROOT.TVector3()
v2.SetPtEtaPhi(values_lxy[v], values_eta[v], values_phi[v])
#if abs(val-vv) < abs(val-values[iv]):
if v1.DeltaR(v2) < v1.DeltaR(ivt):
iv = v
ivt = v2
return iv
# Get trigger SF (2022)
def getTriggerSF(subpt, lxy):
sf = 1.0
sfup = 1.0
sfdown = 1.0
if lxy > 0.0 and lxy < 0.2:
if subpt < 5.0:
sf = 1.12
sfup = 1.12 + 0.01
sfdown = 1.12 - 0.01
elif subpt > 5. and subpt < 10.:
sf = 0.996
sfup = 0.996 + 0.004
sfdown = 0.996 - 0.003
elif subpt > 10. and subpt < 15.:
sf = 0.974
sfup = 0.974 + 0.007
sfdown = 0.974 - 0.008
elif subpt > 15.0:
sf = 0.915
sfup = 0.915 + 0.008
sfdown = 0.915 - 0.008
elif lxy > 0.2 and lxy < 1.0:
if subpt < 5.0:
sf = 1.19
sfup = 1.19 + 0.04
sfdown = 1.19 - 0.05
elif subpt > 5. and subpt < 10.:
sf = 1.008
sfup = 1.008 + 0.007
sfdown = 1.008 - 0.008
elif subpt > 10. and subpt < 15.:
sf = 0.969
sfup = 0.969 + 0.006
sfdown = 0.969 - 0.008
elif subpt > 15.0:
sf = 0.96
sfup = 0.96 + 0.01
sfdown = 0.96 - 0.01
elif lxy > 1.0 and lxy < 2.4:
if subpt < 5.0:
sf = 1.32
sfup = 1.32 + 0.1
sfdown = 1.32 - 0.2
elif subpt > 5. and subpt < 10.:
sf = 1.03
sfup = 1.03 + 0.02
sfdown = 1.03 - 0.02
elif subpt > 10. and subpt < 15.:
sf = 0.986
sfup = 0.986 + 0.008
sfdown = 0.986 - 0.017
elif subpt > 15.0:
sf = 0.98
sfup = 0.98 + 0.01
sfdown = 0.98 - 0.04
elif lxy > 2.4 and subpt < 5.0:
sf = 1.00
sfup = 1.00 + 0.3
sfdown = 1.00 - 0.3
elif lxy > 2.4 and lxy < 3.1:
sf = 1.04
sfup = 1.04 + 0.01
sfdown = 1.04 - 0.02
elif lxy > 3.1 and lxy < 7.0:
sf = 1.02
sfup = 1.02 + 0.01
sfdown = 1.02 - 0.03
elif lxy > 7.0:
sf = 1.00
sfup = 1.00 + 0.05
sfdown = 1.00 - 0.05
return sf, sfup, sfdown
# Get Selection SF
def getSelectionSF(lxy):
sf = 1.0
sfup = 1.0
sfdown = 1.0
if lxy > 0.0 and lxy < 0.2:
sf = 1.10
sfup = 1.10 + 0.01
sfdown = 1.10 - 0.01
elif lxy > 0.2 and lxy < 1.0:
sf = 1.03
sfup = 1.03 + 0.02
sfdown = 1.03 - 0.02
elif lxy > 1.0 and lxy < 2.4:
sf = 1.02
sfup = 1.02 + 0.03
sfdown = 1.02 - 0.03
elif lxy > 2.4 and lxy < 3.1:
sf = 0.96
sfup = 0.96 + 0.06
sfdown = 0.96 - 0.06
elif lxy > 3.1 and lxy < 7.0:
sf = 0.86
sfup = 0.86 + 0.06
sfdown = 0.86 - 0.06
elif lxy > 7.0: # More bins to be added when efficiency (per category) is computed
sf = 1.0
sfup = 1.0 + 0.3
sfdown = 1.0 - 0.3
return sf, sfup, sfdown
## Running settings
indir = args.inDir.replace("/ceph/cms","")
outdir = args.outDir
if args.outSuffix!="":
outdir = outdir+"_"+args.outSuffix
if not os.path.exists(outdir):
os.makedirs(outdir)
applyMaterialVeto = not args.noMaterialVeto
applyMuonIPSel = not args.noMuonIPSel
applyDiMuonAngularSel = not args.noDiMuonAngularSel
applyMuonHitSel = not args.noMuonHitSel
applyFourMuonAngularSel = not args.noFourMuonAngularSel
applyFourMuonMassDiffSel = not args.noFourMuonMassDiffSel
isData = args.data
if "Data" in args.inSample:
isData = True
removeDuplicates = args.removeDuplicates
if not isData:
removeDuplicates = False
MUON_MASS = 0.10566
if args.signal:
isData = False
skimFraction = float(args.partialUnblindingFraction)
skimEvents = args.partialUnblinding
if not isData:
skimEvents = False
rndm_partialUnblinding = ROOT.TRandom3(42)
files = []
prependtodir = ""
if not args.condor:
prependtodir = "/ceph/cms"
else:
prependtodir = "davs://redirector.t2.ucsd.edu:1095"
if not args.condor:
if args.inFile!="*" and args.inSample!="*":
thisfile="output_%s_%s_%s.root"%(args.inSample,args.year,args.inFile)
if os.path.isfile("/ceph/cms%s/%s"%(indir,thisfile)):
files.append("%s%s/%s"%(prependtodir,indir,thisfile))
elif args.inSample!="*":
for f in os.listdir("/ceph/cms%s"%indir):
if ("output_%s_%s_"%(args.inSample,args.year) in f) and os.path.isfile("/ceph/cms%s/%s"%(indir,f)):
files.append("%s%s/%s"%(prependtodir,indir,f))
else:
for f in os.listdir("/ceph/cms%s"%indir):
if (args.year in f) and (".root" in f) and os.path.isfile("/ceph/cms%s/%s"%(indir,f)):
files.append("%s%s/%s"%(prependtodir,indir,f))
else:
os.system('xrdfs redirector.t2.ucsd.edu:1095 ls %s > filein.txt'%indir)
fin = open("filein.txt","r")
for f in fin.readlines():
f = f.strip("\n")
if args.inFile!="*" and args.inSample!="*":
thisfile="output_%s_%s_%s.root"%(args.inSample,args.year,args.inFile)
if thisfile in f:
files.append("%s/%s"%(prependtodir,thisfile))
elif args.inSample!="*":
if "output_%s_%s_"%(args.inSample,args.year) in f:
files.append("%s/%s"%(prependtodir,f))
else:
if (args.year in f) and (".root" in f):
files.append("%s/%s"%(prependtodir,f))
fin.close()
os.system('rm -f filein.txt')
print("Found {} files matching criteria".format(len(files)))
print(files)
index = int(args.splitIndex)
pace = int(args.splitPace)
# Inputs:
t = ROOT.TChain("tout")
for f in files:
t.Add(f)
# MC normalization (Need to integrate everything for all signals that we may produce)
ncounts = 1
efilter = 1.0
lumiweight = 1.0
sampleTag = args.inSample.replace('Signal_', '').split('_202')[0]
unblind_frac = 1.0 if args.unblind else 0.1
if not isData and args.weightMC and 'DileptonMinBias' not in args.inSample:
counts = ROOT.TH1F("totals", "", 1, 0, 1)
print("Simulations: Getting counts")
if "HTo2ZdTo2mu2x" in sampleTag:
for _,f in enumerate(files):
if not args.condor:
f_ = ROOT.TFile.Open(f.replace('davs://redirector.t2.ucsd.edu:1095//', '/ceph/cms/'))
else:
f_ = ROOT.TFile.Open(f)
h_ = f_.Get("counts").Clone("Clone_{}".format(_))
counts.Add(h_)
f_.Close()
ncounts = counts.GetBinContent(1)
with open('data/hahm-request.csv') as mcinfo:
reader = csv.reader(mcinfo, delimiter=',')
for row in reader:
if sampleTag in row[0]:
efilter = float(row[-1])
break
if 'BToPhi' in sampleTag:
for _,f in enumerate(files):
if not args.condor:
f_ = ROOT.TFile.Open(f.replace('davs://redirector.t2.ucsd.edu:1095//', '/ceph/cms/'))
else:
f_ = ROOT.TFile.Open(f)
h_ = f_.Get("counts").Clone("Clone_{}".format(_))
counts.Add(h_)
f_.Close()
ncounts = counts.GetBinContent(1)
with open('data/BToPhi-request.csv') as mcinfo:
reader = csv.reader(mcinfo, delimiter=',')
for row in reader:
if sampleTag in row[0]:
efilter = float(row[-1])
break
if "ScenB2" in sampleTag:
for _,f in enumerate(files):
if not args.condor:
f_ = ROOT.TFile.Open(f.replace('davs://redirector.t2.ucsd.edu:1095//', '/ceph/cms/'))
else:
f_ = ROOT.TFile.Open(f)
h_ = f_.Get("counts").Clone("Clone_{}".format(_))
counts.Add(h_)
f_.Close()
ncounts = counts.GetBinContent(1)
efilter = 1.0
if "ScenarioB1" in sampleTag:
for _,f in enumerate(files):
if not args.condor:
f_ = ROOT.TFile.Open(f.replace('davs://redirector.t2.ucsd.edu:1095//', '/ceph/cms/'))
else:
f_ = ROOT.TFile.Open(f)
h_ = f_.Get("counts").Clone("Clone_{}".format(_))
counts.Add(h_)
f_.Close()
ncounts = counts.GetBinContent(1)
efilter = 1.0
if "2022postEE" in f:
lumiweight = getweight("2022postEE", ncounts/efilter, unblind_frac)
elif "2022" in f:
lumiweight = getweight("2022", ncounts/efilter, unblind_frac)
elif "2023" in f:
lumiweight = getweight("2023", ncounts/efilter, unblind_frac)
elif "2023BPix" in f:
lumiweight = getweight("2023BPix", ncounts/efilter, unblind_frac)
if "ScenB1" in sampleTag:
ncounts = 300000.0
efilter = 1.0
lumiweight = 0.1*(8.077046947 + 26.982330931)*1000.0/(ncounts/efilter)
print("Total number of counts: {}".format(ncounts))
print("Filter efficiency (generation): {}".format(efilter))
print("Lumiweight: {}".format(lumiweight))
# Histograms:
h1d = dict()
variable1d = dict()
#
h2d = dict()
variable2d = dict()
#
h1d,variable1d,h2d,variable2d = histDefinition.histInitialization(not(args.noPreSel),not(args.noDiMuon),not(args.noFourMuon),not(args.noFourMuonOSV))
if args.noHistos:
for key in h1d.keys():
h1d[key] = []
for key in h2d.keys():
h2d[key] = []
###
for cat in h1d.keys():
for h in h1d[cat]:
if isData:
h.Sumw2(ROOT.kFALSE)
else:
h.Sumw2()
for cat in h2d.keys():
for h in h2d[cat]:
if isData:
h.Sumw2(ROOT.kFALSE)
else:
h.Sumw2()
###
L1seeds = []
branch_list = t.GetListOfBranches()
for branch in branch_list:
if branch.GetName().startswith('L1'):
L1seeds.append(branch.GetName())
effL1seeds = [s for s in L1seeds if s not in args.noSeed]
print('List of L1 seeds: ', L1seeds)
if args.noSeed:
print('but excluding: ', args.noSeed)
###
# Lifetime-reweighting
reweightFrom = float(args.reweightFrom)
reweightTo = float(args.reweightTo)
elist = [] # for duplicate removal
print("Starting loop over %d events"%t.GetEntries())
firste = 0
laste = t.GetEntries()
print(args.inSample, laste)
if index>=0:
firste = index*pace
laste = min((index+1)*pace,t.GetEntries())
if firste >= t.GetEntries():
exit()
## Init RooDataSets
# Dimuon binning
lxybins = [0.0, 0.2, 1.0, 2.4, 3.1, 7.0, 11.0, 16.0, 70.0]
lxystrs = [str(l).replace('.', 'p') for l in lxybins]
lxybinlabel = ["lxy{}to{}".format(lxystrs[l], lxystrs[l+1]) for l in range(0, len(lxystrs)-1)]
ptcut = 25. # Tried 25, 50 and 100
dphisvcut = 0.02 # Last Run 2 value
# Variables
mfit = ROOT.RooRealVar("mfit", "mfit", 0.4, 140.0)
m4fit = ROOT.RooRealVar("m4fit", "m4fit", 0.4, 140.0)
roow = ROOT.RooRealVar("roow", "roow", -10000.0, 10000.0)
roow_trg_up = ROOT.RooRealVar("roow_trg_up", "roow_trg_up", -10000.0, 10000.0)
roow_trg_down = ROOT.RooRealVar("roow_trg_down", "roow_trg_down", -10000.0, 10000.0)
roow_sel_up = ROOT.RooRealVar("roow_sel_up", "roow_sel_up", -10000.0, 10000.0)
roow_sel_down = ROOT.RooRealVar("roow_sel_down", "roow_sel_down", -10000.0, 10000.0)
roow4 = ROOT.RooRealVar("roow", "roow", -10000.0, 10000.0)
roow4_trg_up = ROOT.RooRealVar("roow_trg_up", "roow_trg_up", -10000.0, 10000.0)
roow4_trg_down = ROOT.RooRealVar("roow_trg_down", "roow_trg_down", -10000.0, 10000.0)
roow4_sel_up = ROOT.RooRealVar("roow_sel_up", "roow_sel_up", -10000.0, 10000.0)
roow4_sel_down = ROOT.RooRealVar("roow_sel_down", "roow_sel_down", -10000.0, 10000.0)
roods = {}
roods_trg_up = {}
roods_trg_down = {}
roods_sel_up = {}
roods_sel_down = {}
catmass = {}
dbins = []
rooweight = float(args.rooWeight) # Weight for RooDataset
# Categories
dbins.append("FourMu_sep") # 4mu, multivertex
dbins.append("FourMu_osv") # 4mu, 4mu-vertex
dbins.append("Dimuon_full_inclusive") # Dimuons excluded from categorization
for label in lxybinlabel:
dbins.append("Dimuon_"+label+"_inclusive")
dbins.append("Dimuon_"+label+"_iso0_ptlow")
dbins.append("Dimuon_"+label+"_iso0_pthigh")
dbins.append("Dimuon_"+label+"_iso1_ptlow")
dbins.append("Dimuon_"+label+"_iso1_pthigh")
dbins.append("Dimuon_"+label+"_non-pointing") # non-pointing
dbins.append("Dimuon_excluded") # Dimuons excluded from categorization
#
for dbin in dbins:
dname = "d_" + dbin
if 'Dimuon' in dname:
catmass[dbin] = ROOT.TH1F(dname + "_rawmass","; m_{#mu#mu} [GeV]; Events / 0.01 GeV",15000, 0., 150.)
roods[dbin] = ROOT.RooDataSet(dname,dname,ROOT.RooArgSet(mfit,roow),"roow")
roods_trg_up[dbin] = ROOT.RooDataSet(dname + "_trg_up",dname,ROOT.RooArgSet(mfit,roow_trg_up),"roow_trg_up")
roods_trg_down[dbin] = ROOT.RooDataSet(dname + "_trg_down",dname,ROOT.RooArgSet(mfit,roow_trg_down),"roow_trg_down")
roods_sel_up[dbin] = ROOT.RooDataSet(dname + "_sel_up",dname,ROOT.RooArgSet(mfit,roow_sel_up),"roow_sel_up")
roods_sel_down[dbin] = ROOT.RooDataSet(dname + "_sel_down",dname,ROOT.RooArgSet(mfit,roow_sel_down),"roow_sel_down")
else:
catmass[dbin] = ROOT.TH1F(dname + "_rawmass","; m_{4#mu} [GeV]; Events / 0.01 GeV",15000, 0., 150.)
roods[dbin] = ROOT.RooDataSet(dname,dname,ROOT.RooArgSet(m4fit,roow4),"roow")
roods_trg_up[dbin] = ROOT.RooDataSet(dname + "_trg_up",dname,ROOT.RooArgSet(m4fit,roow4_trg_up),"roow_trg_up")
roods_trg_down[dbin] = ROOT.RooDataSet(dname + "_trg_down",dname,ROOT.RooArgSet(m4fit,roow4_trg_down),"roow_trg_down")
roods_sel_up[dbin] = ROOT.RooDataSet(dname + "_sel_up",dname,ROOT.RooArgSet(m4fit,roow4_sel_up),"roow_sel_up")
roods_sel_down[dbin] = ROOT.RooDataSet(dname + "_sel_down",dname,ROOT.RooArgSet(m4fit,roow4_sel_down),"roow_sel_down")
print("From event %d to event %d"%(firste,laste))
for e in range(firste,laste):
t.GetEntry(e)
#if e%1000==0:
# print("At entry %d"%e)
if len(args.noSeed) > 0:
passL1 = evaluateSeeds(t, effL1seeds)
if not passL1:
continue
if removeDuplicates:
# Run, event number & lumisection
eid = t.evtn
run = t.run
lumi = t.lumi
if (run,lumi,eid) in elist:
continue
else:
elist.append((run,lumi,eid))
if skimEvents and skimFraction>0.0:
if rndm_partialUnblinding.Rndm() > skimFraction:
continue
### As advised in LUM POG TWiki
### (https://twiki.cern.ch/twiki/bin/view/CMS/LumiRecommendationsRun3),
### exclude runs 359571 + 359661
if isData and t.run==359571 or t.run==359661:
continue
# Event info
for h in h1d["event"]:
tn = h.GetName()
h.Fill(eval(variable1d[h.GetName()]), lumiweight)
# Gen info
dmugen = []
dmumot = []
if not isData:
nGEN = len(t.GenPart_pdgId)
for i in range(nGEN):
if abs(t.GenPart_pdgId[i]) != 13:
continue
isResonance = False
for j in range(i+1, nGEN):
if i==j:
continue
if t.GenPart_motherIndex[i] != t.GenPart_motherIndex[j] or t.GenPart_pdgId[i]*t.GenPart_pdgId[j] > 0:
continue
dmugen.append(i)
dmugen.append(j)
isResonance = True
for k in range(0, nGEN):
if t.GenPart_index[k] == t.GenPart_motherIndex[i]:
gvec = ROOT.TLorentzVector()
gvec.SetPtEtaPhiM(t.GenPart_pt[k], t.GenPart_eta[k], t.GenPart_phi[k], t.GenPart_m[k])
dmumot.append(gvec)
break
if isResonance:
for h in h1d["genmu"]:
tn = h.GetName()
h.Fill(eval(variable1d[h.GetName()]), lumiweight)
for g,gp in enumerate(dmumot):
lxygen = t.GenPart_lxy[dmugen[2*g]]
if t.GenPart_motherPdgId[dmugen[2*g]]==443:
for h in h1d["jpsi"]:
tn = h.GetName()
h.Fill(eval(variable1d[h.GetName()]), lumiweight)
for h in h1d["llp"]:
tn = h.GetName()
h.Fill(eval(variable1d[h.GetName()]), lumiweight)
# Identify the LLPs
LLPs = []
LLPs_lxy = []
LLPs_ct = []
LLPs_eta = []
LLPs_phi = []
if (reweightTo > 0 and reweightFrom > 0):
for i in range(0, len(t.GenPart_pdgId)):
if t.GenPart_pdgId[i] not in [1023]:
continue
for j in range(0, len(t.GenPart_pdgId)):
if (t.GenPart_pdgId[j]==13 and t.GenPart_motherIndex[j]==t.GenPart_index[i]):
LLPs.append(i)
LLPs_lxy.append(t.GenPart_lxy[j]) # from the muon
LLPs_eta.append(t.GenPart_eta[i])
LLPs_phi.append(t.GenPart_phi[i])
LLPs_ct.append(t.GenPart_ct[i])
break
# Loop over SVs
nSV = len(t.SV_index)
if nSV<1:
continue
nSVsel = 0
for v in range(nSV):
if args.noPreSel:
break
if not t.SV_selected[v]:
continue
if applyMaterialVeto and (t.SV_onModuleWithinUnc[v] or (abs(t.SV_minDistanceFromDet_x[v]) < 0.81 and abs(t.SV_minDistanceFromDet_y[v]) < 3.24 and abs(t.SV_minDistanceFromDet_z[v]) < 0.0145)):
continue
nSVsel = nSVsel+1
lxy = t.SV_lxy[v]
for h in h1d["svsel"]:
tn = h.GetName()
h.Fill(eval(variable1d[h.GetName()]), lumiweight)
for h in h2d["svsel"]:
tn = h.GetName()
h.Fill(eval(variable2d[h.GetName()][0]),eval(variable2d[h.GetName()][1]), lumiweight)
nSVs = nSVsel
for h in h1d["nsvsel"]:
if args.noPreSel:
break
tn = h.GetName()
h.Fill(eval(variable1d[h.GetName()]), lumiweight)
# Loop over muons
nMu = len(t.Muon_selected)
nMuSel = 0
nMuAss = 0
nMuAssOverlap = 0
muselidxs = []
for m in range(nMu):
if not t.Muon_selected[m]:
continue
nMuSel = nMuSel+1
if t.Muon_bestAssocSVOverlapIdx[m]>-1:
nMuAss = nMuAss+1
nMuAssOverlap = nMuAssOverlap+1
elif t.Muon_bestAssocSVIdx[m]>-1:
nMuAss = nMuAss+1
else:
continue
muselidxs.append(m)
if args.noPreSel:
continue
for h in h1d["muon"]:
if args.noPreSel:
break
tn = h.GetName()
h.Fill(eval(variable1d[h.GetName()]), lumiweight)
for h in h2d["muon"]:
if args.noPreSel:
break
tn = h.GetName()
h.Fill(eval(variable2d[h.GetName()][0]),eval(variable2d[h.GetName()][1]), lumiweight)
for h in h1d["nmuon"]:
if args.noPreSel:
break
tn = h.GetName()
h.Fill(eval(variable1d[h.GetName()]), lumiweight)
# Select events witb at least two muons associated to a SV
if nMuAss<2:
continue
# Muon pairing
dmuvec = []
dmu_muvecdp = []
dmuidxs = []
svvec = []
svidx = []
dmuvec_osv = []
dmu_muvecdp_osv = []
dmuidxs_osv = []
osvvec = []
osvidx = []
qmuvec_osv = []
qmuidxs_osv = []
qmuidxs_osv_sel = []
qmu_dmuvec_osv = []
qmu_muvecdp_osv = []
osvvec_qmu = []
osvidx_qmu = []
qmuvec = []
qmuidxs = []
qmuidxs_sel = []
qmuidxsminlxy = []
qmuidxsmaxlxy = []
qmu_muvecdp = []
qmu_muvecdpminlxy = []
qmu_muvecdpmaxlxy = []
qmu_dmuvecminlxy = []
qmu_dmuvecmaxlxy = []
qmu_dmuvecdpminlxy = []
qmu_dmuvecdpmaxlxy = []
svvecminlxy_qmu = []
svidxminlxy_qmu = []
svvecmaxlxy_qmu = []
svidxmaxlxy_qmu = []
for m in muselidxs:
chg = t.Muon_ch[m]
ovidx = -1
vidx = -1
ovpos = -1
vpos = -1
# First, identify muons from overlapping SVs
if t.Muon_bestAssocSVOverlapIdx[m]>-1:
if applyMaterialVeto and (t.SV_onModuleWithinUnc[t.SVOverlap_vtxIdxs[t.Muon_bestAssocSVOverlapIdx[m]][0]] or (abs(t.SV_minDistanceFromDet_x[t.SVOverlap_vtxIdxs[t.Muon_bestAssocSVOverlapIdx[m]][0]]) < 0.81 and abs(t.SV_minDistanceFromDet_y[t.SVOverlap_vtxIdxs[t.Muon_bestAssocSVOverlapIdx[m]][0]]) < 3.24 and abs(t.SV_minDistanceFromDet_z[t.SVOverlap_vtxIdxs[t.Muon_bestAssocSVOverlapIdx[m]][0]]) < 0.0145)):
continue
ovidx = t.Muon_bestAssocSVOverlapIdx[m]
ovpos = ovidx
# Then, identify muons from non-overlapping SVs
elif t.Muon_bestAssocSVIdx[m]>-1:
vidx = t.Muon_bestAssocSVIdx[m]
for v in range(len(t.SV_index)):
if applyMaterialVeto and (t.SV_onModuleWithinUnc[v] or (abs(t.SV_minDistanceFromDet_x[v]) < 0.81 and abs(t.SV_minDistanceFromDet_y[v]) < 3.24 and abs(t.SV_minDistanceFromDet_z[v]) < 0.0145)):
continue
if t.SV_index[v]==vidx:
vpos = v
break
# Loop over muons, and do pairing
for mm in muselidxs:
if mm==m:
continue
if abs(chg+t.Muon_ch[mm])>0:
continue
# First, identify muon pairs from overlapping SVs
if ovidx>-1 and t.Muon_bestAssocSVOverlapIdx[mm]==ovidx and ovpos>-1:
if not (m in dmuidxs_osv or mm in dmuidxs_osv):
dmuvec_osv.append(t.Muon_vec[m])
dmuvec_osv[len(dmuvec_osv)-1] = dmuvec_osv[len(dmuvec_osv)-1] + t.Muon_vec[mm]
dmuidxs_osv.append(m)
dmuidxs_osv.append(mm)
dmu_muvecdp_osv.append(ROOT.TLorentzVector())
dmu_muvecdp_osv[-1].SetPtEtaPhiM(t.Muon_pt[m],t.Muon_eta[m],t.Muon_phi[m],MUON_MASS)
dmu_muvecdp_osv.append(ROOT.TLorentzVector())
dmu_muvecdp_osv[-1].SetPtEtaPhiM(t.Muon_pt[mm],t.Muon_eta[mm],t.Muon_phi[mm],MUON_MASS)
osvvec.append(ROOT.TVector3())
osvvec[len(osvvec)-1].SetXYZ(t.SVOverlap_x[ovpos]-t.PV_x, t.SVOverlap_y[ovpos]-t.PV_y, t.SVOverlap_z[ovpos]-t.PV_z)
osvidx.append(ovpos)
# Then, identify muon pairs from non-overlapping SVs
elif vidx>-1 and t.Muon_bestAssocSVIdx[mm]==vidx and vpos>-1:
if not (m in dmuidxs or mm in dmuidxs):
dmuvec.append(t.Muon_vec[m])
dmuvec[len(dmuvec)-1] = dmuvec[len(dmuvec)-1] + t.Muon_vec[mm]
dmuidxs.append(m)
dmuidxs.append(mm)
dmu_muvecdp.append(ROOT.TLorentzVector())
dmu_muvecdp[-1].SetPtEtaPhiM(t.Muon_pt[m],t.Muon_eta[m],t.Muon_phi[m],MUON_MASS)
dmu_muvecdp.append(ROOT.TLorentzVector())
dmu_muvecdp[-1].SetPtEtaPhiM(t.Muon_pt[mm],t.Muon_eta[mm],t.Muon_phi[mm],MUON_MASS)
svvec.append(ROOT.TVector3())
svvec[len(svvec)-1].SetXYZ(t.SV_x[vpos]-t.PV_x, t.SV_y[vpos]-t.PV_y, t.SV_z[vpos]-t.PV_z)
svidx.append(vpos)
# If multiple muon pairs from overlapping SVs are found, create four-muon system
if len(dmuidxs_osv)>2 and float(len(dmuidxs_osv))/float(len(set(osvidx)))>2:
for m in range(len(dmuidxs_osv)):
if m%2>0:
continue
if len(qmuidxs_osv)>3:
break
for mm in range(m+2,len(dmuidxs_osv)):
if mm%2>0:
continue
if len(qmuidxs_osv)>3:
break
if osvidx[int(m/2)]==osvidx[int(mm/2)]:
if (dmuidxs_osv[m] in qmuidxs_osv) or (dmuidxs_osv[mm] in qmuidxs_osv):
continue
else:
qmuidxs_osv.append(dmuidxs_osv[m])
qmuidxs_osv.append(dmuidxs_osv[m+1])
qmuidxs_osv.append(dmuidxs_osv[mm])
qmuidxs_osv.append(dmuidxs_osv[mm+1])
qmu_muvecdp_osv.append(dmu_muvecdp_osv[m])
qmu_muvecdp_osv.append(dmu_muvecdp_osv[m+1])
qmu_muvecdp_osv.append(dmu_muvecdp_osv[mm])
qmu_muvecdp_osv.append(dmu_muvecdp_osv[mm+1])
qmuvec_osv.append(dmuvec_osv[int(m/2)])
qmuvec_osv[len(qmuvec_osv)-1] = qmuvec_osv[len(qmuvec_osv)-1]+dmuvec_osv[int(mm/2)]
qmu_dmuvec_osv.append(dmuvec_osv[int(m/2)])
qmu_dmuvec_osv.append(dmuvec_osv[int(mm/2)])
osvvec_qmu.append(ROOT.TVector3())
osvvec_qmu[len(osvvec_qmu)-1].SetXYZ(t.SVOverlap_x[osvidx[int(m/2)]]-t.PV_x, t.SVOverlap_y[osvidx[int(m/2)]]-t.PV_y, t.SVOverlap_z[osvidx[int(m/2)]]-t.PV_z)
osvidx_qmu.append(osvidx[int(m/2)])
dmuidxs_all = dmuidxs_osv+dmuidxs
dmuvec_all = dmuvec_osv+dmuvec
dmu_muvecdp_all = dmu_muvecdp_osv+dmu_muvecdp
svidx_all = osvidx+svidx
svvec_all = osvvec+svvec
# If multiple muon pairs are found not from overlapping SVs, create four-muon system from non-overlapping SVs
if len(dmuidxs_all)>=4:
for m in range(len(dmuidxs_all)):
if m%2>0:
continue
if len(qmuidxs)>3:
break
if m in dmuidxs_osv and m in qmuidxs_osv and len(qmuidxs_osv)>3:
continue
for mm in range(m+2,len(dmuidxs_all)):
if mm%2>0:
continue
if len(qmuidxs)>3:
break
if mm in dmuidxs_osv and mm in qmuidxs_osv and len(qmuidxs_osv)>3:
continue
if svidx_all[int(m/2)] in osvidx_qmu or svidx_all[int(mm/2)] in osvidx_qmu:
continue
else:
qmuidxs.append(dmuidxs_all[m])
qmuidxs.append(dmuidxs_all[m+1])
qmuidxs.append(dmuidxs_all[mm])
qmuidxs.append(dmuidxs_all[mm+1])
qmu_muvecdp.append(dmu_muvecdp_all[m])
qmu_muvecdp.append(dmu_muvecdp_all[m+1])
qmu_muvecdp.append(dmu_muvecdp_all[mm])
qmu_muvecdp.append(dmu_muvecdp_all[mm+1])
qmuvec.append(dmuvec_all[int(m/2)])
qmuvec[len(qmuvec)-1] = qmuvec[len(qmuvec)-1]+dmuvec_all[int(mm/2)]
if svvec_all[int(m/2)].Perp() < svvec_all[int(mm/2)].Perp():
svvecminlxy_qmu.append(svvec_all[int(m/2)])
svidxminlxy_qmu.append(svidx_all[int(m/2)])
qmu_dmuvecminlxy.append(dmuvec_all[int(m/2)])
svvecmaxlxy_qmu.append(svvec_all[int(mm/2)])
svidxmaxlxy_qmu.append(svidx_all[int(mm/2)])
qmu_dmuvecmaxlxy.append(dmuvec_all[int(mm/2)])
qmu_dmuvecdpminlxy.append(dmu_muvecdp_all[m]+dmu_muvecdp_all[m+1])
qmu_dmuvecdpmaxlxy.append(dmu_muvecdp_all[mm]+dmu_muvecdp_all[mm+1])
qmuidxsminlxy.append(dmuidxs_all[m])
qmuidxsminlxy.append(dmuidxs_all[m+1])
qmuidxsmaxlxy.append(dmuidxs_all[mm])
qmuidxsmaxlxy.append(dmuidxs_all[mm+1])
qmu_muvecdpminlxy.append(dmu_muvecdp_all[m])
qmu_muvecdpminlxy.append(dmu_muvecdp_all[m+1])
qmu_muvecdpmaxlxy.append(dmu_muvecdp_all[mm])
qmu_muvecdpmaxlxy.append(dmu_muvecdp_all[mm+1])
else:
svvecminlxy_qmu.append(svvec_all[int(mm/2)])
svidxminlxy_qmu.append(svidx_all[int(mm/2)])
qmu_dmuvecminlxy.append(dmuvec_all[int(mm/2)])
svvecmaxlxy_qmu.append(svvec_all[int(m/2)])
svidxmaxlxy_qmu.append(svidx_all[int(m/2)])
qmu_dmuvecmaxlxy.append(dmuvec_all[int(m/2)])
qmu_dmuvecdpminlxy.append(dmu_muvecdp_all[mm]+dmu_muvecdp_all[mm+1])
qmu_dmuvecdpmaxlxy.append(dmu_muvecdp_all[m]+dmu_muvecdp_all[m+1])
qmuidxsminlxy.append(dmuidxs_all[mm])
qmuidxsminlxy.append(dmuidxs_all[mm+1])
qmuidxsmaxlxy.append(dmuidxs_all[m])
qmuidxsmaxlxy.append(dmuidxs_all[m+1])
qmu_muvecdpminlxy.append(dmu_muvecdp_all[mm])
qmu_muvecdpminlxy.append(dmu_muvecdp_all[mm+1])
qmu_muvecdpmaxlxy.append(dmu_muvecdp_all[m])
qmu_muvecdpmaxlxy.append(dmu_muvecdp_all[m+1])
### Scan analysis initialization
# Cat selection:
filledcat4musep = False
filledcat4muosv = False
filledcat2mu = False
# Apply selections and fill histograms for four-muon systems from non-overlapping SVs
selqmusvidxs = []
selqmuvecs = []
mindrmm, mindpmm, mindemm, mindedpmm, mina3dmm = 1e6, 1e6, 1e6, 1e6, 1e6
maxdrmm, maxdpmm, maxdemm, maxdedpmm, maxa3dmm = -1., -1., -1., -1., -1.
mindrmmu, mindpmmu, mindemmu, mindedpmmu, mina3dmmu = 1e6, 1e6, 1e6, 1e6, 1e6
maxdrmmu, maxdpmmu, maxdemmu, maxdedpmmu, maxa3dmmu = -1., -1., -1., -1., -1.
for vn,v in enumerate(qmuvec):
if args.noFourMuon:
break
if not applyDiMuonSelectionForFourMuon(qmu_dmuvecminlxy[vn], qmu_dmuvecmaxlxy[vn]):
continue
if not applyFourMuonSelection(v):
continue
minlxy = svvecminlxy_qmu[vn].Perp()
maxlxy = svvecmaxlxy_qmu[vn].Perp()
minl3d = svvecminlxy_qmu[vn].Mag()
maxl3d = svvecmaxlxy_qmu[vn].Mag()
if not applyFourMuonLxySelection(minlxy,maxlxy):
continue
qmuidxs_sel.append(qmuidxs[vn*4])
qmuidxs_sel.append(qmuidxs[vn*4+1])
qmuidxs_sel.append(qmuidxs[vn*4+2])
qmuidxs_sel.append(qmuidxs[vn*4+3])
selqmusvidxs.append(svidxminlxy_qmu[vn])
selqmusvidxs.append(svidxmaxlxy_qmu[vn])
selqmuvecs.append(vn)
mass = v.M()
minmass = min(qmu_dmuvecminlxy[vn].M(), qmu_dmuvecmaxlxy[vn].M())
maxmass = max(qmu_dmuvecminlxy[vn].M(), qmu_dmuvecmaxlxy[vn].M())
avgmass = 0.5*(minmass+maxmass)
reldmass= (maxmass-minmass)/avgmass
pt = v.Pt()
minpt = min(qmu_dmuvecminlxy[vn].Pt(), qmu_dmuvecmaxlxy[vn].Pt())
maxpt = max(qmu_dmuvecminlxy[vn].Pt(), qmu_dmuvecmaxlxy[vn].Pt())
nhitsbeforesvtotal = t.Muon_nhitsbeforesv[qmuidxs[vn*4]] + t.Muon_nhitsbeforesv[qmuidxs[vn*4+1]] + t.Muon_nhitsbeforesv[qmuidxs[vn*4+2]] + t.Muon_nhitsbeforesv[qmuidxs[vn*4+3]]
ptlist = [t.Muon_pt[qmuidxs[vn*4]], t.Muon_pt[qmuidxs[vn*4+1]], t.Muon_pt[qmuidxs[vn*4+2]], t.Muon_pt[qmuidxs[vn*4+3]]]
ptlist.sort(reverse=True)
subpt = ptlist[1]
for m in range(vn*4,vn*4+4):
if not m%2==0:
continue
drmm = t.Muon_vec[qmuidxs[m]].DeltaR(t.Muon_vec[qmuidxs[m+1]])
dpmm = abs(t.Muon_vec[qmuidxs[m]].DeltaPhi(t.Muon_vec[qmuidxs[m+1]]))
demm = abs(t.Muon_vec[qmuidxs[m]].Eta()-t.Muon_vec[qmuidxs[m+1]].Eta())
dedpmm = 1e6
if dpmm>0.0: