Skip to content

Commit 9d39dbc

Browse files
committed
Adapth ITS/MFT digit reader to IRFrames filtering
1 parent 53682be commit 9d39dbc

File tree

2 files changed

+167
-30
lines changed

2 files changed

+167
-30
lines changed

Detectors/ITSMFT/common/workflow/include/ITSMFTWorkflow/DigitReaderSpec.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "Headers/DataHeader.h"
2525
#include "DataFormatsITSMFT/ROFRecord.h"
2626
#include "DetectorsCommonDataFormats/DetID.h"
27+
#include "SimulationDataFormat/IOMCTruthContainerView.h"
28+
#include "SimulationDataFormat/ConstMCTruthContainer.h"
2729

2830
using namespace o2::framework;
2931

@@ -48,14 +50,18 @@ class DigitReader : public Task
4850
std::vector<o2::itsmft::GBTCalibData> mCalib, *mCalibPtr = &mCalib;
4951
std::vector<o2::itsmft::ROFRecord> mDigROFRec, *mDigROFRecPtr = &mDigROFRec;
5052
std::vector<o2::itsmft::MC2ROFRecord> mDigMC2ROFs, *mDigMC2ROFsPtr = &mDigMC2ROFs;
51-
53+
o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel> mConstLabels;
5254
o2::header::DataOrigin mOrigin = o2::header::gDataOriginInvalid;
5355

5456
std::unique_ptr<TFile> mFile;
5557
std::unique_ptr<TTree> mTree;
5658
bool mUseMC = true; // use MC truth
5759
bool mUseCalib = true; // send calib data
5860
bool mTriggerOut = true; // send dummy triggers vector
61+
bool mUseIRFrames = false; // selected IRFrames modes
62+
int mROFBiasInBC = 0;
63+
int mROFLengthInBC = 0;
64+
int mNRUs = 0;
5965
std::string mDetName = "";
6066
std::string mDetNameLC = "";
6167
std::string mFileName = "";

Detectors/ITSMFT/common/workflow/src/DigitReaderSpec.cxx

Lines changed: 160 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@
1919
#include "Framework/ConfigParamRegistry.h"
2020
#include "Framework/Logger.h"
2121
#include "ITSMFTWorkflow/DigitReaderSpec.h"
22+
#include "ITSMFTBase/DPLAlpideParam.h"
23+
#include "ITSMFTReconstruction/ChipMappingITS.h"
24+
#include "ITSMFTReconstruction/ChipMappingMFT.h"
2225
#include "SimulationDataFormat/MCCompLabel.h"
2326
#include "SimulationDataFormat/ConstMCTruthContainer.h"
24-
#include "SimulationDataFormat/IOMCTruthContainerView.h"
2527
#include "DataFormatsITSMFT/PhysTrigger.h"
2628
#include "CommonUtils/NameConf.h"
29+
#include "CommonDataFormat/IRFrame.h"
30+
#include "CommonUtils/IRFrameSelector.h"
31+
#include "CCDB/BasicCCDBManager.h"
2732
#include <cassert>
2833

2934
using namespace o2::framework;
@@ -56,43 +61,169 @@ void DigitReader::init(InitContext& ic)
5661
{
5762
mFileName = o2::utils::Str::concat_string(o2::utils::Str::rectifyDirectory(ic.options().get<std::string>("input-dir")),
5863
ic.options().get<std::string>((mDetNameLC + "-digit-infile").c_str()));
64+
if (ic.options().hasOption("ignore-irframes") && !ic.options().get<bool>("ignore-irframes")) {
65+
mUseIRFrames = true;
66+
}
5967
connectTree(mFileName);
6068
}
6169

