Skip to content

Commit

Permalink
Adapth ITS/MFT digit reader to IRFrames filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
shahor02 committed Oct 5, 2024
1 parent 53682be commit 9d39dbc
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "Headers/DataHeader.h"
#include "DataFormatsITSMFT/ROFRecord.h"
#include "DetectorsCommonDataFormats/DetID.h"
#include "SimulationDataFormat/IOMCTruthContainerView.h"
#include "SimulationDataFormat/ConstMCTruthContainer.h"

using namespace o2::framework;

Expand All @@ -48,14 +50,18 @@ class DigitReader : public Task
std::vector<o2::itsmft::GBTCalibData> mCalib, *mCalibPtr = &mCalib;
std::vector<o2::itsmft::ROFRecord> mDigROFRec, *mDigROFRecPtr = &mDigROFRec;
std::vector<o2::itsmft::MC2ROFRecord> mDigMC2ROFs, *mDigMC2ROFsPtr = &mDigMC2ROFs;

o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel> mConstLabels;
o2::header::DataOrigin mOrigin = o2::header::gDataOriginInvalid;

std::unique_ptr<TFile> mFile;
std::unique_ptr<TTree> mTree;
bool mUseMC = true; // use MC truth
bool mUseCalib = true; // send calib data
bool mTriggerOut = true; // send dummy triggers vector
bool mUseIRFrames = false; // selected IRFrames modes
int mROFBiasInBC = 0;
int mROFLengthInBC = 0;
int mNRUs = 0;
std::string mDetName = "";
std::string mDetNameLC = "";
std::string mFileName = "";
Expand Down
189 changes: 160 additions & 29 deletions Detectors/ITSMFT/common/workflow/src/DigitReaderSpec.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
#include "Framework/ConfigParamRegistry.h"
#include "Framework/Logger.h"
#include "ITSMFTWorkflow/DigitReaderSpec.h"
#include "ITSMFTBase/DPLAlpideParam.h"
#include "ITSMFTReconstruction/ChipMappingITS.h"
#include "ITSMFTReconstruction/ChipMappingMFT.h"
#include "SimulationDataFormat/MCCompLabel.h"
#include "SimulationDataFormat/ConstMCTruthContainer.h"
#include "SimulationDataFormat/IOMCTruthContainerView.h"
#include "DataFormatsITSMFT/PhysTrigger.h"
#include "CommonUtils/NameConf.h"
#include "CommonDataFormat/IRFrame.h"
#include "CommonUtils/IRFrameSelector.h"
#include "CCDB/BasicCCDBManager.h"
#include <cassert>

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

