|
| 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 "TPCWorkflow/TrackReaderSpec.h" |
| 19 | + |
| 20 | +using namespace o2::framework; |
| 21 | + |
| 22 | +namespace o2 |
| 23 | +{ |
| 24 | +namespace TPC |
| 25 | +{ |
| 26 | + |
| 27 | +TrackReader::TrackReader(bool useMC) |
| 28 | +{ |
| 29 | + mUseMC = useMC; |
| 30 | +} |
| 31 | + |
| 32 | +void TrackReader::init(InitContext& ic) |
| 33 | +{ |
| 34 | + mInputFileName = ic.options().get<std::string>("tpc-tracks-infile"); |
| 35 | +} |
| 36 | + |
| 37 | +void TrackReader::run(ProcessingContext& pc) |
| 38 | +{ |
| 39 | + |
| 40 | + if (mFinished) { |
| 41 | + return; |
| 42 | + } |
| 43 | + accumulate(); |
| 44 | + |
| 45 | + LOG(INFO) << "TPCTrackReader pushes " << mTracksOut.size() << " tracks"; |
| 46 | + pc.outputs().snapshot(Output{ "TPC", "TRACKS", 0, Lifetime::Timeframe }, mTracksOut); |
| 47 | + if (mUseMC) { |
| 48 | + pc.outputs().snapshot(Output{ "TPC", "TRACKSMCLBL", 0, Lifetime::Timeframe }, mMCTruthOut); |
| 49 | + } |
| 50 | + |
| 51 | + mFinished = true; |
| 52 | + pc.services().get<ControlService>().readyToQuit(false); |
| 53 | +} |
| 54 | + |
| 55 | +void TrackReader::accumulate() |
| 56 | +{ |
| 57 | + // load data from files |
| 58 | + TFile trFile(mInputFileName.c_str(), "read"); |
| 59 | + if (trFile.IsZombie()) { |
| 60 | + LOG(FATAL) << "Failed to open tracks file " << mInputFileName; |
| 61 | + } |
| 62 | + TTree* trTree = (TTree*)trFile.Get(mTrackTreeName.c_str()); |
| 63 | + if (!trTree) { |
| 64 | + LOG(FATAL) << "Failed to load tracks tree " << mTrackTreeName << " from " << mInputFileName; |
| 65 | + } |
| 66 | + LOG(INFO) << "Loaded tracks tree " << mTrackTreeName << " from " << mInputFileName; |
| 67 | + |
| 68 | + trTree->SetBranchAddress(mTrackBranchName.c_str(), &mTracksInp); |
| 69 | + if (mUseMC) { |
| 70 | + if (trTree->GetBranch(mTrackMCTruthBranchName.c_str())) { |
| 71 | + trTree->SetBranchAddress(mTrackMCTruthBranchName.c_str(), &mMCTruthInp); |
| 72 | + LOG(INFO) << "Will use MC-truth from " << mTrackMCTruthBranchName; |
| 73 | + } else { |
| 74 | + LOG(INFO) << "MC-truth is missing"; |
| 75 | + mUseMC = false; |
| 76 | + } |
| 77 | + } |
| 78 | + int nEnt = trTree->GetEntries(); |
| 79 | + if (nEnt == 1) { |
| 80 | + trTree->GetEntry(0); |
| 81 | + mTracksOut.swap(*mTracksInp); |
| 82 | + if (mUseMC) { |
| 83 | + mMCTruthOut.mergeAtBack(*mMCTruthInp); |
| 84 | + } |
| 85 | + } else { |
| 86 | + int lastEntry = -1; |
| 87 | + int ntrAcc = 0; |
| 88 | + for (int iev = 0; iev < nEnt; iev++) { |
| 89 | + trTree->GetEntry(0); |
| 90 | + auto tr0 = mTracksInp->begin(); |
| 91 | + auto tr1 = mTracksInp->end(); |
| 92 | + std::copy(tr0, tr1, std::back_inserter(mTracksOut)); |
| 93 | + // MC |
| 94 | + if (mUseMC) { |
| 95 | + mMCTruthOut.mergeAtBack(*mMCTruthInp); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +DataProcessorSpec getTPCTrackReaderSpec(bool useMC) |
| 102 | +{ |
| 103 | + std::vector<OutputSpec> outputSpec; |
| 104 | + outputSpec.emplace_back("TPC", "TRACKS", 0, Lifetime::Timeframe); |
| 105 | + if (useMC) { |
| 106 | + outputSpec.emplace_back("TPC", "TRACKSMCLBL", 0, Lifetime::Timeframe); |
| 107 | + } |
| 108 | + |
| 109 | + return DataProcessorSpec{ |
| 110 | + "tpc-track-reader", |
| 111 | + Inputs{}, |
| 112 | + outputSpec, |
| 113 | + AlgorithmSpec{ adaptFromTask<TrackReader>(useMC) }, |
| 114 | + Options{ |
| 115 | + { "tpc-tracks-infile", VariantType::String, "tpctracks.root", { "Name of the input track file" } } } |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace TPC |
| 120 | +} // namespace o2 |
0 commit comments