forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigitReaderSpec.cxx
More file actions
176 lines (161 loc) · 6.25 KB
/
DigitReaderSpec.cxx
File metadata and controls
176 lines (161 loc) · 6.25 KB
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
// 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 "CTPWorkflowIO/DigitReaderSpec.h"
#include "TFile.h"
#include "TTree.h"
#include "DataFormatsCTP/Digits.h"
#include "DataFormatsCTP/LumiInfo.h"
#include "Headers/DataHeader.h"
#include "DetectorsCommonDataFormats/DetID.h"
#include "SimulationDataFormat/MCCompLabel.h"
#include "SimulationDataFormat/ConstMCTruthContainer.h"
#include "CommonUtils/NameConf.h"
#include "CommonUtils/IRFrameSelector.h"
#include "Framework/DataProcessorSpec.h"
#include "Framework/Task.h"
#include "Framework/ControlService.h"
#include "Framework/ConfigParamRegistry.h"
#include "Framework/Logger.h"
#include <vector>
using namespace o2::framework;
namespace o2
{
namespace ctp
{
class DigitReader : public Task
{
public:
DigitReader() = delete;
DigitReader(bool useMC);
~DigitReader() override = default;
void init(InitContext& ic) final;
void run(ProcessingContext& pc) final;
protected:
void connectTree(const std::string& filename);
std::vector<o2::ctp::CTPDigit> mDigits, *mDigitsPtr = &mDigits;
o2::ctp::LumiInfo mLumi, *mLumiPtr = &mLumi;
std::unique_ptr<TFile> mFile;
std::unique_ptr<TTree> mTree;
bool mUseMC = false; // use MC truth
bool mUseIRFrames = false; // selected IRFrames mode
std::string mDigTreeName = "o2sim";
std::string mDigitBranchName = "CTPDigits";
std::string mLumiBranchName = "CTPLumi";
};
DigitReader::DigitReader(bool useMC)
{
if (useMC) {
LOG(info) << "CTP : truth = data as CTP inputs are already digital";
}
}
void DigitReader::init(InitContext& ic)
{
auto filename = o2::utils::Str::concat_string(o2::utils::Str::rectifyDirectory(ic.options().get<std::string>("input-dir")),
ic.options().get<std::string>("ctp-digit-infile"));
if (ic.options().hasOption("ignore-irframes") && !ic.options().get<bool>("ignore-irframes")) {
mUseIRFrames = true;
}
connectTree(filename);
}
void DigitReader::run(ProcessingContext& pc)
{
gsl::span<const o2::dataformats::IRFrame> irFrames{};
// LOG(info) << "Using IRs:" << mUseIRFrames;
if (mUseIRFrames) {
irFrames = pc.inputs().get<gsl::span<o2::dataformats::IRFrame>>("driverInfo");
}
auto ent = mTree->GetReadEntry();
if (!mUseIRFrames) {
ent++;
assert(ent < mTree->GetEntries()); // this should not happen
mTree->GetEntry(ent);
LOG(info) << "DigitReader pushes " << mDigits.size() << " digits at entry " << ent;
pc.outputs().snapshot(Output{"CTP", "DIGITS", 0}, mDigits);
pc.outputs().snapshot(Output{"CTP", "LUMI", 0}, mLumi);
if (mTree->GetReadEntry() + 1 >= mTree->GetEntries()) {
pc.services().get<ControlService>().endOfStream();
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
}
} else {
std::vector<o2::ctp::CTPDigit> digitSel;
if (irFrames.size()) { // we assume the IRFrames are in the increasing order
if (ent < 0) {
ent++;
}
o2::utils::IRFrameSelector irfSel;
// MC digits are already aligned
irfSel.setSelectedIRFrames(irFrames, 0, 0, 0, true);
const auto irMin = irfSel.getIRFrames().front().getMin(); // use processed IRframes for rough comparisons (possible shift!)
const auto irMax = irfSel.getIRFrames().back().getMax();
LOGP(info, "Selecting IRFrame {}-{}", irMin.asString(), irMax.asString());
while (ent < mTree->GetEntries()) {
if (ent > mTree->GetReadEntry()) {
mTree->GetEntry(ent);
}
if (mDigits.front().intRecord <= irMax && mDigits.back().intRecord >= irMin) { // THere is overlap
for (int i = 0; i < (int)mDigits.size(); i++) {
const auto& dig = mDigits[i];
// if(irfSel.check(dig.intRecord)) { // adding selected digit
if (dig.intRecord >= irMin && dig.intRecord <= irMax) {
digitSel.push_back(dig);
LOG(info) << "adding:" << dig.intRecord << " ent:" << ent;
}
}
}
if (mDigits.back().intRecord < irMax) { // need to check the next entry
ent++;
continue;
}
break; // push collected data
}
}
pc.outputs().snapshot(Output{"CTP", "DIGITS", 0}, digitSel);
pc.outputs().snapshot(Output{"CTP", "LUMI", 0}, mLumi); // add full lumi for this TF
if (!irFrames.size() || irFrames.back().isLast()) {
pc.services().get<ControlService>().endOfStream();
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
}
}
}
void DigitReader::connectTree(const std::string& filename)
{
mTree.reset(nullptr); // in case it was already loaded
mFile.reset(TFile::Open(filename.c_str()));
assert(mFile && !mFile->IsZombie());
mTree.reset((TTree*)mFile->Get(mDigTreeName.c_str()));
assert(mTree);
if (mTree->GetBranch(mDigitBranchName.c_str())) {
mTree->SetBranchAddress(mDigitBranchName.c_str(), &mDigitsPtr);
} else {
LOGP(warn, "Digits branch {} is absent", mDigitBranchName);
}
if (mTree->GetBranch(mLumiBranchName.c_str())) {
mTree->SetBranchAddress(mLumiBranchName.c_str(), &mLumiPtr);
} else {
LOGP(warn, "Lumi branch {} is absent", mLumiBranchName);
}
mTree->SetBranchAddress(mDigitBranchName.c_str(), &mDigitsPtr);
LOG(info) << "Loaded tree from " << filename << " with " << mTree->GetEntries() << " entries";
}
DataProcessorSpec getDigitsReaderSpec(bool useMC, const std::string& defFile)
{
return DataProcessorSpec{
"ctp-digit-reader",
Inputs{},
Outputs{{"CTP", "DIGITS", 0, Lifetime::Timeframe},
{"CTP", "LUMI", 0, o2::framework::Lifetime::Timeframe}},
AlgorithmSpec{adaptFromTask<DigitReader>(useMC)},
Options{
{"ctp-digit-infile", VariantType::String, defFile, {"Name of the input digit file"}},
{"input-dir", VariantType::String, "none", {"Input directory"}}}};
}
} // namespace ctp
} // namespace o2