|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \file ClusterReaderSpec.cxx |
| 13 | +/// \brief The TF3 cluster reader (ClusterReaderSpec.h). |
| 14 | + |
| 15 | +#include "IOTOFWorkflow/ClusterReaderSpec.h" |
| 16 | + |
| 17 | +#include "Framework/ConfigParamRegistry.h" |
| 18 | +#include "Framework/ControlService.h" |
| 19 | +#include "Framework/Logger.h" |
| 20 | +#include "Headers/DataHeader.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | +#include <stdexcept> |
| 24 | + |
| 25 | +using namespace o2::framework; |
| 26 | + |
| 27 | +namespace o2::iotof |
| 28 | +{ |
| 29 | + |
| 30 | +void ClusterReader::init(InitContext& ic) |
| 31 | +{ |
| 32 | + connectTree(ic.options().get<std::string>("tf3-cluster-infile")); |
| 33 | +} |
| 34 | + |
| 35 | +void ClusterReader::run(ProcessingContext& pc) |
| 36 | +{ |
| 37 | + const auto entry = mTree->GetReadEntry() + 1; |
| 38 | + // A timeframe without a collision has no entry: publish empty streams, so |
| 39 | + // that the consumers still see the timeframe (as TRK's reader does). |
| 40 | + const bool noEntry = entry >= mTree->GetEntries(); |
| 41 | + if (noEntry) { |
| 42 | + LOG(info) << "TF3ClusterReader: no entry to read, publishing empty output"; |
| 43 | + } else { |
| 44 | + mTree->GetEntry(entry); |
| 45 | + } |
| 46 | + static const std::vector<o2::iotof::Cluster> noClusters; |
| 47 | + static const std::vector<unsigned char> noPatterns; |
| 48 | + static const std::vector<o2::itsmft::ROFRecord> noROFs; |
| 49 | + static const o2::dataformats::MCTruthContainer<o2::MCCompLabel> noLabels; |
| 50 | + const auto& clusters = noEntry ? noClusters : *mClusters; |
| 51 | + LOG(info) << "TF3ClusterReader pushes " << clusters.size() << " clusters at entry " << entry; |
| 52 | + pc.outputs().snapshot(Output{o2::header::gDataOriginTF3, "CLUSTERS", 0}, clusters); |
| 53 | + pc.outputs().snapshot(Output{o2::header::gDataOriginTF3, "PATTERNS", 0}, noEntry ? noPatterns : *mPatterns); |
| 54 | + pc.outputs().snapshot(Output{o2::header::gDataOriginTF3, "CLUSTERSROF", 0}, noEntry ? noROFs : *mROFs); |
| 55 | + if (mUseMC) { |
| 56 | + pc.outputs().snapshot(Output{o2::header::gDataOriginTF3, "CLUSTERSMCTR", 0}, noEntry ? noLabels : *mLabels); |
| 57 | + } |
| 58 | + if (noEntry || mTree->GetReadEntry() + 1 >= mTree->GetEntries()) { |
| 59 | + pc.services().get<ControlService>().endOfStream(); |
| 60 | + pc.services().get<ControlService>().readyToQuit(QuitRequest::Me); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void ClusterReader::connectTree(const std::string& filename) |
| 65 | +{ |
| 66 | + mTree.reset(); |
| 67 | + mFile.reset(TFile::Open(filename.c_str())); |
| 68 | + if (!mFile || mFile->IsZombie()) { |
| 69 | + throw std::runtime_error("TF3ClusterReader: cannot open " + filename); |
| 70 | + } |
| 71 | + mTree.reset(mFile->Get<TTree>("o2sim")); |
| 72 | + if (!mTree) { |
| 73 | + throw std::runtime_error("TF3ClusterReader: no tree o2sim in " + filename); |
| 74 | + } |
| 75 | + const auto connect = [this](const char* name, auto*& address) { |
| 76 | + if (!mTree->GetBranch(name)) { |
| 77 | + throw std::runtime_error(std::format("TF3ClusterReader: no branch {}", name)); |
| 78 | + } |
| 79 | + if (const Int_t status = mTree->SetBranchAddress(name, &address); status < 0) { |
| 80 | + throw std::runtime_error(std::format("TF3ClusterReader: cannot set the address of {} ({})", name, status)); |
| 81 | + } |
| 82 | + }; |
| 83 | + connect("TF3Cluster", mClusters); |
| 84 | + connect("TF3ClusterPatt", mPatterns); |
| 85 | + connect("TF3ClusterROF", mROFs); |
| 86 | + if (mUseMC) { |
| 87 | + connect("TF3ClusterMCTruth", mLabels); |
| 88 | + } |
| 89 | + LOG(info) << "TF3ClusterReader: " << filename << " with " << mTree->GetEntries() << " entries"; |
| 90 | +} |
| 91 | + |
| 92 | +DataProcessorSpec getIOTOFClusterReaderSpec(bool useMC) |
| 93 | +{ |
| 94 | + std::vector<OutputSpec> outputs{{o2::header::gDataOriginTF3, "CLUSTERS", 0, Lifetime::Timeframe}, |
| 95 | + {o2::header::gDataOriginTF3, "PATTERNS", 0, Lifetime::Timeframe}, |
| 96 | + {o2::header::gDataOriginTF3, "CLUSTERSROF", 0, Lifetime::Timeframe}}; |
| 97 | + if (useMC) { |
| 98 | + outputs.emplace_back(o2::header::gDataOriginTF3, "CLUSTERSMCTR", 0, Lifetime::Timeframe); |
| 99 | + } |
| 100 | + return DataProcessorSpec{ |
| 101 | + .name = "tf3-cluster-reader", |
| 102 | + .inputs = Inputs{}, |
| 103 | + .outputs = outputs, |
| 104 | + .algorithm = AlgorithmSpec{adaptFromTask<ClusterReader>(useMC)}, |
| 105 | + .options = Options{{"tf3-cluster-infile", VariantType::String, "tf3clusters.root", {"Name of the input cluster file"}}}, |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace o2::iotof |
0 commit comments