/
eventSelection.cxx
705 lines (633 loc) · 35.3 KB
/
eventSelection.cxx
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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Framework/ConfigParamSpec.h"
#include "Framework/runDataProcessing.h"
#include "Framework/AnalysisTask.h"
#include "Framework/AnalysisDataModel.h"
#include "Common/DataModel/EventSelection.h"
#include "Common/CCDB/EventSelectionParams.h"
#include "Common/CCDB/TriggerAliases.h"
#include "CCDB/BasicCCDBManager.h"
#include "CommonConstants/LHCConstants.h"
#include "Framework/HistogramRegistry.h"
#include "DataFormatsFT0/Digit.h"
#include "DataFormatsParameters/GRPLHCIFData.h"
#include "DataFormatsParameters/GRPECSObject.h"
#include "ITSMFTBase/DPLAlpideParam.h"
#include "TH1D.h"
using namespace o2;
using namespace o2::framework;
using namespace o2::aod::evsel;
using BCsWithRun2InfosTimestampsAndMatches = soa::Join<aod::BCs, aod::Run2BCInfos, aod::Timestamps, aod::Run2MatchedToBCSparse>;
using BCsWithRun3Matchings = soa::Join<aod::BCs, aod::Timestamps, aod::Run3MatchedToBCSparse>;
using BCsWithBcSelsRun2 = soa::Join<aod::BCs, aod::Timestamps, aod::BcSels, aod::Run2BCInfos, aod::Run2MatchedToBCSparse>;
using BCsWithBcSelsRun3 = soa::Join<aod::BCs, aod::Timestamps, aod::BcSels>;
using FullTracksIU = soa::Join<aod::TracksIU, aod::TracksExtra>;
struct BcSelectionTask {
Produces<aod::BcSels> bcsel;
Service<o2::ccdb::BasicCCDBManager> ccdb;
HistogramRegistry histos{"Histos", {}, OutputObjHandlingPolicy::AnalysisObject};
Configurable<int> confTriggerBcShift{"triggerBcShift", 999, "set to 294 for apass2/apass3 in LHC22o-t"};
Configurable<int> confITSROFrameStartBorderMargin{"ITSROFrameStartBorderMargin", -1, "Number of bcs at the start of ITS RO Frame border. Take from CCDB if -1"};
Configurable<int> confITSROFrameEndBorderMargin{"ITSROFrameEndBorderMargin", -1, "Number of bcs at the end of ITS RO Frame border. Take from CCDB if -1"};
Configurable<int> confTimeFrameStartBorderMargin{"TimeFrameStartBorderMargin", -1, "Number of bcs to cut at the start of the Time Frame. Take from CCDB if -1"};
Configurable<int> confTimeFrameEndBorderMargin{"TimeFrameEndBorderMargin", -1, "Number of bcs to cut at the end of the Time Frame. Take from CCDB if -1"};
int lastRunNumber = -1;
int64_t bcSOR = -1; // global bc of the start of the first orbit
int64_t nBCsPerTF = -1; // duration of TF in bcs, should be 128*3564 or 32*3564
int mITSROFrameStartBorderMargin = 10; // default value
int mITSROFrameEndBorderMargin = 20; // default value
int mTimeFrameStartBorderMargin = 300; // default value
int mTimeFrameEndBorderMargin = 4000; // default value
void init(InitContext&)
{
// ccdb->setURL("http://ccdb-test.cern.ch:8080");
ccdb->setURL("http://alice-ccdb.cern.ch");
ccdb->setCaching(true);
ccdb->setLocalObjectValidityChecking();
histos.add("hCounterTVX", "", kTH1D, {{1, 0., 1.}});
histos.add("hCounterTCE", "", kTH1D, {{1, 0., 1.}});
histos.add("hCounterZEM", "", kTH1D, {{1, 0., 1.}});
histos.add("hCounterZNC", "", kTH1D, {{1, 0., 1.}});
histos.add("hCounterTVXafterBCcuts", "", kTH1D, {{1, 0., 1.}});
histos.add("hCounterTCEafterBCcuts", "", kTH1D, {{1, 0., 1.}});
histos.add("hCounterZEMafterBCcuts", "", kTH1D, {{1, 0., 1.}});
histos.add("hCounterZNCafterBCcuts", "", kTH1D, {{1, 0., 1.}});
histos.add("hLumiTVX", ";;Luminosity, 1/#mub", kTH1D, {{1, 0., 1.}});
histos.add("hLumiTCE", ";;Luminosity, 1/#mub", kTH1D, {{1, 0., 1.}});
histos.add("hLumiZEM", ";;Luminosity, 1/#mub", kTH1D, {{1, 0., 1.}});
histos.add("hLumiZNC", ";;Luminosity, 1/#mub", kTH1D, {{1, 0., 1.}});
histos.add("hLumiTVXafterBCcuts", ";;Luminosity, 1/#mub", kTH1D, {{1, 0., 1.}});
histos.add("hLumiTCEafterBCcuts", ";;Luminosity, 1/#mub", kTH1D, {{1, 0., 1.}});
histos.add("hLumiZEMafterBCcuts", ";;Luminosity, 1/#mub", kTH1D, {{1, 0., 1.}});
histos.add("hLumiZNCafterBCcuts", ";;Luminosity, 1/#mub", kTH1D, {{1, 0., 1.}});
}
void processRun2(
BCsWithRun2InfosTimestampsAndMatches const& bcs,
aod::Zdcs const&,
aod::FV0As const&,
aod::FV0Cs const&,
aod::FT0s const&,
aod::FDDs const&)
{
bcsel.reserve(bcs.size());
for (auto& bc : bcs) {
EventSelectionParams* par = ccdb->getForTimeStamp<EventSelectionParams>("EventSelection/EventSelectionParams", bc.timestamp());
TriggerAliases* aliases = ccdb->getForTimeStamp<TriggerAliases>("EventSelection/TriggerAliases", bc.timestamp());
// fill fired aliases
uint32_t alias{0};
uint64_t triggerMask = bc.triggerMask();
for (auto& al : aliases->GetAliasToTriggerMaskMap()) {
if (triggerMask & al.second) {
alias |= BIT(al.first);
}
}
uint64_t triggerMaskNext50 = bc.triggerMaskNext50();
for (auto& al : aliases->GetAliasToTriggerMaskNext50Map()) {
if (triggerMaskNext50 & al.second) {
alias |= BIT(al.first);
}
}
alias |= BIT(kALL);
// get timing info from ZDC, FV0, FT0 and FDD
float timeZNA = bc.has_zdc() ? bc.zdc().timeZNA() : -999.f;
float timeZNC = bc.has_zdc() ? bc.zdc().timeZNC() : -999.f;
float timeV0A = bc.has_fv0a() ? bc.fv0a().time() : -999.f;
float timeV0C = bc.has_fv0c() ? bc.fv0c().time() : -999.f;
float timeT0A = bc.has_ft0() ? bc.ft0().timeA() : -999.f;
float timeT0C = bc.has_ft0() ? bc.ft0().timeC() : -999.f;
float timeFDA = bc.has_fdd() ? bc.fdd().timeA() : -999.f;
float timeFDC = bc.has_fdd() ? bc.fdd().timeC() : -999.f;
LOGF(debug, "timeZNA=%f timeZNC=%f", timeZNA, timeZNC);
LOGF(debug, "timeV0A=%f timeV0C=%f", timeV0A, timeV0C);
LOGF(debug, "timeFDA=%f timeFDC=%f", timeFDA, timeFDC);
LOGF(debug, "timeT0A=%f timeT0C=%f", timeT0A, timeT0C);
// fill time-based selection criteria
uint64_t selection{0};
selection |= timeV0A > par->fV0ABBlower && timeV0A < par->fV0ABBupper ? BIT(kIsBBV0A) : 0;
selection |= timeV0C > par->fV0CBBlower && timeV0C < par->fV0CBBupper ? BIT(kIsBBV0C) : 0;
selection |= timeFDA > par->fFDABBlower && timeFDA < par->fFDABBupper ? BIT(kIsBBFDA) : 0;
selection |= timeFDC > par->fFDCBBlower && timeFDC < par->fFDCBBupper ? BIT(kIsBBFDC) : 0;
selection |= !(timeV0A > par->fV0ABGlower && timeV0A < par->fV0ABGupper) ? BIT(kNoBGV0A) : 0;
selection |= !(timeV0C > par->fV0CBGlower && timeV0C < par->fV0CBGupper) ? BIT(kNoBGV0C) : 0;
selection |= !(timeFDA > par->fFDABGlower && timeFDA < par->fFDABGupper) ? BIT(kNoBGFDA) : 0;
selection |= !(timeFDC > par->fFDCBGlower && timeFDC < par->fFDCBGupper) ? BIT(kNoBGFDC) : 0;
selection |= (timeT0A > par->fT0ABBlower && timeT0A < par->fT0ABBupper) ? BIT(kIsBBT0A) : 0;
selection |= (timeT0C > par->fT0CBBlower && timeT0C < par->fT0CBBupper) ? BIT(kIsBBT0C) : 0;
selection |= (timeZNA > par->fZNABBlower && timeZNA < par->fZNABBupper) ? BIT(kIsBBZNA) : 0;
selection |= (timeZNC > par->fZNCBBlower && timeZNC < par->fZNCBBupper) ? BIT(kIsBBZNC) : 0;
selection |= !(fabs(timeZNA) > par->fZNABGlower && fabs(timeZNA) < par->fZNABGupper) ? BIT(kNoBGZNA) : 0;
selection |= !(fabs(timeZNC) > par->fZNCBGlower && fabs(timeZNC) < par->fZNCBGupper) ? BIT(kNoBGZNC) : 0;
selection |= (pow((timeZNA + timeZNC - par->fZNSumMean) / par->fZNSumSigma, 2) + pow((timeZNA - timeZNC - par->fZNDifMean) / par->fZNDifSigma, 2) < 1) ? BIT(kIsBBZAC) : 0;
// Calculate V0 multiplicity per ring
float multRingV0A[5] = {0.};
float multRingV0C[4] = {0.};
float multFV0A = 0;
float multFV0C = 0;
if (bc.has_fv0a()) {
for (unsigned int i = 0; i < bc.fv0a().amplitude().size(); ++i) {
int ring = bc.fv0a().channel()[i] / 8;
multRingV0A[ring] += bc.fv0a().amplitude()[i];
multFV0A += bc.fv0a().amplitude()[i];
}
}
if (bc.has_fv0c()) {
for (unsigned int i = 0; i < bc.fv0c().amplitude().size(); ++i) {
int ring = bc.fv0c().channel()[i] / 8;
multRingV0C[ring] += bc.fv0c().amplitude()[i];
multFV0C += bc.fv0c().amplitude()[i];
}
}
// Calculate pileup and background related selection flags
// V0A0 excluded from online V0A charge sum => excluding also from offline sum for consistency
float ofV0M = multFV0A + multFV0C - multRingV0A[0];
float onV0M = bc.v0TriggerChargeA() + bc.v0TriggerChargeC();
float ofSPD = bc.spdFiredChipsL0() + bc.spdFiredChipsL1();
float onSPD = bc.spdFiredFastOrL0() + bc.spdFiredFastOrL1();
float multV0C012 = multRingV0C[0] + multRingV0C[1] + multRingV0C[2];
selection |= (onV0M > par->fV0MOnVsOfA + par->fV0MOnVsOfB * ofV0M) ? BIT(kNoV0MOnVsOfPileup) : 0;
selection |= (onSPD > par->fSPDOnVsOfA + par->fSPDOnVsOfB * ofSPD) ? BIT(kNoSPDOnVsOfPileup) : 0;
selection |= (multRingV0C[3] > par->fV0CasymA + par->fV0CasymB * multV0C012) ? BIT(kNoV0Casymmetry) : 0;
selection |= (TESTBIT(selection, kIsBBV0A) || TESTBIT(selection, kIsBBV0C) || ofSPD) ? BIT(kIsINT1) : 0;
selection |= (bc.has_ft0() ? TESTBIT(bc.ft0().triggerMask(), o2::ft0::Triggers::bitVertex) : 0) ? BIT(kIsTriggerTVX) : 0;
// copy remaining selection decisions from eventCuts
uint32_t eventCuts = bc.eventCuts();
selection |= (eventCuts & 1 << aod::kTimeRangeCut) ? BIT(kIsGoodTimeRange) : 0;
selection |= (eventCuts & 1 << aod::kIncompleteDAQ) ? BIT(kNoIncompleteDAQ) : 0;
selection |= !(eventCuts & 1 << aod::kIsTPCLaserWarmUp) ? BIT(kNoTPCLaserWarmUp) : 0;
selection |= !(eventCuts & 1 << aod::kIsTPCHVdip) ? BIT(kNoTPCHVdip) : 0;
selection |= !(eventCuts & 1 << aod::kIsPileupFromSPD) ? BIT(kNoPileupFromSPD) : 0;
selection |= !(eventCuts & 1 << aod::kIsV0PFPileup) ? BIT(kNoV0PFPileup) : 0;
selection |= (eventCuts & 1 << aod::kConsistencySPDandTrackVertices) ? BIT(kNoInconsistentVtx) : 0;
selection |= (eventCuts & 1 << aod::kPileupInMultBins) ? BIT(kNoPileupInMultBins) : 0;
selection |= (eventCuts & 1 << aod::kPileUpMV) ? BIT(kNoPileupMV) : 0;
selection |= (eventCuts & 1 << aod::kTPCPileUp) ? BIT(kNoPileupTPC) : 0;
int32_t foundFT0 = bc.has_ft0() ? bc.ft0().globalIndex() : -1;
int32_t foundFV0 = bc.has_fv0a() ? bc.fv0a().globalIndex() : -1;
int32_t foundFDD = bc.has_fdd() ? bc.fdd().globalIndex() : -1;
int32_t foundZDC = bc.has_zdc() ? bc.zdc().globalIndex() : -1;
// Fill TVX (T0 vertex) counters
if (TESTBIT(selection, kIsTriggerTVX)) {
histos.get<TH1>(HIST("hCounterTVX"))->Fill(Form("%d", bc.runNumber()), 1);
}
// Fill bc selection columns
bcsel(alias, selection, foundFT0, foundFV0, foundFDD, foundZDC);
}
}
PROCESS_SWITCH(BcSelectionTask, processRun2, "Process Run2 event selection", true);
void processRun3(BCsWithRun3Matchings const& bcs,
aod::Zdcs const&,
aod::FV0As const&,
aod::FT0s const&,
aod::FDDs const&)
{
if (bcs.size() == 0)
return;
bcsel.reserve(bcs.size());
// extract ITS time frame parameters
int64_t ts = bcs.iteratorAt(0).timestamp();
auto alppar = ccdb->getForTimeStamp<o2::itsmft::DPLAlpideParam<0>>("ITS/Config/AlpideParam", ts);
// map from GlobalBC to BcId needed to find triggerBc
std::map<uint64_t, int32_t> mapGlobalBCtoBcId;
for (auto& bc : bcs) {
mapGlobalBCtoBcId[bc.globalBC()] = bc.globalIndex();
}
int triggerBcShift = confTriggerBcShift;
if (confTriggerBcShift == 999) {
int run = bcs.iteratorAt(0).runNumber();
triggerBcShift = (run <= 526766 || (run >= 526886 && run <= 527237) || (run >= 527259 && run <= 527518) || run == 527523 || run == 527734 || run >= 534091) ? 0 : 294;
}
// extract run number and related information
int run = bcs.iteratorAt(0).runNumber();
if (run != lastRunNumber) {
lastRunNumber = run; // do it only once
if (run >= 500000) { // access CCDB for data or anchored MC only
int64_t ts = bcs.iteratorAt(0).timestamp();
// access orbitShift, ITSROF and TF border margins
EventSelectionParams* par = ccdb->getForTimeStamp<EventSelectionParams>("EventSelection/EventSelectionParams", ts);
mITSROFrameStartBorderMargin = confITSROFrameStartBorderMargin < 0 ? par->fITSROFrameStartBorderMargin : confITSROFrameStartBorderMargin;
mITSROFrameEndBorderMargin = confITSROFrameEndBorderMargin < 0 ? par->fITSROFrameEndBorderMargin : confITSROFrameEndBorderMargin;
mTimeFrameStartBorderMargin = confTimeFrameStartBorderMargin < 0 ? par->fTimeFrameStartBorderMargin : confTimeFrameStartBorderMargin;
mTimeFrameEndBorderMargin = confTimeFrameEndBorderMargin < 0 ? par->fTimeFrameEndBorderMargin : confTimeFrameEndBorderMargin;
// access orbit-reset timestamp
auto ctpx = ccdb->getForTimeStamp<std::vector<Long64_t>>("CTP/Calib/OrbitReset", ts);
int64_t tsOrbitReset = (*ctpx)[0]; // us
// access TF duration, start-of-run and end-of-run timestamps from ECS GRP
std::map<std::string, std::string> metadata;
metadata["runNumber"] = Form("%d", run);
auto grpecs = ccdb->getSpecific<o2::parameters::GRPECSObject>("GLO/Config/GRPECS", ts, metadata);
uint32_t nOrbitsPerTF = grpecs->getNHBFPerTF(); // assuming 1 orbit = 1 HBF; nOrbitsPerTF=128 in 2022, 32 in 2023
int64_t tsSOR = grpecs->getTimeStart(); // ms
// calculate SOR orbit
int64_t orbitSOR = (tsSOR * 1000 - tsOrbitReset) / o2::constants::lhc::LHCOrbitMUS;
// adjust to the nearest TF edge
orbitSOR = orbitSOR / nOrbitsPerTF * nOrbitsPerTF + par->fTimeFrameOrbitShift;
// first bc of the first orbit (should coincide with TF start)
bcSOR = orbitSOR * o2::constants::lhc::LHCMaxBunches;
// duration of TF in bcs
nBCsPerTF = nOrbitsPerTF * o2::constants::lhc::LHCMaxBunches;
LOGP(info, "tsOrbitReset={} us, SOR = {} ms, orbitSOR = {}, nBCsPerTF = {}", tsOrbitReset, tsSOR, orbitSOR, nBCsPerTF);
}
}
// bc loop
for (auto bc : bcs) {
EventSelectionParams* par = ccdb->getForTimeStamp<EventSelectionParams>("EventSelection/EventSelectionParams", bc.timestamp());
TriggerAliases* aliases = ccdb->getForTimeStamp<TriggerAliases>("EventSelection/TriggerAliases", bc.timestamp());
uint32_t alias{0};
// workaround for pp2022 (trigger info is shifted by -294 bcs)
int32_t triggerBcId = mapGlobalBCtoBcId[bc.globalBC() + triggerBcShift];
if (triggerBcId) {
auto triggerBc = bcs.iteratorAt(triggerBcId);
uint64_t triggerMask = triggerBc.triggerMask();
for (auto& al : aliases->GetAliasToTriggerMaskMap()) {
if (triggerMask & al.second) {
alias |= BIT(al.first);
}
}
}
alias |= BIT(kALL);
// get timing info from ZDC, FV0, FT0 and FDD
float timeZNA = bc.has_zdc() ? bc.zdc().timeZNA() : -999.f;
float timeZNC = bc.has_zdc() ? bc.zdc().timeZNC() : -999.f;
float timeV0A = bc.has_fv0a() ? bc.fv0a().time() : -999.f;
float timeT0A = bc.has_ft0() ? bc.ft0().timeA() : -999.f;
float timeT0C = bc.has_ft0() ? bc.ft0().timeC() : -999.f;
float timeFDA = bc.has_fdd() ? bc.fdd().timeA() : -999.f;
float timeFDC = bc.has_fdd() ? bc.fdd().timeC() : -999.f;
float timeV0ABG = -999.f;
float timeT0ABG = -999.f;
float timeT0CBG = -999.f;
float timeFDABG = -999.f;
float timeFDCBG = -999.f;
uint64_t globalBC = bc.globalBC();
// move to previous bcs to check beam-gas in FT0, FV0 and FDD
int64_t backwardMoveCount = 0;
int64_t deltaBC = 6; // up to 6 bcs back
while (bc.globalBC() + deltaBC >= globalBC) {
if (bc == bcs.begin()) {
break;
}
--bc;
backwardMoveCount++;
if (bc.globalBC() + 1 == globalBC) {
timeV0ABG = bc.has_fv0a() ? bc.fv0a().time() : -999.f;
timeT0ABG = bc.has_ft0() ? bc.ft0().timeA() : -999.f;
timeT0CBG = bc.has_ft0() ? bc.ft0().timeC() : -999.f;
}
if (bc.globalBC() + 5 == globalBC) {
timeFDABG = bc.has_fdd() ? bc.fdd().timeA() : -999.f;
timeFDCBG = bc.has_fdd() ? bc.fdd().timeC() : -999.f;
}
}
// move back to initial position
bc.moveByIndex(backwardMoveCount);
// fill time-based selection criteria
uint64_t selection{0};
selection |= timeV0A > par->fV0ABBlower && timeV0A < par->fV0ABBupper ? BIT(kIsBBV0A) : 0;
selection |= timeFDA > par->fFDABBlower && timeFDA < par->fFDABBupper ? BIT(kIsBBFDA) : 0;
selection |= timeFDC > par->fFDCBBlower && timeFDC < par->fFDCBBupper ? BIT(kIsBBFDC) : 0;
selection |= !(timeV0ABG > par->fV0ABGlower && timeV0ABG < par->fV0ABGupper) ? BIT(kNoBGV0A) : 0;
selection |= !(timeFDABG > par->fFDABGlower && timeFDABG < par->fFDABGupper) ? BIT(kNoBGFDA) : 0;
selection |= !(timeFDCBG > par->fFDCBGlower && timeFDCBG < par->fFDCBGupper) ? BIT(kNoBGFDC) : 0;
selection |= !(timeT0ABG > par->fT0ABGlower && timeT0ABG < par->fT0ABGupper) ? BIT(kNoBGT0A) : 0;
selection |= !(timeT0CBG > par->fT0CBGlower && timeT0CBG < par->fT0CBGupper) ? BIT(kNoBGT0C) : 0;
selection |= (timeT0A > par->fT0ABBlower && timeT0A < par->fT0ABBupper) ? BIT(kIsBBT0A) : 0;
selection |= (timeT0C > par->fT0CBBlower && timeT0C < par->fT0CBBupper) ? BIT(kIsBBT0C) : 0;
selection |= (timeZNA > par->fZNABBlower && timeZNA < par->fZNABBupper) ? BIT(kIsBBZNA) : 0;
selection |= (timeZNC > par->fZNCBBlower && timeZNC < par->fZNCBBupper) ? BIT(kIsBBZNC) : 0;
selection |= (pow((timeZNA + timeZNC - par->fZNSumMean) / par->fZNSumSigma, 2) + pow((timeZNA - timeZNC - par->fZNDifMean) / par->fZNDifSigma, 2) < 1) ? BIT(kIsBBZAC) : 0;
selection |= !(fabs(timeZNA) > par->fZNABGlower && fabs(timeZNA) < par->fZNABGupper) ? BIT(kNoBGZNA) : 0;
selection |= !(fabs(timeZNC) > par->fZNCBGlower && fabs(timeZNC) < par->fZNCBGupper) ? BIT(kNoBGZNC) : 0;
selection |= (bc.has_ft0() ? (bc.ft0().triggerMask() & BIT(o2::ft0::Triggers::bitVertex)) > 0 : 0) ? BIT(kIsTriggerTVX) : 0;
// check if bc is far (at least confITSROFrameBorderMargin) from the end of ITS RO Frame border
// 2bc margin is also introduced at ehe beginning of ITS RO Frame to account for the uncertainty of the roFrameBiasInBC
uint16_t bcInITSROF = (globalBC + 3564 - alppar->roFrameBiasInBC) % alppar->roFrameLengthInBC;
LOGP(debug, "bcInITSROF={}", bcInITSROF);
selection |= bcInITSROF > mITSROFrameStartBorderMargin && bcInITSROF < alppar->roFrameLengthInBC - mITSROFrameEndBorderMargin ? BIT(kNoITSROFrameBorder) : 0;
// check if bc is far from the Time Frame borders
int64_t bcInTF = (globalBC - bcSOR) % nBCsPerTF;
LOGP(debug, "bcInTF={}", bcInTF);
selection |= bcInTF > mTimeFrameStartBorderMargin && bcInTF < nBCsPerTF - mTimeFrameEndBorderMargin ? BIT(kNoTimeFrameBorder) : 0;
int32_t foundFT0 = bc.has_ft0() ? bc.ft0().globalIndex() : -1;
int32_t foundFV0 = bc.has_fv0a() ? bc.fv0a().globalIndex() : -1;
int32_t foundFDD = bc.has_fdd() ? bc.fdd().globalIndex() : -1;
int32_t foundZDC = bc.has_zdc() ? bc.zdc().globalIndex() : -1;
LOGP(debug, "foundFT0={}", foundFT0);
// Temporary workaround to get visible cross section. TODO: store run-by-run visible cross sections in CCDB
const char* srun = Form("%d", run);
auto grplhcif = ccdb->getForTimeStamp<o2::parameters::GRPLHCIFData>("GLO/Config/GRPLHCIF", bc.timestamp());
int beamZ1 = grplhcif->getBeamZ(o2::constants::lhc::BeamA);
int beamZ2 = grplhcif->getBeamZ(o2::constants::lhc::BeamC);
bool isPP = beamZ1 == 1 && beamZ2 == 1;
bool injectionEnergy = (run >= 500000 && run <= 520099) || (run >= 534133 && run <= 534468);
// Cross sections in ub. Using dummy -1 if lumi estimator is not reliable
float csTVX = isPP ? (injectionEnergy ? 0.0355e6 : 0.0594e6) : -1.;
float csTCE = isPP ? -1. : 10.36e6;
float csZEM = isPP ? -1. : 415.2e6; // see AN: https://alice-notes.web.cern.ch/node/1515
float csZNC = isPP ? -1. : 214.5e6; // see AN: https://alice-notes.web.cern.ch/node/1515
if (run > 543437 && run < 543514) {
csTCE = 8.3e6;
}
if (run >= 543514) {
csTCE = 4.10e6; // see AN: https://alice-notes.web.cern.ch/node/1515
}
// Fill TVX (T0 vertex) counters
if (TESTBIT(selection, kIsTriggerTVX)) {
histos.get<TH1>(HIST("hCounterTVX"))->Fill(srun, 1);
histos.get<TH1>(HIST("hLumiTVX"))->Fill(srun, 1. / csTVX);
if (TESTBIT(selection, kNoITSROFrameBorder) && TESTBIT(selection, kNoTimeFrameBorder)) {
histos.get<TH1>(HIST("hCounterTVXafterBCcuts"))->Fill(srun, 1);
histos.get<TH1>(HIST("hLumiTVXafterBCcuts"))->Fill(srun, 1. / csTVX);
}
}
// Fill counters and lumi histograms for Pb-Pb lumi monitoring
// TODO: introduce pileup correction
if (bc.has_ft0() ? (TESTBIT(selection, kIsTriggerTVX) && TESTBIT(bc.ft0().triggerMask(), o2::ft0::Triggers::bitCen)) : 0) {
histos.get<TH1>(HIST("hCounterTCE"))->Fill(srun, 1);
histos.get<TH1>(HIST("hLumiTCE"))->Fill(srun, 1. / csTCE);
if (TESTBIT(selection, kNoITSROFrameBorder) && TESTBIT(selection, kNoTimeFrameBorder)) {
histos.get<TH1>(HIST("hCounterTCEafterBCcuts"))->Fill(srun, 1);
histos.get<TH1>(HIST("hLumiTCEafterBCcuts"))->Fill(srun, 1. / csTCE);
}
}
if (TESTBIT(selection, kIsBBZNA) || TESTBIT(selection, kIsBBZNC)) {
histos.get<TH1>(HIST("hCounterZEM"))->Fill(srun, 1);
histos.get<TH1>(HIST("hLumiZEM"))->Fill(srun, 1. / csZEM);
if (TESTBIT(selection, kNoITSROFrameBorder) && TESTBIT(selection, kNoTimeFrameBorder)) {
histos.get<TH1>(HIST("hCounterZEMafterBCcuts"))->Fill(srun, 1);
histos.get<TH1>(HIST("hLumiZEMafterBCcuts"))->Fill(srun, 1. / csZEM);
}
}
if (TESTBIT(selection, kIsBBZNC)) {
histos.get<TH1>(HIST("hCounterZNC"))->Fill(srun, 1);
histos.get<TH1>(HIST("hLumiZNC"))->Fill(srun, 1. / csZNC);
if (TESTBIT(selection, kNoITSROFrameBorder) && TESTBIT(selection, kNoTimeFrameBorder)) {
histos.get<TH1>(HIST("hCounterZNCafterBCcuts"))->Fill(srun, 1);
histos.get<TH1>(HIST("hLumiZNCafterBCcuts"))->Fill(srun, 1. / csZNC);
}
}
// Fill bc selection columns
bcsel(alias, selection, foundFT0, foundFV0, foundFDD, foundZDC);
}
}
PROCESS_SWITCH(BcSelectionTask, processRun3, "Process Run3 event selection", false);
};
struct EventSelectionTask {
SliceCache cache;
Produces<aod::EvSels> evsel;
Configurable<std::string> syst{"syst", "PbPb", "pp, pPb, Pbp, PbPb, XeXe"}; // TODO determine from AOD metadata or from CCDB
Configurable<int> muonSelection{"muonSelection", 0, "0 - barrel, 1 - muon selection with pileup cuts, 2 - muon selection without pileup cuts"};
Configurable<float> maxDiffZvtxFT0vsPV{"maxDiffZvtxFT0vsPV", 1., "maximum difference (in cm) between z-vertex from FT0 and PV"};
Configurable<bool> isMC{"isMC", 0, "0 - data, 1 - MC"};
Partition<aod::Tracks> tracklets = (aod::track::trackType == static_cast<uint8_t>(o2::aod::track::TrackTypeEnum::Run2Tracklet));
Service<o2::ccdb::BasicCCDBManager> ccdb;
HistogramRegistry histos{"Histos", {}, OutputObjHandlingPolicy::AnalysisObject};
int lastRun = -1; // last run number (needed to access ccdb only if run!=lastRun)
std::bitset<o2::constants::lhc::LHCMaxBunches> bcPatternB; // bc pattern of colliding bunches
int32_t findClosest(int64_t globalBC, std::map<int64_t, int32_t>& bcs)
{
auto it = bcs.lower_bound(globalBC);
int64_t bc1 = it->first;
int32_t index1 = it->second;
if (it != bcs.begin())
--it;
int64_t bc2 = it->first;
int32_t index2 = it->second;
int64_t dbc1 = std::abs(bc1 - globalBC);
int64_t dbc2 = std::abs(bc2 - globalBC);
return (dbc1 <= dbc2) ? index1 : index2;
}
void init(InitContext&)
{
// ccdb->setURL("http://ccdb-test.cern.ch:8080");
ccdb->setURL("http://alice-ccdb.cern.ch");
ccdb->setCaching(true);
ccdb->setLocalObjectValidityChecking();
histos.add("hColCounterAll", "", kTH1D, {{1, 0., 1.}});
histos.add("hColCounterTVX", "", kTH1D, {{1, 0., 1.}});
histos.add("hColCounterAcc", "", kTH1D, {{1, 0., 1.}});
}
void process(aod::Collisions const& collisions)
{
evsel.reserve(collisions.size());
}
void processRun2(aod::Collision const& col, BCsWithBcSelsRun2 const&, aod::Tracks const&, aod::FV0Cs const&)
{
auto bc = col.bc_as<BCsWithBcSelsRun2>();
EventSelectionParams* par = ccdb->getForTimeStamp<EventSelectionParams>("EventSelection/EventSelectionParams", bc.timestamp());
bool* applySelection = par->GetSelection(muonSelection);
if (isMC) {
applySelection[kIsBBZAC] = 0;
applySelection[kNoV0MOnVsOfPileup] = 0;
applySelection[kNoSPDOnVsOfPileup] = 0;
applySelection[kNoV0Casymmetry] = 0;
applySelection[kNoV0PFPileup] = 0;
}
int32_t foundBC = bc.globalIndex();
int32_t foundFT0 = bc.foundFT0Id();
int32_t foundFV0 = bc.foundFV0Id();
int32_t foundFDD = bc.foundFDDId();
int32_t foundZDC = bc.foundZDCId();
// copy alias decisions from bcsel table
uint32_t alias = bc.alias_raw();
// copy selection decisions from bcsel table
uint64_t selection = bc.selection_raw();
// calculate V0C012 multiplicity
float multRingV0C[4] = {0.};
if (bc.has_fv0c()) {
for (unsigned int i = 0; i < bc.fv0c().amplitude().size(); ++i) {
int ring = bc.fv0c().channel()[i] / 8;
multRingV0C[ring] += bc.fv0c().amplitude()[i];
}
}
float multV0C012 = multRingV0C[0] + multRingV0C[1] + multRingV0C[2];
// applying selections depending on the number of tracklets
auto trackletsGrouped = tracklets->sliceByCached(aod::track::collisionId, col.globalIndex(), cache);
int nTkl = trackletsGrouped.size();
int spdClusters = bc.spdClustersL0() + bc.spdClustersL1();
selection |= (spdClusters < par->fSPDClsVsTklA + nTkl * par->fSPDClsVsTklB) ? BIT(kNoSPDClsVsTklBG) : 0;
selection |= !(nTkl < 6 && multV0C012 > par->fV0C012vsTklA + nTkl * par->fV0C012vsTklB) ? BIT(kNoV0C012vsTklBG) : 0;
// apply int7-like selections
bool sel7 = 1;
for (int i = 0; i < kNsel; i++) {
sel7 = sel7 && (applySelection[i] ? TESTBIT(selection, i) : 1);
}
// TODO introduce array of sel[0]... sel[8] or similar?
bool sel8 = bc.selection_bit(kIsBBT0A) && bc.selection_bit(kIsBBT0C); // TODO apply other cuts for sel8
bool sel1 = bc.selection_bit(kIsINT1);
sel1 = sel1 && bc.selection_bit(kNoBGV0A);
sel1 = sel1 && bc.selection_bit(kNoBGV0C);
sel1 = sel1 && bc.selection_bit(kNoTPCLaserWarmUp);
sel1 = sel1 && bc.selection_bit(kNoTPCHVdip);
// INT1 (SPDFO>0 | V0A | V0C) minimum bias trigger logic used in pp2010 and pp2011
bool isINT1period = bc.runNumber() <= 136377 || (bc.runNumber() >= 144871 && bc.runNumber() <= 159582);
// fill counters
if (isMC || (!isINT1period && bc.alias_bit(kINT7)) || (isINT1period && bc.alias_bit(kINT1))) {
histos.get<TH1>(HIST("hColCounterAll"))->Fill(Form("%d", bc.runNumber()), 1);
if ((!isINT1period && sel7) || (isINT1period && sel1)) {
histos.get<TH1>(HIST("hColCounterAcc"))->Fill(Form("%d", bc.runNumber()), 1);
}
}
evsel(alias, selection, sel7, sel8, foundBC, foundFT0, foundFV0, foundFDD, foundZDC);
}
PROCESS_SWITCH(EventSelectionTask, processRun2, "Process Run2 event selection", true);
Preslice<FullTracksIU> perCollision = aod::track::collisionId;
void processRun3(aod::Collisions const& cols, FullTracksIU const& tracks, BCsWithBcSelsRun3 const& bcs, aod::FT0s const&)
{
int run = bcs.iteratorAt(0).runNumber();
// extract bc pattern from CCDB for data or anchored MC only
if (run != lastRun && run >= 500000) {
lastRun = run;
int64_t ts = bcs.iteratorAt(0).timestamp();
auto grplhcif = ccdb->getForTimeStamp<o2::parameters::GRPLHCIFData>("GLO/Config/GRPLHCIF", ts);
bcPatternB = grplhcif->getBunchFilling().getBCPattern();
}
// create maps from globalBC to bc index for TVX or FT0-OR fired bcs
// to be used for closest TVX (FT0-OR) searches
std::map<int64_t, int32_t> mapGlobalBcWithTVX;
std::map<int64_t, int32_t> mapGlobalBcWithTOR;
for (auto& bc : bcs) {
int64_t globalBC = bc.globalBC();
// skip non-colliding bcs for data and anchored runs
if (run >= 500000 && bcPatternB[globalBC % o2::constants::lhc::LHCMaxBunches] == 0) {
continue;
}
if (bc.selection_bit(kIsBBT0A) || bc.selection_bit(kIsBBT0C)) {
mapGlobalBcWithTOR[globalBC] = bc.globalIndex();
}
if (bc.selection_bit(kIsTriggerTVX)) {
mapGlobalBcWithTVX[globalBC] = bc.globalIndex();
}
}
// protection against empty FT0 maps
if (mapGlobalBcWithTOR.size() == 0 || mapGlobalBcWithTVX.size() == 0) {
LOGP(error, "FT0 table is empty or corrupted. Filling evsel table with dummy values");
for (auto& col : cols) {
auto bc = col.bc_as<BCsWithBcSelsRun3>();
int32_t foundBC = bc.globalIndex();
int32_t foundFT0 = bc.foundFT0Id();
int32_t foundFV0 = bc.foundFV0Id();
int32_t foundFDD = bc.foundFDDId();
int32_t foundZDC = bc.foundZDCId();
evsel(bc.alias_raw(), bc.selection_raw(), kFALSE, kFALSE, foundBC, foundFT0, foundFV0, foundFDD, foundZDC);
}
return;
}
std::vector<int> vFoundBCindex(cols.size(), -1); // indices of found bcs
std::vector<bool> vIsVertexITSTPC(cols.size(), 0); // at least one of vertex contributors is ITS-TPC track
std::vector<bool> vIsVertexTOFmatched(cols.size(), 0); // at least one of vertex contributors is matched to TOF
std::vector<bool> vIsVertexTRDmatched(cols.size(), 0); // at least one of vertex contributors is matched to TRD
std::vector<int> vCollisionsPerBc(bcs.size(), 0); // counter of collisions per found bc for pileup checks
// loop to find nearest bc with FT0 entry -> foundBC index
for (auto& col : cols) {
auto bc = col.bc_as<BCsWithBcSelsRun3>();
int64_t meanBC = bc.globalBC();
const double bcNS = o2::constants::lhc::LHCBunchSpacingNS;
int64_t deltaBC = std::ceil(col.collisionTimeRes() / bcNS * 4);
// count tracks of different types
int nITSTPCtracks = 0;
int nTOFtracks = 0;
int nTRDtracks = 0;
int nTRDnotTOFtracks = 0;
double timeFromTOFtracks = 0;
double timeFromTRDtracks = 0;
auto tracksGrouped = tracks.sliceBy(perCollision, col.globalIndex());
for (auto& track : tracksGrouped) {
if (!track.isPVContributor()) {
continue;
}
nITSTPCtracks += track.hasITS() && track.hasTPC();
nTOFtracks += track.hasTOF();
nTRDtracks += track.hasTRD();
nTRDnotTOFtracks += track.hasTRD() && !track.hasTOF();
// calculate average time using TOF and TRD tracks
if (track.hasTOF()) {
timeFromTOFtracks += track.trackTime();
} else if (track.hasTRD()) {
timeFromTRDtracks += track.trackTime();
}
}
LOGP(debug, "nContrib={} nITSTPCtracks={} nTOFtracks={} nTRDtracks={} nTRDnotTOFtracks={}", col.numContrib(), nITSTPCtracks, nTOFtracks, nTRDtracks, nTRDnotTOFtracks);
if (nTRDnotTOFtracks > 0) {
meanBC += TMath::Nint(timeFromTRDtracks / nTRDnotTOFtracks / bcNS); // assign collision bc using TRD-matched tracks
deltaBC = 0; // use precise bc from TRD-matched tracks
} else if (nTOFtracks > 0) {
meanBC += TMath::FloorNint(timeFromTOFtracks / nTOFtracks / bcNS); // assign collision bc using TOF-matched tracks
deltaBC = 4; // use precise bc from TOF tracks with +/-4 bc margin
} else if (nITSTPCtracks > 0) {
deltaBC += 30; // extend deltaBC for collisions built with ITS-TPC tracks only
}
int64_t minBC = meanBC - deltaBC;
int64_t maxBC = meanBC + deltaBC;
int32_t indexClosestTVX = findClosest(meanBC, mapGlobalBcWithTVX);
int64_t tvxBC = bcs.iteratorAt(indexClosestTVX).globalBC();
if (tvxBC >= minBC && tvxBC <= maxBC) { // closest TVX within search region
bc.setCursor(indexClosestTVX);
} else { // no TVX within search region, searching for TOR = T0A | T0C
int32_t indexClosestTOR = findClosest(meanBC, mapGlobalBcWithTOR);
int64_t torBC = bcs.iteratorAt(indexClosestTOR).globalBC();
if (torBC >= minBC && torBC <= maxBC) {
bc.setCursor(indexClosestTOR);
}
}
int32_t foundBC = bc.globalIndex();
int32_t colIndex = col.globalIndex();
LOGP(debug, "foundBC = {} globalBC = {}", foundBC, bc.globalBC());
vFoundBCindex[colIndex] = foundBC;
vIsVertexITSTPC[colIndex] = nITSTPCtracks > 0;
vIsVertexTOFmatched[colIndex] = nTOFtracks > 0;
vIsVertexTRDmatched[colIndex] = nTRDtracks > 0;
vCollisionsPerBc[foundBC]++;
}
for (auto& col : cols) {
int32_t colIndex = col.globalIndex();
int32_t foundBC = vFoundBCindex[colIndex];
auto bc = bcs.iteratorAt(foundBC);
int32_t foundFT0 = bc.foundFT0Id();
int32_t foundFV0 = bc.foundFV0Id();
int32_t foundFDD = bc.foundFDDId();
int32_t foundZDC = bc.foundZDCId();
// compare zVtx from FT0 and from PV
bool isGoodZvtxFT0vsPV = bc.has_foundFT0() ? fabs(bc.foundFT0().posZ() - col.posZ()) < maxDiffZvtxFT0vsPV : 0;
// copy alias decisions from bcsel table
uint32_t alias = bc.alias_raw();
// copy selection decisions from bcsel table
uint64_t selection = bc.selection_raw();
selection |= vCollisionsPerBc[foundBC] <= 1 ? BIT(kNoSameBunchPileup) : 0;
selection |= vIsVertexITSTPC[colIndex] ? BIT(kIsVertexITSTPC) : 0;
selection |= vIsVertexTOFmatched[colIndex] ? BIT(kIsVertexTOFmatched) : 0;
selection |= vIsVertexTRDmatched[colIndex] ? BIT(kIsVertexTRDmatched) : 0;
selection |= isGoodZvtxFT0vsPV ? BIT(kIsGoodZvtxFT0vsPV) : 0;
// apply int7-like selections
bool sel7 = 0;
// TODO apply other cuts for sel8
// TODO introduce sel1 etc?
// TODO introduce array of sel[0]... sel[8] or similar?
bool sel8 = bc.selection_bit(kIsTriggerTVX) && bc.selection_bit(kNoTimeFrameBorder) && bc.selection_bit(kNoITSROFrameBorder);
// fill counters
histos.get<TH1>(HIST("hColCounterAll"))->Fill(Form("%d", bc.runNumber()), 1);
if (bc.selection_bit(kIsTriggerTVX)) {
histos.get<TH1>(HIST("hColCounterTVX"))->Fill(Form("%d", bc.runNumber()), 1);
}
if (sel8) {
histos.get<TH1>(HIST("hColCounterAcc"))->Fill(Form("%d", bc.runNumber()), 1);
}
evsel(alias, selection, sel7, sel8, foundBC, foundFT0, foundFV0, foundFDD, foundZDC);
}
}
PROCESS_SWITCH(EventSelectionTask, processRun3, "Process Run3 event selection", false);
};
WorkflowSpec defineDataProcessing(ConfigContext const& cfgc)
{
return WorkflowSpec{
adaptAnalysisTask<BcSelectionTask>(cfgc),
adaptAnalysisTask<EventSelectionTask>(cfgc)};
}