Skip to content

Commit b94ea5d

Browse files
matthiasrichtershahor02
authored andcommitted
Simplifying writer specs in the TOF workflows
Using DPL utility RootTreeWriter and the MakeRootTreeWriterSpec generator and removing duplicated boiler-plate code.
1 parent 1d2e235 commit b94ea5d

File tree

8 files changed

+134
-361
lines changed

8 files changed

+134
-361
lines changed

Detectors/GlobalTrackingWorkflow/tofworkflow/include/TOFWorkflow/TOFCalibWriterSpec.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
#ifndef TOFWORKFLOW_TOFCALIBWRITER_H_
1414
#define TOFWORKFLOW_TOFCALIBWRITER_H_
1515

16-
#include "TFile.h"
17-
1816
#include "Framework/DataProcessorSpec.h"
19-
#include "Framework/Task.h"
20-
#include <string>
2117

2218
using namespace o2::framework;
2319

@@ -26,20 +22,6 @@ namespace o2
2622
namespace tof
2723
{
2824

29-
class TOFCalibWriter : public Task
30-
{
31-
public:
32-
TOFCalibWriter() = default;
33-
~TOFCalibWriter() override = default;
34-
void init(InitContext& ic) final;
35-
void run(ProcessingContext& pc) final;
36-
37-
private:
38-
bool mFinished = false;
39-
std::string mOutFileName; // read from workflow
40-
std::string mOutTreeName; // read from workflow
41-
};
42-
4325
/// create a processor spec
4426
/// write TOF calbi info in a root file
4527
o2::framework::DataProcessorSpec getTOFCalibWriterSpec();

Detectors/GlobalTrackingWorkflow/tofworkflow/include/TOFWorkflow/TOFMatchedWriterSpec.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
#ifndef TOFWORKFLOW_TOFMATCHEDWRITER_H_
1414
#define TOFWORKFLOW_TOFMATCHEDWRITER_H_
1515

16-
#include "TTree.h"
17-
#include "TFile.h"
1816
#include "Framework/DataProcessorSpec.h"
19-
#include "Framework/Task.h"
20-
#include <string>
2117

2218
using namespace o2::framework;
2319

@@ -26,23 +22,6 @@ namespace o2
2622
namespace tof
2723
{
2824

29-
class TOFMatchedWriter : public Task
30-
{
31-
public:
32-
TOFMatchedWriter(bool useMC = true) : mUseMC(useMC) {}
33-
~TOFMatchedWriter() override = default;
34-
void init(InitContext& ic) final;
35-
void run(ProcessingContext& pc) final;
36-
void endOfStream(EndOfStreamContext& ec) final;
37-
38-
private:
39-
std::unique_ptr<TFile> mFile = nullptr;
40-
std::unique_ptr<TTree> mTree = nullptr;
41-
bool mUseMC = true;
42-
std::string mOutFileName; // read from workflow
43-
std::string mOutTreeName; // read from workflow
44-
};
45-
4625
/// create a processor spec
4726
/// write TOF matching info in a root file
4827
o2::framework::DataProcessorSpec getTOFMatchedWriterSpec(bool useMC);

Detectors/GlobalTrackingWorkflow/tofworkflow/src/TOFCalibWriterSpec.cxx

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "TOFWorkflow/TOFCalibWriterSpec.h"
1414
#include "Framework/ConfigParamRegistry.h"
1515
#include "Framework/ControlService.h"
16+
#include "DPLUtils/MakeRootTreeWriterSpec.h"
1617
#include "Headers/DataHeader.h"
1718
#include <SimulationDataFormat/MCCompLabel.h>
1819
#include <SimulationDataFormat/MCTruthContainer.h>
@@ -29,64 +30,25 @@ namespace o2
2930
{
3031
namespace tof
3132
{
32-
using evIdx = o2::dataformats::EvIndex<int, int>;
33-
using OutputType = std::vector<o2::dataformats::CalibInfoTOF>;
3433

3534
template <typename T>
36-
TBranch* getOrMakeBranch(TTree& tree, std::string brname, T* ptr)
37-
{
38-
if (auto br = tree.GetBranch(brname.c_str())) {
39-
br->SetAddress(static_cast<void*>(&ptr));
40-
return br;
41-
}
42-
// otherwise make it
43-
return tree.Branch(brname.c_str(), ptr);
44-
}
45-
46-
void TOFCalibWriter::init(InitContext& ic)
47-
{
48-
// get the option from the init context
49-
mOutFileName = ic.options().get<std::string>("tof-calib-outfile");
50-
mOutTreeName = ic.options().get<std::string>("treename");
51-
}
52-
53-
void TOFCalibWriter::run(ProcessingContext& pc)
54-
{
55-
if (mFinished) {
56-
return;
57-
}
58-
59-
TFile outf(mOutFileName.c_str(), "recreate");
60-
if (outf.IsZombie()) {
61-
LOG(FATAL) << "Failed to open output file " << mOutFileName;
62-
}
63-
TTree tree(mOutTreeName.c_str(), "Tree of TOF calib infos");
64-
auto indata = pc.inputs().get<OutputType>("tofcalibinfo");
65-
LOG(INFO) << "RECEIVED MATCHED SIZE " << indata.size();
66-
67-
auto br = getOrMakeBranch(tree, "TOFCalibInfo", &indata);
68-
br->Fill();
69-
70-
tree.SetEntries(1);
71-
tree.Write();
72-
mFinished = true;
73-
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
74-
}
75-
35+
using BranchDefinition = MakeRootTreeWriterSpec::BranchDefinition<T>;
36+
using CalibInfosType = std::vector<o2::dataformats::CalibInfoTOF>;
7637
DataProcessorSpec getTOFCalibWriterSpec()
7738
{
78-
std::vector<InputSpec> inputs;
79-
inputs.emplace_back("tofcalibinfo", o2::header::gDataOriginTOF, "CALIBINFOS", 0, Lifetime::Timeframe);
80-
81-
return DataProcessorSpec{
82-
"TOFCalibWriter",
83-
inputs,
84-
{}, // no output
85-
AlgorithmSpec{adaptFromTask<TOFCalibWriter>()},
86-
Options{
87-
{"tof-calib-outfile", VariantType::String, "o2calib_tof.root", {"Name of the input file"}},
88-
{"treename", VariantType::String, "calibTOF", {"Name of top-level TTree"}},
89-
}};
39+
// A spectator for logging
40+
auto logger = [](CalibInfosType const& indata) {
41+
LOG(INFO) << "RECEIVED MATCHED SIZE " << indata.size();
42+
};
43+
return MakeRootTreeWriterSpec("TOFCalibWriter",
44+
"o2calib_tof.root",
45+
"calibTOF",
46+
BranchDefinition<CalibInfosType>{InputSpec{"input", o2::header::gDataOriginTOF, "CALIBINFOS", 0},
47+
"TOFCalibInfo",
48+
"calibinfo-branch-name",
49+
1,
50+
logger})();
9051
}
52+
9153
} // namespace tof
9254
} // namespace o2

Detectors/GlobalTrackingWorkflow/tofworkflow/src/TOFMatchedWriterSpec.cxx

Lines changed: 42 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "TOFWorkflow/TOFMatchedWriterSpec.h"
1414
#include "Framework/ControlService.h"
1515
#include "Framework/ConfigParamRegistry.h"
16+
#include "DPLUtils/MakeRootTreeWriterSpec.h"
1617
#include "Headers/DataHeader.h"
1718
#include <SimulationDataFormat/MCCompLabel.h>
1819
#include <SimulationDataFormat/MCTruthContainer.h>
@@ -27,83 +28,54 @@ namespace o2
2728
{
2829
namespace tof
2930
{
30-
using evIdx = o2::dataformats::EvIndex<int, int>;
31-
using MatchOutputType = std::vector<o2::dataformats::MatchInfoTOF>;
3231

3332
template <typename T>
34-
TBranch* getOrMakeBranch(TTree* tree, const char* brname, T* ptr)
35-
{
36-
if (auto br = tree->GetBranch(brname)) {
37-
br->SetAddress(static_cast<void*>(&ptr));
38-
return br;
39-
}
40-
// otherwise make it
41-
return tree->Branch(brname, ptr);
42-
}
33+
using BranchDefinition = MakeRootTreeWriterSpec::BranchDefinition<T>;
34+
using OutputType = std::vector<o2::dataformats::MatchInfoTOF>;
35+
using LabelsType = std::vector<o2::MCCompLabel>;
36+
using namespace o2::header;
4337

44-
void TOFMatchedWriter::init(InitContext& ic)
45-
{
46-
// get the option from the init context
47-
mOutFileName = ic.options().get<std::string>("tof-matched-outfile");
48-
mOutTreeName = ic.options().get<std::string>("treename");
49-
mFile = std::make_unique<TFile>(mOutFileName.c_str(), "RECREATE");
50-
if (!mFile->IsOpen()) {
51-
throw std::runtime_error(o2::utils::concat_string("failed to open TOF macthes output file ", mOutFileName));
52-
}
53-
mTree = std::make_unique<TTree>(mOutTreeName.c_str(), "Tree of TOF matching infos");
54-
}
55-
56-
void TOFMatchedWriter::run(ProcessingContext& pc)
38+
DataProcessorSpec getTOFMatchedWriterSpec(bool useMC)
5739
{
58-
auto indata = pc.inputs().get<MatchOutputType>("tofmatching");
59-
LOG(INFO) << "RECEIVED MATCHED SIZE " << indata.size();
60-
auto indataPtr = &indata;
61-
getOrMakeBranch(mTree.get(), "TOFMatchInfo", &indataPtr);
62-
std::vector<o2::MCCompLabel> labeltof, *labeltofPtr = &labeltof;
63-
std::vector<o2::MCCompLabel> labeltpc, *labeltpcPtr = &labeltpc;
64-
std::vector<o2::MCCompLabel> labelits, *labelitsPtr = &labelits;
65-
if (mUseMC) {
66-
labeltof = std::move(pc.inputs().get<std::vector<o2::MCCompLabel>>("matchtoflabels"));
67-
labeltpc = std::move(pc.inputs().get<std::vector<o2::MCCompLabel>>("matchtpclabels")); // RS why do we need to repead ITS/TPC labels ?
68-
labelits = std::move(pc.inputs().get<std::vector<o2::MCCompLabel>>("matchitslabels")); // They can be extracted from TPC-ITS matches
40+
// spectators for logging
41+
auto loggerMatched = [](OutputType const& indata) {
42+
LOG(INFO) << "RECEIVED MATCHED SIZE " << indata.size();
43+
};
44+
auto loggerTofLabels = [](LabelsType const& labeltof) {
6945
LOG(INFO) << "TOF LABELS GOT " << labeltof.size() << " LABELS ";
46+
};
47+
auto loggerTpcLabels = [](LabelsType const& labeltpc) {
7048
LOG(INFO) << "TPC LABELS GOT " << labeltpc.size() << " LABELS ";
49+
};
50+
auto loggerItsLabels = [](LabelsType const& labelits) {
7151
LOG(INFO) << "ITS LABELS GOT " << labelits.size() << " LABELS ";
72-
// connect this to particular branches
73-
getOrMakeBranch(mTree.get(), "MatchTOFMCTruth", &labeltofPtr);
74-
getOrMakeBranch(mTree.get(), "MatchTPCMCTruth", &labeltpcPtr);
75-
getOrMakeBranch(mTree.get(), "MatchITSMCTruth", &labelitsPtr);
76-
}
77-
mTree->Fill();
78-
}
79-
80-
void TOFMatchedWriter::endOfStream(EndOfStreamContext& ec)
81-
{
82-
LOG(INFO) << "Finalizing TOF matching info writing";
83-
mTree->Write();
84-
mTree.release()->Delete();
85-
mFile->Close();
86-
}
87-
88-
DataProcessorSpec getTOFMatchedWriterSpec(bool useMC)
89-
{
90-
std::vector<InputSpec> inputs;
91-
inputs.emplace_back("tofmatching", o2::header::gDataOriginTOF, "MATCHINFOS", 0, Lifetime::Timeframe);
92-
if (useMC) {
93-
inputs.emplace_back("matchtoflabels", o2::header::gDataOriginTOF, "MATCHTOFINFOSMC", 0, Lifetime::Timeframe);
94-
inputs.emplace_back("matchtpclabels", o2::header::gDataOriginTOF, "MATCHTPCINFOSMC", 0, Lifetime::Timeframe);
95-
inputs.emplace_back("matchitslabels", o2::header::gDataOriginTOF, "MATCHITSINFOSMC", 0, Lifetime::Timeframe);
96-
}
97-
98-
return DataProcessorSpec{
99-
"TOFMatchedWriter",
100-
inputs,
101-
{}, // no output
102-
AlgorithmSpec{adaptFromTask<TOFMatchedWriter>(useMC)},
103-
Options{
104-
{"tof-matched-outfile", VariantType::String, "o2match_tof.root", {"Name of the input file"}},
105-
{"treename", VariantType::String, "matchTOF", {"Name of top-level TTree"}},
106-
}};
52+
};
53+
// TODO: there was a comment in the original implementation:
54+
// RS why do we need to repead ITS/TPC labels ?
55+
// They can be extracted from TPC-ITS matches
56+
return MakeRootTreeWriterSpec("TOFMatchedWriter",
57+
"o2match_tof.root",
58+
"matchTOF",
59+
BranchDefinition<OutputType>{InputSpec{"tofmatching", gDataOriginTOF, "MATCHINFOS", 0},
60+
"TOFMatchInfo",
61+
"TOFMatchInfo-branch-name",
62+
1,
63+
loggerMatched},
64+
BranchDefinition<LabelsType>{InputSpec{"matchtoflabels", gDataOriginTOF, "MATCHTOFINFOSMC", 0},
65+
"MatchTOFMCTruth",
66+
"MatchTOFMCTruth-branch-name",
67+
(useMC ? 1 : 0), // one branch if mc labels enabled
68+
loggerTofLabels},
69+
BranchDefinition<LabelsType>{InputSpec{"matchtpclabels", gDataOriginTOF, "MATCHTPCINFOSMC", 0},
70+
"MatchTPCMCTruth",
71+
"MatchTPCMCTruth-branch-name",
72+
(useMC ? 1 : 0), // one branch if mc labels enabled
73+
loggerTpcLabels},
74+
BranchDefinition<LabelsType>{InputSpec{"matchitslabels", gDataOriginTOF, "MATCHITSINFOSMC", 0},
75+
"MatchITSMCTruth",
76+
"MatchITSMCTruth-branch-name",
77+
(useMC ? 1 : 0), // one branch if mc labels enabled
78+
loggerItsLabels})();
10779
}
10880
} // namespace tof
10981
} // namespace o2

Detectors/TOF/workflow/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ o2_add_library(TOFWorkflowUtils
1717
src/TOFRawWriterSpec.cxx
1818
src/CompressedDecodingTask.cxx
1919
src/CompressedInspectorTask.cxx
20-
PUBLIC_LINK_LIBRARIES O2::Framework O2::TOFBase O2::DataFormatsTOF
20+
PUBLIC_LINK_LIBRARIES O2::Framework O2::DPLUtils O2::TOFBase O2::DataFormatsTOF
2121
O2::TOFReconstruction)

Detectors/TOF/workflow/include/TOFWorkflow/TOFClusterWriterSpec.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313
#ifndef STEER_DIGITIZERWORKFLOW_TOFCLUSTERWRITER_H_
1414
#define STEER_DIGITIZERWORKFLOW_TOFCLUSTERWRITER_H_
1515

16-
#include "TFile.h"
17-
18-
#include "Framework/RootSerializationSupport.h"
1916
#include "Framework/DataProcessorSpec.h"
20-
#include "Framework/Task.h"
21-
#include <string>
2217

2318
using namespace o2::framework;
2419

@@ -27,21 +22,6 @@ namespace o2
2722
namespace tof
2823
{
2924

30-
class ClusterWriter : public Task
31-
{
32-
public:
33-
ClusterWriter(bool useMC = true) : mUseMC(useMC) {}
34-
~ClusterWriter() override = default;
35-
void init(InitContext& ic) final;
36-
void run(ProcessingContext& pc) final;
37-
38-
private:
39-
bool mFinished = false;
40-
bool mUseMC = true;
41-
std::string mOutFileName; // read from workflow
42-
std::string mOutTreeName; // read from workflow
43-
};
44-
4525
/// create a processor spec
4626
/// write ITS tracks a root file
4727
o2::framework::DataProcessorSpec getTOFClusterWriterSpec(bool useMC);

0 commit comments

Comments
 (0)