|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +/// @file TrackReaderSpec.cxx |
| 12 | + |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#include "TTree.h" |
| 16 | + |
| 17 | +#include "Framework/ControlService.h" |
| 18 | +#include "ITSWorkflow/TrackReaderSpec.h" |
| 19 | + |
| 20 | +using namespace o2::framework; |
| 21 | +using namespace o2::ITS; |
| 22 | + |
| 23 | +namespace o2 |
| 24 | +{ |
| 25 | +namespace ITS |
| 26 | +{ |
| 27 | + |
| 28 | +TrackReader::TrackReader(bool useMC) |
| 29 | +{ |
| 30 | + mUseMC = useMC; |
| 31 | +} |
| 32 | + |
| 33 | +void TrackReader::init(InitContext& ic) |
| 34 | +{ |
| 35 | + mInputFileName = ic.options().get<std::string>("its-tracks-infile"); |
| 36 | +} |
| 37 | + |
| 38 | +void TrackReader::run(ProcessingContext& pc) |
| 39 | +{ |
| 40 | + |
| 41 | + if (mFinished) { |
| 42 | + return; |
| 43 | + } |
| 44 | + accumulate(); |
| 45 | + |
| 46 | + LOG(INFO) << "ITSTrackReader pushes " << mROFRecOut.size() << " ROFRecords," |
| 47 | + << mTracksOut.size() << " tracks"; |
| 48 | + pc.outputs().snapshot(Output{ mOrigin, "ITSTrackROF", 0, Lifetime::Timeframe }, mROFRecOut); |
| 49 | + pc.outputs().snapshot(Output{ mOrigin, "TRACKS", 0, Lifetime::Timeframe }, mTracksOut); |
| 50 | + if (mUseMC) { |
| 51 | + pc.outputs().snapshot(Output{ mOrigin, "TRACKSMCTR", 0, Lifetime::Timeframe }, mMCTruthOut); |
| 52 | + } |
| 53 | + |
| 54 | + mFinished = true; |
| 55 | + pc.services().get<ControlService>().readyToQuit(false); |
| 56 | +} |
| 57 | + |
| 58 | +void TrackReader::accumulate() |
| 59 | +{ |
| 60 | + // load data from files |
| 61 | + TFile trFile(mInputFileName.c_str(), "read"); |
| 62 | + if (trFile.IsZombie()) { |
| 63 | + LOG(FATAL) << "Failed to open tracks file " << mInputFileName; |
| 64 | + } |
| 65 | + TTree* trTree = (TTree*)trFile.Get(mTrackTreeName.c_str()); |
| 66 | + if (!trTree) { |
| 67 | + LOG(FATAL) << "Failed to load tracks tree " << mTrackTreeName << " from " << mInputFileName; |
| 68 | + } |
| 69 | + TTree* rofTree = (TTree*)trFile.Get(mROFTreeName.c_str()); |
| 70 | + if (!rofTree) { |
| 71 | + LOG(FATAL) << "Failed to load tracks ROF tree " << rofTree << " from " << mInputFileName; |
| 72 | + } |
| 73 | + LOG(INFO) << "Loaded tracks tree " << mTrackTreeName << " and ROFRecords " << mROFTreeName |
| 74 | + << " from " << mInputFileName; |
| 75 | + |
| 76 | + rofTree->SetBranchAddress(mROFTreeName.c_str(), &mROFRecInp); |
| 77 | + trTree->SetBranchAddress(mTrackBranchName.c_str(), &mTracksInp); |
| 78 | + if (mUseMC) { |
| 79 | + if (trTree->GetBranch(mTrackMCTruthBranchName.c_str())) { |
| 80 | + trTree->SetBranchAddress(mTrackMCTruthBranchName.c_str(), &mMCTruthInp); |
| 81 | + LOG(INFO) << "Will use MC-truth from " << mTrackMCTruthBranchName; |
| 82 | + } else { |
| 83 | + LOG(INFO) << "MC-truth is missing"; |
| 84 | + mUseMC = false; |
| 85 | + } |
| 86 | + } |
| 87 | + // it is possible that the track data is stored in multiple entries, in this case we need to refill to 1 single vector |
| 88 | + if (rofTree->GetEntries() > 1) { |
| 89 | + LOG(FATAL) << "Tracks ROFRecords tree has " << rofTree->GetEntries() << " entries instead of 1"; |
| 90 | + } |
| 91 | + rofTree->GetEntry(0); |
| 92 | + int nROFs = mROFRecInp->size(); |
| 93 | + mROFRecOut.swap(*mROFRecInp); |
| 94 | + int nEnt = trTree->GetEntries(); |
| 95 | + if (nEnt == 1) { |
| 96 | + trTree->GetEntry(0); |
| 97 | + mTracksOut.swap(*mTracksInp); |
| 98 | + if (mUseMC) { |
| 99 | + mMCTruthOut.mergeAtBack(*mMCTruthInp); |
| 100 | + } |
| 101 | + } else { |
| 102 | + int lastEntry = -1; |
| 103 | + int ntrAcc = 0; |
| 104 | + for (auto& rof : mROFRecOut) { |
| 105 | + auto rEntry = rof.getROFEntry().getEvent(); |
| 106 | + if (lastEntry != rEntry) { |
| 107 | + trTree->GetEntry((lastEntry = rEntry)); |
| 108 | + } |
| 109 | + auto tr0 = mTracksInp->begin() + rof.getROFEntry().getIndex(); |
| 110 | + auto tr1 = tr0 + rof.getNROFEntries(); |
| 111 | + std::copy(tr0, tr1, std::back_inserter(mTracksOut)); |
| 112 | + // MC |
| 113 | + if (mUseMC) { |
| 114 | + mMCTruthOut.mergeAtBack(*mMCTruthInp); |
| 115 | + } |
| 116 | + rof.getROFEntry().setEvent(0); |
| 117 | + rof.getROFEntry().setIndex(ntrAcc); |
| 118 | + ntrAcc += rof.getNROFEntries(); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +DataProcessorSpec getITSTrackReaderSpec(bool useMC) |
| 124 | +{ |
| 125 | + std::vector<OutputSpec> outputSpec; |
| 126 | + outputSpec.emplace_back("ITS", "ITSTrackROF", 0, Lifetime::Timeframe); |
| 127 | + outputSpec.emplace_back("ITS", "TRACKS", 0, Lifetime::Timeframe); |
| 128 | + if (useMC) { |
| 129 | + outputSpec.emplace_back("ITS", "TRACKSMCTR", 0, Lifetime::Timeframe); |
| 130 | + } |
| 131 | + |
| 132 | + return DataProcessorSpec{ |
| 133 | + "its-track-reader", |
| 134 | + Inputs{}, |
| 135 | + outputSpec, |
| 136 | + AlgorithmSpec{ adaptFromTask<TrackReader>(useMC) }, |
| 137 | + Options{ |
| 138 | + { "its-tracks-infile", VariantType::String, "o2trac_its.root", { "Name of the input track file" } } } |
| 139 | + }; |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace ITS |
| 143 | +} // namespace o2 |
0 commit comments