88// granted to it by virtue of its status as an Intergovernmental Organization
99// or submit itself to any jurisdiction.
1010
11- // / @brief Processor spec for a ROOT file writer for TOF digits
11+ // / @brief Processor spec for a ROOT file writer for FIT T0&V0 digits
1212
1313#include " FITDigitWriterSpec.h"
1414#include " Framework/CallbackService.h"
1515#include " Framework/ControlService.h"
16- #include < SimulationDataFormat/MCCompLabel.h>
17- #include < SimulationDataFormat/MCTruthContainer.h>
18- #include " TTree.h"
19- #include " TBranch.h"
20- #include " TFile.h"
16+ #include " Framework/Task.h"
2117#include " FITBase/Digit.h"
18+ #include " Headers/DataHeader.h"
19+ #include " DetectorsCommonDataFormats/DetID.h"
20+ #include " SimulationDataFormat/MCTruthContainer.h"
21+ #include " SimulationDataFormat/MCCompLabel.h"
22+ #include < TTree.h>
23+ #include < TBranch.h>
24+ #include < TFile.h>
2225#include < memory> // for make_shared, make_unique, unique_ptr
2326#include < vector>
27+ #include < string>
28+ #include < algorithm>
2429
2530using namespace o2 ::framework;
2631using SubSpecificationType = o2::framework::DataAllocator::SubSpecificationType;
@@ -30,88 +35,124 @@ namespace o2
3035namespace fit
3136{
3237
33- template <typename T>
34- TBranch* getOrMakeBranch (TTree& tree, std::string brname, T* ptr)
38+ class FITDPLDigitWriter
3539{
36- if (auto br = tree.GetBranch (brname.c_str ())) {
37- br->SetAddress (static_cast <void *>(&ptr));
38- return br;
40+
41+ using MCCont = o2::dataformats::MCTruthContainer<o2::MCCompLabel>;
42+
43+ public:
44+ void init (framework::InitContext& ic)
45+ {
46+ std::string detStrL = mID .getName ();
47+ std::transform (detStrL.begin (), detStrL.end (), detStrL.begin (), ::tolower);
48+
49+ auto filename = ic.options ().get <std::string>((detStrL + " -digit-outfile" ).c_str ());
50+ auto treename = ic.options ().get <std::string>(" treename" );
51+
52+ mOutFile = std::make_unique<TFile>(filename.c_str (), " RECREATE" );
53+ if (!mOutFile || mOutFile ->IsZombie ()) {
54+ LOG (ERROR) << " Failed to open " << filename << " output file" ;
55+ } else {
56+ LOG (INFO) << " Opened " << filename << " output file" ;
57+ }
58+ mOutTree = std::make_unique<TTree>(treename.c_str (), treename.c_str ());
3959 }
40- // otherwise make it
41- return tree.Branch (brname.c_str (), ptr);
42- }
4360
61+ void run (framework::ProcessingContext& pc)
62+ {
63+ if (mFinished ) {
64+ return ;
65+ }
66+ std::string detStr = mID .getName ();
67+ std::string detStrL = mID .getName ();
68+ std::transform (detStrL.begin (), detStrL.end (), detStrL.begin (), ::tolower);
69+
70+ // retrieve the digits from the input
71+ auto inDigits = pc.inputs ().get <std::vector<o2::fit::Digit>>((detStr + " digits" ).c_str ());
72+ // auto inROFs = pc.inputs().get<std::vector<o2::ITSMFT::ROFRecord>>((detStr + "digitsROF").c_str());
73+ // auto inMC2ROFs = pc.inputs().get<std::vector<o2::ITSMFT::MC2ROFRecord>>((detStr + "digitsMC2ROF").c_str());
74+ // auto inLabels = pc.inputs().get<MCCont*>((detStr + "digitsMCTR").c_str());
75+ LOG (INFO) << " RECEIVED DIGITS SIZE " << inDigits.size ();
76+
77+ auto digitsP = &inDigits;
78+ // auto labelsRaw = inLabels.get();
79+ // connect this to a particular branch
80+
81+ auto brDig = getOrMakeBranch (*mOutTree .get (), (detStr + " Digit" ).c_str (), &digitsP);
82+ // auto brLbl = getOrMakeBranch(*mOutTree.get(), (detStr + "DigitMCTruth").c_str(), &labelsRaw);
83+ mOutTree ->Fill ();
84+
85+ mOutFile ->cd ();
86+ mOutTree ->Write ();
87+ mOutTree .reset (); // delete the tree before closing the file
88+ mOutFile ->Close ();
89+ mFinished = true ;
90+ pc.services ().get <ControlService>().readyToQuit (false );
91+ }
92+
93+ protected:
94+ FITDPLDigitWriter () {}
95+ template <typename T>
96+ TBranch* getOrMakeBranch (TTree& tree, std::string brname, T* ptr)
97+ {
98+ if (auto br = tree.GetBranch (brname.c_str ())) {
99+ br->SetAddress (static_cast <void *>(ptr));
100+ return br;
101+ }
102+ // otherwise make it
103+ return tree.Branch (brname.c_str (), ptr);
104+ }
105+
106+ bool mFinished = false ;
107+ o2::detectors::DetID mID ;
108+ o2::header::DataOrigin mOrigin = o2::header::gDataOriginInvalid ;
109+ std::vector<o2::fit::Digit> mDigits ; // input digits
110+ std::unique_ptr<TFile> mOutFile ;
111+ std::unique_ptr<TTree> mOutTree ;
112+ };
113+
114+ // _______________________________________________
115+ class T0DPLDigitWriter : public FITDPLDigitWriter
116+ {
117+ public:
118+ // FIXME: origina should be extractable from the DetID, the problem is 3d party header dependencies
119+ static constexpr o2::detectors::DetID::ID DETID = o2::detectors::DetID::T0;
120+ static constexpr o2::header::DataOrigin DETOR = o2::header::gDataOriginT0 ;
121+ T0DPLDigitWriter ()
122+ {
123+ mID = DETID;
124+ mOrigin = DETOR;
125+ }
126+ };
127+
128+ constexpr o2::detectors::DetID::ID T0DPLDigitWriter::DETID;
129+ constexpr o2::header::DataOrigin T0DPLDigitWriter::DETOR;
130+
131+ // _______________________________________________
44132// / create the processor spec
45- // / TODO: replace by generic processor once this is working
46- DataProcessorSpec getFITDigitWriterSpec ()
133+ // / describing a processor receiving digits for ITS/MFT and writing them to file
134+ DataProcessorSpec getT0DigitWriterSpec ()
47135{
48- auto initFunction = [](InitContext& ic) {
49- // get the option from the init context
50- auto filename = ic. options (). get <std::string>( " fit-digit-outfile " );
51- auto treename = ic. options (). get <std::string>( " treename " ) ;
136+ std::string detStr = o2::detectors::DetID::getName (T0DPLDigitWriter::DETID);
137+ std::string detStrL = detStr;
138+ std::transform (detStrL. begin (), detStrL. end (), detStrL. begin (), ::tolower );
139+ auto detOrig = T0DPLDigitWriter::DETOR ;
52140
53- auto outputfile = std::make_shared<TFile>(filename.c_str (), " RECREATE" );
54- auto outputtree = std::make_shared<TTree>(treename.c_str (), treename.c_str ());
55-
56- // container for incoming digits
57- auto digits = std::make_shared<std::vector<o2::fit::Digit>>();
58-
59- // the callback to be set as hook at stop of processing for the framework
60- auto finishWriting = [outputfile, outputtree]() {
61- outputtree->SetEntries (1 );
62- outputtree->Write ();
63- outputfile->Close ();
64- };
65- ic.services ().get <CallbackService>().set (CallbackService::Id::Stop, finishWriting);
66-
67- // setup the processing function
68- // using by-copy capture of the worker instance shared pointer
69- // the shared pointer makes sure to clean up the instance when the processing
70- // function gets out of scope
71- auto processingFct = [outputfile, outputtree, digits](ProcessingContext& pc) {
72- static bool finished = false ;
73- if (finished) {
74- // avoid being executed again when marked as finished;
75- return ;
76- }
77-
78- // retrieve the digits from the input
79- auto indata = pc.inputs ().get <std::vector<o2::fit::Digit>>(" fitdigits" );
80- LOG (INFO) << " RECEIVED DIGITS SIZE " << indata.size ();
81- *digits.get () = std::move (indata);
82-
83- // connect this to a particular branch
84- auto br = getOrMakeBranch (*outputtree.get (), " FITDigit" , digits.get ());
85- br->Fill ();
86-
87- // retrieve labels from the input
88- // auto labeldata = pc.inputs().get<o2::dataformats::MCTruthContainer<o2::MCCompLabel>*>("tofdigitlabels");
89- // LOG(INFO) << "TOF GOT " << labeldata->getNElements() << " LABELS ";
90- // auto labeldataraw = labeldata.get();
91- // connect this to a particular branch
92- // auto labelbr = getOrMakeBranch(*outputtree.get(), "TOFDigitMCTruth", &labeldataraw);
93- // labelbr->Fill();
94-
95- finished = true ;
96- pc.services ().get <ControlService>().readyToQuit (false );
97- };
98-
99- // return the actual processing function as a lambda function using variables
100- // of the init function
101- return processingFct;
102- };
141+ std::vector<InputSpec> inputs;
142+ inputs.emplace_back (InputSpec{ (detStr + " digits" ).c_str (), detOrig, " DIGITS" , 0 , Lifetime::Timeframe });
103143
104144 return DataProcessorSpec{
105- " FITDigitWriter" ,
106- Inputs{ InputSpec{ " fitdigits" , " FIT" , " DIGITS" , 0 , Lifetime::Timeframe },
107- /* InputSpec{ "tofdigitlabels", "TOF", "DIGITSMCTR", 0, Lifetime::Timeframe } */ },
145+ (detStr + " DigitWriter" ).c_str (),
146+ inputs,
108147 {}, // no output
109- AlgorithmSpec (initFunction ),
148+ AlgorithmSpec (adaptFromTask<T0DPLDigitWriter>() ),
110149 Options{
111- { " fit -digit-outfile" , VariantType::String, " fitdigits .root" , { " Name of the input file" } },
150+ { (detStrL + " -digit-outfile" ). c_str () , VariantType::String, (detStrL + " digits .root" ). c_str () , { " Name of the input file" } },
112151 { " treename" , VariantType::String, " o2sim" , { " Name of top-level TTree" } },
113152 }
114153 };
115154}
155+
156+
116157} // end namespace fit
117158} // end namespace o2
0 commit comments