From 2a24b2ce44ccfa4f2b3b17b6edffe5bbfa84f817 Mon Sep 17 00:00:00 2001 From: Maximiliano Puccio Date: Thu, 19 Nov 2026 18:11:09 +0100 Subject: [PATCH] IOTOF: cluster reader o2-alice3-iotof-reco-workflow writes the TF3 clusters (ClusterWriterSpec) but nothing reads them back. getIOTOFClusterReaderSpec publishes TF3/CLUSTERS, PATTERNS, CLUSTERSROF and, with MC, CLUSTERSMCTR (subspec 0) from the writer's tree, one timeframe per entry, as TRK's reader does: an empty timeframe past the last entry. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit f8e7ab21ae7b8cf6fb4624769471b34f1fbc2b5f) --- .../ALICE3/IOTOF/workflow/CMakeLists.txt | 2 + .../include/IOTOFWorkflow/ClusterReaderSpec.h | 65 +++++++++++ .../IOTOF/workflow/src/ClusterReaderSpec.cxx | 109 ++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 Detectors/Upgrades/ALICE3/IOTOF/workflow/include/IOTOFWorkflow/ClusterReaderSpec.h create mode 100644 Detectors/Upgrades/ALICE3/IOTOF/workflow/src/ClusterReaderSpec.cxx diff --git a/Detectors/Upgrades/ALICE3/IOTOF/workflow/CMakeLists.txt b/Detectors/Upgrades/ALICE3/IOTOF/workflow/CMakeLists.txt index 14a0e215587d8..5a1982824ae26 100644 --- a/Detectors/Upgrades/ALICE3/IOTOF/workflow/CMakeLists.txt +++ b/Detectors/Upgrades/ALICE3/IOTOF/workflow/CMakeLists.txt @@ -15,12 +15,14 @@ o2_add_library(IOTOFWorkflow src/DigitWriterSpec.cxx src/ClustererSpec.cxx src/ClusterWriterSpec.cxx + src/ClusterReaderSpec.cxx src/RecoWorkflow.cxx PUBLIC_LINK_LIBRARIES O2::Framework O2::DataFormatsIOTOF O2::DataFormatsITSMFT O2::IOTOFReconstruction O2::DPLUtils + O2::SimulationDataFormat ) o2_add_executable(reco-workflow diff --git a/Detectors/Upgrades/ALICE3/IOTOF/workflow/include/IOTOFWorkflow/ClusterReaderSpec.h b/Detectors/Upgrades/ALICE3/IOTOF/workflow/include/IOTOFWorkflow/ClusterReaderSpec.h new file mode 100644 index 0000000000000..ce11c37f4f1d9 --- /dev/null +++ b/Detectors/Upgrades/ALICE3/IOTOF/workflow/include/IOTOFWorkflow/ClusterReaderSpec.h @@ -0,0 +1,65 @@ +// 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. + +/// \file ClusterReaderSpec.h +/// \brief The TF3 clusters of o2-alice3-iotof-reco-workflow's cluster file +/// (ClusterWriterSpec.h), one timeframe per entry. + +#ifndef O2_IOTOF_CLUSTERREADERSPEC_H +#define O2_IOTOF_CLUSTERREADERSPEC_H + +#include "DataFormatsIOTOF/Cluster.h" +#include "DataFormatsITSMFT/ROFRecord.h" +#include "Framework/DataProcessorSpec.h" +#include "Framework/Task.h" +#include "SimulationDataFormat/MCCompLabel.h" +#include "SimulationDataFormat/MCTruthContainer.h" + +#include +#include + +#include +#include +#include + +namespace o2::iotof +{ + +/// Publishes TF3/CLUSTERS, TF3/PATTERNS, TF3/CLUSTERSROF and, with MC, +/// TF3/CLUSTERSMCTR (subspec 0) from the tree `o2sim` of `--tf3-cluster-infile`, +/// as the clusterer publishes them; an empty timeframe past the tree's last +/// entry. +class ClusterReader : public o2::framework::Task +{ + public: + explicit ClusterReader(bool useMC) : mUseMC(useMC) {} + void init(o2::framework::InitContext& ic) final; + void run(o2::framework::ProcessingContext& pc) final; + + private: + void connectTree(const std::string& filename); + + std::unique_ptr mFile; + std::unique_ptr mTree; + std::vector* mClusters = nullptr; + std::vector* mPatterns = nullptr; + std::vector* mROFs = nullptr; + o2::dataformats::MCTruthContainer* mLabels = nullptr; + bool mUseMC = false; +}; + +/// The reader's device, `tf3-cluster-reader`; its file `--tf3-cluster-infile` +/// (tf3clusters.root by default). +o2::framework::DataProcessorSpec getIOTOFClusterReaderSpec(bool useMC); + +} // namespace o2::iotof + +#endif // O2_IOTOF_CLUSTERREADERSPEC_H diff --git a/Detectors/Upgrades/ALICE3/IOTOF/workflow/src/ClusterReaderSpec.cxx b/Detectors/Upgrades/ALICE3/IOTOF/workflow/src/ClusterReaderSpec.cxx new file mode 100644 index 0000000000000..49e0d7ea3ccf0 --- /dev/null +++ b/Detectors/Upgrades/ALICE3/IOTOF/workflow/src/ClusterReaderSpec.cxx @@ -0,0 +1,109 @@ +// 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. + +/// \file ClusterReaderSpec.cxx +/// \brief The TF3 cluster reader (ClusterReaderSpec.h). + +#include "IOTOFWorkflow/ClusterReaderSpec.h" + +#include "Framework/ConfigParamRegistry.h" +#include "Framework/ControlService.h" +#include "Framework/Logger.h" +#include "Headers/DataHeader.h" + +#include +#include + +using namespace o2::framework; + +namespace o2::iotof +{ + +void ClusterReader::init(InitContext& ic) +{ + connectTree(ic.options().get("tf3-cluster-infile")); +} + +void ClusterReader::run(ProcessingContext& pc) +{ + const auto entry = mTree->GetReadEntry() + 1; + // A timeframe without a collision has no entry: publish empty streams, so + // that the consumers still see the timeframe (as TRK's reader does). + const bool noEntry = entry >= mTree->GetEntries(); + if (noEntry) { + LOG(info) << "TF3ClusterReader: no entry to read, publishing empty output"; + } else { + mTree->GetEntry(entry); + } + static const std::vector noClusters; + static const std::vector noPatterns; + static const std::vector noROFs; + static const o2::dataformats::MCTruthContainer noLabels; + const auto& clusters = noEntry ? noClusters : *mClusters; + LOG(info) << "TF3ClusterReader pushes " << clusters.size() << " clusters at entry " << entry; + pc.outputs().snapshot(Output{o2::header::gDataOriginTF3, "CLUSTERS", 0}, clusters); + pc.outputs().snapshot(Output{o2::header::gDataOriginTF3, "PATTERNS", 0}, noEntry ? noPatterns : *mPatterns); + pc.outputs().snapshot(Output{o2::header::gDataOriginTF3, "CLUSTERSROF", 0}, noEntry ? noROFs : *mROFs); + if (mUseMC) { + pc.outputs().snapshot(Output{o2::header::gDataOriginTF3, "CLUSTERSMCTR", 0}, noEntry ? noLabels : *mLabels); + } + if (noEntry || mTree->GetReadEntry() + 1 >= mTree->GetEntries()) { + pc.services().get().endOfStream(); + pc.services().get().readyToQuit(QuitRequest::Me); + } +} + +void ClusterReader::connectTree(const std::string& filename) +{ + mTree.reset(); + mFile.reset(TFile::Open(filename.c_str())); + if (!mFile || mFile->IsZombie()) { + throw std::runtime_error("TF3ClusterReader: cannot open " + filename); + } + mTree.reset(mFile->Get("o2sim")); + if (!mTree) { + throw std::runtime_error("TF3ClusterReader: no tree o2sim in " + filename); + } + const auto connect = [this](const char* name, auto*& address) { + if (!mTree->GetBranch(name)) { + throw std::runtime_error(std::format("TF3ClusterReader: no branch {}", name)); + } + if (const Int_t status = mTree->SetBranchAddress(name, &address); status < 0) { + throw std::runtime_error(std::format("TF3ClusterReader: cannot set the address of {} ({})", name, status)); + } + }; + connect("TF3Cluster", mClusters); + connect("TF3ClusterPatt", mPatterns); + connect("TF3ClusterROF", mROFs); + if (mUseMC) { + connect("TF3ClusterMCTruth", mLabels); + } + LOG(info) << "TF3ClusterReader: " << filename << " with " << mTree->GetEntries() << " entries"; +} + +DataProcessorSpec getIOTOFClusterReaderSpec(bool useMC) +{ + std::vector outputs{{o2::header::gDataOriginTF3, "CLUSTERS", 0, Lifetime::Timeframe}, + {o2::header::gDataOriginTF3, "PATTERNS", 0, Lifetime::Timeframe}, + {o2::header::gDataOriginTF3, "CLUSTERSROF", 0, Lifetime::Timeframe}}; + if (useMC) { + outputs.emplace_back(o2::header::gDataOriginTF3, "CLUSTERSMCTR", 0, Lifetime::Timeframe); + } + return DataProcessorSpec{ + .name = "tf3-cluster-reader", + .inputs = Inputs{}, + .outputs = outputs, + .algorithm = AlgorithmSpec{adaptFromTask(useMC)}, + .options = Options{{"tf3-cluster-infile", VariantType::String, "tf3clusters.root", {"Name of the input cluster file"}}}, + }; +} + +} // namespace o2::iotof