void DigitReader::run(ProcessingContext& pc)
{
auto ent = mTree->GetReadEntry() + 1;
assert(ent < mTree->GetEntries()); // this should not happen

o2::dataformats::IOMCTruthContainerView* plabels = nullptr;
if (mUseMC) {
mTree->SetBranchAddress(mDigtMCTruthBranchName.c_str(), &plabels);
}
mTree->GetEntry(ent);
LOG(info) << mDetName << "DigitReader pushes " << mDigROFRec.size() << " ROFRecords, "
<< mDigits.size() << " digits at entry " << ent;

// This is a very ugly way of providing DataDescription, which anyway does not need to contain detector name.
// To be fixed once the names-definition class is ready
pc.outputs().snapshot(Output{mOrigin, "DIGITSROF", 0}, mDigROFRec);
pc.outputs().snapshot(Output{mOrigin, "DIGITS", 0}, mDigits);
if (mUseCalib) {
pc.outputs().snapshot(Output{mOrigin, "GBTCALIB", 0}, mCalib);
const auto& tinfo = pc.services().get<o2::framework::TimingInfo>();
if (tinfo.globalRunNumberChanged) { // new run is starting: 1st call
// TODO: we have to find a way define CCDBInput for IRFrames mode only using DPL fetcher
auto& ccdb = o2::ccdb::BasicCCDBManager::instance();
auto rlim = ccdb.getRunDuration(tinfo.runNumber);
long ts = (rlim.first + rlim.second) / 2;
if (mOrigin == o2::header::gDataOriginITS) {
ccdb.getForTimeStamp<o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>>("ITS/Config/AlpideParam", ts);
const auto& alpideParam = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>::Instance();
mROFBiasInBC = alpideParam.roFrameBiasInBC;
mROFLengthInBC = alpideParam.roFrameLengthInBC;
mNRUs = o2::itsmft::ChipMappingITS::getNRUs();
} else {
ccdb.getForTimeStamp<o2::itsmft::DPLAlpideParam<o2::detectors::DetID::MFT>>("MFT/Config/AlpideParam", ts);
const auto& alpideParam = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::MFT>::Instance();
mROFBiasInBC = alpideParam.roFrameBiasInBC;
mROFLengthInBC = alpideParam.roFrameLengthInBC;
mNRUs = o2::itsmft::ChipMappingMFT::getNRUs();
}
}
if (mTriggerOut) {
std::vector<o2::itsmft::PhysTrigger> dummyTrig;
pc.outputs().snapshot(Output{mOrigin, "PHYSTRIG", 0}, dummyTrig);
gsl::span<const o2::dataformats::IRFrame> irFrames{};
if (mUseIRFrames) {
irFrames = pc.inputs().get<gsl::span<o2::dataformats::IRFrame>>("driverInfo");
}
if (mUseMC) {
auto& sharedlabels = pc.outputs().make<o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>>(Output{mOrigin, "DIGITSMCTR", 0});
plabels->copyandflatten(sharedlabels);
delete plabels;
pc.outputs().snapshot(Output{mOrigin, "DIGITSMC2ROF", 0}, mDigMC2ROFs);
static o2::dataformats::IOMCTruthContainerView* plabels = nullptr;
if (mUseMC && !plabels) {
mTree->SetBranchAddress(mDigtMCTruthBranchName.c_str(), &plabels);
}
auto ent = mTree->GetReadEntry();

if (mTree->GetReadEntry() + 1 >= mTree->GetEntries()) {
pc.services().get<ControlService>().endOfStream();
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
if (!mUseIRFrames) {
ent++;
assert(ent < mTree->GetEntries()); // this should not happen
mTree->GetEntry(ent);
LOG(info) << mDetName << "DigitReader pushes " << mDigROFRec.size() << " ROFRecords, " << mDigits.size() << " digits at entry " << ent;
pc.outputs().snapshot(Output{mOrigin, "DIGITSROF", 0}, mDigROFRec);
pc.outputs().snapshot(Output{mOrigin, "DIGITS", 0}, mDigits);
if (mUseCalib) {
pc.outputs().snapshot(Output{mOrigin, "GBTCALIB", 0}, mCalib);
}
if (mTriggerOut) {
std::vector<o2::itsmft::PhysTrigger> dummyTrig;
pc.outputs().snapshot(Output{mOrigin, "PHYSTRIG", 0}, dummyTrig);
}
if (mUseMC) {
auto& sharedlabels = pc.outputs().make<o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>>(Output{mOrigin, "DIGITSMCTR", 0});
plabels->copyandflatten(sharedlabels);
delete plabels;
plabels = nullptr;
pc.outputs().snapshot(Output{mOrigin, "DIGITSMC2ROF", 0}, mDigMC2ROFs);
}
if (mTree->GetReadEntry() + 1 >= mTree->GetEntries()) {
pc.services().get<ControlService>().endOfStream();
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
}
} else { // need to select particulars IRs range, presumably from the same tree entry
std::vector<o2::itsmft::Digit> digitsSel;
std::vector<o2::itsmft::GBTCalibData> calibSel;
std::vector<o2::itsmft::ROFRecord> digROFRecSel;
std::vector<o2::itsmft::MC2ROFRecord> digMC2ROFsSel;
o2::dataformats::MCTruthContainer<o2::MCCompLabel> digitLabelsSel;

if (irFrames.size()) { // we assume the IRFrames are in the increasing order
if (ent < 0) {
ent++;
}
o2::utils::IRFrameSelector irfSel;
irfSel.setSelectedIRFrames(irFrames, 0, 0, mROFBiasInBC, true);
const auto irMin = irFrames.front().getMin();
const auto irMax = irFrames.back().getMax();
LOGP(info, "Selecting IRFrame {}-{}", irMin.asString(), irMax.asString());
while (ent < mTree->GetEntries()) {
// do we need to read a new entry?
if (ent > mTree->GetReadEntry()) {
if (mUseMC) {
delete plabels;
plabels = nullptr;
mConstLabels.clear();
mTree->SetBranchAddress(mDigtMCTruthBranchName.c_str(), &plabels);
}
mTree->GetEntry(ent);
if (mUseMC) {
plabels->copyandflatten(mConstLabels);
delete plabels;
plabels = nullptr;
}
}
std::vector<int> rofOld2New;
rofOld2New.resize(mDigROFRec.size(), -1);

if (mDigROFRec.front().getBCData() <= irMax && mDigROFRec.back().getBCData() >= irMin) { // there is an overlap
for (int irof = 0; irof < (int)mDigROFRec.size(); irof++) {
const auto& rof = mDigROFRec[irof];
if (irfSel.check({rof.getBCData(), rof.getBCData() + mROFLengthInBC - 1}) != -1) {
rofOld2New[irof] = (int)digROFRecSel.size();
LOGP(debug, "Adding selected ROF {}", rof.getBCData().asString());
digROFRecSel.push_back(rof);
int offs = digitsSel.size();
digROFRecSel.back().setFirstEntry(offs);
std::copy(mDigits.begin() + rof.getFirstEntry(), mDigits.begin() + rof.getFirstEntry() + rof.getNEntries(), std::back_inserter(digitsSel));
for (int id = 0; id < rof.getNEntries(); id++) { // copy MC info
digitLabelsSel.addElements(id + offs, mConstLabels.getLabels(id + rof.getFirstEntry()));
}
if (mCalib.size() >= size_t((irof + 1) * mNRUs)) {
std::copy(mCalib.begin() + irof * mNRUs, mCalib.begin() + (irof + 1) * mNRUs, std::back_inserter(calibSel));
}
}
}
}
if (mUseMC) {
digMC2ROFsSel = mDigMC2ROFs;
for (auto& mc2rof : digMC2ROFsSel) {
if (mc2rof.rofRecordID < 0) {
continue; // did not contribute even to the original data
}
unsigned int mn = 0xffff, mx = 0;
for (int ir = mc2rof.minROF; ir <= mc2rof.maxROF; ir++) {
if (rofOld2New[ir] >= 0) { // used
mx = rofOld2New[ir];
if (mn > mx) {
mn = mx;
}
}
}
mc2rof.rofRecordID = mn == 0xffff ? -1 : int(mn);
mc2rof.minROF = mn;
mc2rof.maxROF = mx;
}
}
if (mDigROFRec.back().getBCData() < irMax) { // need to check the next entry
ent++;
continue;
}
break; // push collected data
}
}
pc.outputs().snapshot(Output{mOrigin, "DIGITSROF", 0}, digROFRecSel);
pc.outputs().snapshot(Output{mOrigin, "DIGITS", 0}, digitsSel);
if (mUseCalib) {
pc.outputs().snapshot(Output{mOrigin, "GBTCALIB", 0}, calibSel);
}
if (mTriggerOut) {
std::vector<o2::itsmft::PhysTrigger> dummyTrig;
pc.outputs().snapshot(Output{mOrigin, "PHYSTRIG", 0}, dummyTrig);
}
if (mUseMC) {
auto& sharedlabels = pc.outputs().make<o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>>(Output{mOrigin, "DIGITSMCTR", 0});
digitLabelsSel.flatten_to(sharedlabels);
pc.outputs().snapshot(Output{mOrigin, "DIGITSMC2ROF", 0}, digMC2ROFsSel);
}

if (!irFrames.size() || irFrames.back().isLast()) {
pc.services().get<ControlService>().endOfStream();
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
}
}
}

Expand Down

0 comments on commit 9d39dbc

Please sign in to comment.