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{
2829namespace tof
2930{
30- using evIdx = o2::dataformats::EvIndex<int , int >;
31- using MatchOutputType = std::vector<o2::dataformats::MatchInfoTOF>;
3231
3332template <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
0 commit comments