6270
void DigitReader::run(ProcessingContext& pc)
6371
{
64-
auto ent = mTree->GetReadEntry() + 1;
65-
assert(ent < mTree->GetEntries()); // this should not happen
66-
67-
o2::dataformats::IOMCTruthContainerView* plabels = nullptr;
68-
if (mUseMC) {
69-
mTree->SetBranchAddress(mDigtMCTruthBranchName.c_str(), &plabels);
70-
}
71-
mTree->GetEntry(ent);
72-
LOG(info) << mDetName << "DigitReader pushes " << mDigROFRec.size() << " ROFRecords, "
73-
<< mDigits.size() << " digits at entry " << ent;
74-
75-
// This is a very ugly way of providing DataDescription, which anyway does not need to contain detector name.
76-
// To be fixed once the names-definition class is ready
77-
pc.outputs().snapshot(Output{mOrigin, "DIGITSROF", 0}, mDigROFRec);
78-
pc.outputs().snapshot(Output{mOrigin, "DIGITS", 0}, mDigits);
79-
if (mUseCalib) {
80-
pc.outputs().snapshot(Output{mOrigin, "GBTCALIB", 0}, mCalib);
72+
const auto& tinfo = pc.services().get<o2::framework::TimingInfo>();
73+
if (tinfo.globalRunNumberChanged) { // new run is starting: 1st call
74+
// TODO: we have to find a way define CCDBInput for IRFrames mode only using DPL fetcher
75+
auto& ccdb = o2::ccdb::BasicCCDBManager::instance();
76+
auto rlim = ccdb.getRunDuration(tinfo.runNumber);
77+
long ts = (rlim.first + rlim.second) / 2;
78+
if (mOrigin == o2::header::gDataOriginITS) {
79+
ccdb.getForTimeStamp<o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>>("ITS/Config/AlpideParam", ts);
80+
const auto& alpideParam = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>::Instance();
81+
mROFBiasInBC = alpideParam.roFrameBiasInBC;
82+
mROFLengthInBC = alpideParam.roFrameLengthInBC;
83+
mNRUs = o2::itsmft::ChipMappingITS::getNRUs();
84+
} else {
85+
ccdb.getForTimeStamp<o2::itsmft::DPLAlpideParam<o2::detectors::DetID::MFT>>("MFT/Config/AlpideParam", ts);
86+
const auto& alpideParam = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::MFT>::Instance();
87+
mROFBiasInBC = alpideParam.roFrameBiasInBC;
88+
mROFLengthInBC = alpideParam.roFrameLengthInBC;
89+
mNRUs = o2::itsmft::ChipMappingMFT::getNRUs();
90+
}
8191
}
82-
if (mTriggerOut) {
83-
std::vector<o2::itsmft::PhysTrigger> dummyTrig;
84-
pc.outputs().snapshot(Output{mOrigin, "PHYSTRIG", 0}, dummyTrig);
92+
gsl::span<const o2::dataformats::IRFrame> irFrames{};
93+
if (mUseIRFrames) {
94+
irFrames = pc.inputs().get<gsl::span<o2::dataformats::IRFrame>>("driverInfo");
8595
}
86-
if (mUseMC) {
87-
auto& sharedlabels = pc.outputs().make<o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>>(Output{mOrigin, "DIGITSMCTR", 0});
88-
plabels->copyandflatten(sharedlabels);
89-
delete plabels;
90-
pc.outputs().snapshot(Output{mOrigin, "DIGITSMC2ROF", 0}, mDigMC2ROFs);
96+
static o2::dataformats::IOMCTruthContainerView* plabels = nullptr;
97+
if (mUseMC && !plabels) {
98+
mTree->SetBranchAddress(mDigtMCTruthBranchName.c_str(), &plabels);
9199
}
100+
auto ent = mTree->GetReadEntry();
92101

93-
if (mTree->GetReadEntry() + 1 >= mTree->GetEntries()) {
94-
pc.services().get<ControlService>().endOfStream();
95-
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
102+
if (!mUseIRFrames) {
103+
ent++;
104+
assert(ent < mTree->GetEntries()); // this should not happen
105+
mTree->GetEntry(ent);
106+
LOG(info) << mDetName << "DigitReader pushes " << mDigROFRec.size() << " ROFRecords, " << mDigits.size() << " digits at entry " << ent;
107+
pc.outputs().snapshot(Output{mOrigin, "DIGITSROF", 0}, mDigROFRec);
108+
pc.outputs().snapshot(Output{mOrigin, "DIGITS", 0}, mDigits);
109+
if (mUseCalib) {
110+
pc.outputs().snapshot(Output{mOrigin, "GBTCALIB", 0}, mCalib);
111+
}
112+
if (mTriggerOut) {
113+
std::vector<o2::itsmft::PhysTrigger> dummyTrig;
114+
pc.outputs().snapshot(Output{mOrigin, "PHYSTRIG", 0}, dummyTrig);
115+
}
116+
if (mUseMC) {
117+
auto& sharedlabels = pc.outputs().make<o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>>(Output{mOrigin, "DIGITSMCTR", 0});
118+
plabels->copyandflatten(sharedlabels);
119+
delete plabels;
120+
plabels = nullptr;
121+
pc.outputs().snapshot(Output{mOrigin, "DIGITSMC2ROF", 0}, mDigMC2ROFs);
122+
}
123+
if (mTree->GetReadEntry() + 1 >= mTree->GetEntries()) {
124+
pc.services().get<ControlService>().endOfStream();
125+
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
126+
}
127+
} else { // need to select particulars IRs range, presumably from the same tree entry
128+
std::vector<o2::itsmft::Digit> digitsSel;
129+
std::vector<o2::itsmft::GBTCalibData> calibSel;
130+
std::vector<o2::itsmft::ROFRecord> digROFRecSel;
131+
std::vector<o2::itsmft::MC2ROFRecord> digMC2ROFsSel;
132+
o2::dataformats::MCTruthContainer<o2::MCCompLabel> digitLabelsSel;
133+
134+
if (irFrames.size()) { // we assume the IRFrames are in the increasing order
135+
if (ent < 0) {
136+
ent++;
137+
}
138+
o2::utils::IRFrameSelector irfSel;
139+
irfSel.setSelectedIRFrames(irFrames, 0, 0, mROFBiasInBC, true);
140+
const auto irMin = irFrames.front().getMin();
141+
const auto irMax = irFrames.back().getMax();
142+
LOGP(info, "Selecting IRFrame {}-{}", irMin.asString(), irMax.asString());
143+
while (ent < mTree->GetEntries()) {
144+
// do we need to read a new entry?
145+
if (ent > mTree->GetReadEntry()) {
146+
if (mUseMC) {
147+
delete plabels;
148+
plabels = nullptr;
149+
mConstLabels.clear();
150+
mTree->SetBranchAddress(mDigtMCTruthBranchName.c_str(), &plabels);
151+
}
152+
mTree->GetEntry(ent);
153+
if (mUseMC) {
154+
plabels->copyandflatten(mConstLabels);
155+
delete plabels;
156+
plabels = nullptr;
157+
}
158+
}
159+
std::vector<int> rofOld2New;
160+
rofOld2New.resize(mDigROFRec.size(), -1);
161+
162+
if (mDigROFRec.front().getBCData() <= irMax && mDigROFRec.back().getBCData() >= irMin) { // there is an overlap
163+
for (int irof = 0; irof < (int)mDigROFRec.size(); irof++) {
164+
const auto& rof = mDigROFRec[irof];
165+
if (irfSel.check({rof.getBCData(), rof.getBCData() + mROFLengthInBC - 1}) != -1) {
166+
rofOld2New[irof] = (int)digROFRecSel.size();
167+
LOGP(debug, "Adding selected ROF {}", rof.getBCData().asString());
168+
digROFRecSel.push_back(rof);
169+
int offs = digitsSel.size();
170+
digROFRecSel.back().setFirstEntry(offs);
171+
std::copy(mDigits.begin() + rof.getFirstEntry(), mDigits.begin() + rof.getFirstEntry() + rof.getNEntries(), std::back_inserter(digitsSel));
172+
for (int id = 0; id < rof.getNEntries(); id++) { // copy MC info
173+
digitLabelsSel.addElements(id + offs, mConstLabels.getLabels(id + rof.getFirstEntry()));
174+
}
175+
if (mCalib.size() >= size_t((irof + 1) * mNRUs)) {
176+
std::copy(mCalib.begin() + irof * mNRUs, mCalib.begin() + (irof + 1) * mNRUs, std::back_inserter(calibSel));
177+
}
178+
}
179+
}
180+
}
181+
if (mUseMC) {
182+
digMC2ROFsSel = mDigMC2ROFs;
183+
for (auto& mc2rof : digMC2ROFsSel) {
184+
if (mc2rof.rofRecordID < 0) {
185+
continue; // did not contribute even to the original data
186+
}
187+
unsigned int mn = 0xffff, mx = 0;
188+
for (int ir = mc2rof.minROF; ir <= mc2rof.maxROF; ir++) {
189+
if (rofOld2New[ir] >= 0) { // used
190+
mx = rofOld2New[ir];
191+
if (mn > mx) {
192+
mn = mx;
193+
}
194+
}
195+
}
196+
mc2rof.rofRecordID = mn == 0xffff ? -1 : int(mn);
197+
mc2rof.minROF = mn;
198+
mc2rof.maxROF = mx;
199+
}
200+
}
201+
if (mDigROFRec.back().getBCData() < irMax) { // need to check the next entry
202+
ent++;
203+
continue;
204+
}
205+
break; // push collected data
206+
}
207+
}
208+
pc.outputs().snapshot(Output{mOrigin, "DIGITSROF", 0}, digROFRecSel);
209+
pc.outputs().snapshot(Output{mOrigin, "DIGITS", 0}, digitsSel);
210+
if (mUseCalib) {
211+
pc.outputs().snapshot(Output{mOrigin, "GBTCALIB", 0}, calibSel);
212+
}
213+
if (mTriggerOut) {
214+
std::vector<o2::itsmft::PhysTrigger> dummyTrig;
215+
pc.outputs().snapshot(Output{mOrigin, "PHYSTRIG", 0}, dummyTrig);
216+
}
217+
if (mUseMC) {
218+
auto& sharedlabels = pc.outputs().make<o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>>(Output{mOrigin, "DIGITSMCTR", 0});
219+
digitLabelsSel.flatten_to(sharedlabels);
220+
pc.outputs().snapshot(Output{mOrigin, "DIGITSMC2ROF", 0}, digMC2ROFsSel);
221+
}
222+
223+
if (!irFrames.size() || irFrames.back().isLast()) {
224+
pc.services().get<ControlService>().endOfStream();
225+
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
226+
}
96227
}
97228
}
98229

0 commit comments

Comments
 (0)