Skip to content

Commit 8055c16

Browse files
shahor02sawenzel
authored andcommitted
added TPC TrackReader DPL specs
1 parent 768db4a commit 8055c16

3 files changed

Lines changed: 185 additions & 0 deletions

File tree

Detectors/TPC/workflow/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ set(SRCS
1818
src/ClustererSpec.cxx
1919
src/ClusterDecoderRawSpec.cxx
2020
src/CATrackerSpec.cxx
21+
#
22+
src/TrackReaderSpec.cxx
2123
)
2224

2325
## TODO: feature of macro, it deletes the variables we pass to it, set them again
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.h
12+
13+
#ifndef O2_TPC_TRACKREADER
14+
#define O2_TPC_TRACKREADER
15+
16+
#include "TFile.h"
17+
18+
#include "Framework/DataProcessorSpec.h"
19+
#include "Framework/Task.h"
20+
#include "Headers/DataHeader.h"
21+
#include "DataFormatsTPC/TrackTPC.h"
22+
#include "SimulationDataFormat/MCCompLabel.h"
23+
#include "SimulationDataFormat/MCTruthContainer.h"
24+
25+
using namespace o2::framework;
26+
27+
namespace o2
28+
{
29+
namespace TPC
30+
{
31+
///< DPL device to read and send the TPC tracks (+MC) info
32+
33+
class TrackReader : public Task
34+
{
35+
public:
36+
TrackReader(bool useMC = true);
37+
~TrackReader() override = default;
38+
void init(InitContext& ic) final;
39+
void run(ProcessingContext& pc) final;
40+
41+
private:
42+
void accumulate();
43+
44+
std::vector<o2::TPC::TrackTPC>*mTracksInp = nullptr, mTracksOut;
45+
o2::dataformats::MCTruthContainer<o2::MCCompLabel>*mMCTruthInp = nullptr, mMCTruthOut;
46+
47+
bool mFinished = false;
48+
bool mUseMC = true; // use MC truth
49+
50+
std::string mInputFileName = "tpctracks.root";
51+
std::string mTrackTreeName = "events";
52+
std::string mTrackBranchName = "Tracks";
53+
std::string mTrackMCTruthBranchName = "TracksMCTruth";
54+
};
55+
56+
/// create a processor spec
57+
/// read TPC track data from a root file
58+
framework::DataProcessorSpec getTPCTrackReaderSpec(bool useMC = true);
59+
60+
} // namespace TPC
61+
} // namespace o2
62+
63+
#endif /* O2_TPC_TRACKREADER */
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)