@@ -47,13 +47,40 @@ using o2::dataformats::FileMetaData;
4747using SubSpecificationType = DataAllocator::SubSpecificationType;
4848using DetID = o2::detectors::DetID;
4949
50+ enum class BranchType {
51+ Krypton,
52+ Digits,
53+ };
54+
55+ const std::unordered_map<std::string, BranchType> BranchTypeMap{
56+ {" krypton" , BranchType::Krypton},
57+ {" digits" , BranchType::Digits},
58+ };
59+
60+ const std::unordered_map<BranchType, std::string> BranchName{
61+ {BranchType::Krypton, " TPCBoxCluster" },
62+ {BranchType::Digits, " TPCDigit" },
63+ };
64+
65+ const std::unordered_map<BranchType, std::string> TreeName{
66+ {BranchType::Krypton, " Clusters" },
67+ {BranchType::Digits, " o2sim" },
68+ };
69+
5070namespace o2 ::tpc
5171{
5272
5373class FileWriterDevice : public Task
5474{
5575 public:
56- FileWriterDevice () = default ;
76+ FileWriterDevice (const std::string branchType)
77+ {
78+ try {
79+ mBranchType = BranchTypeMap.at (branchType);
80+ } catch (std::out_of_range&) {
81+ throw std::invalid_argument (std::string (" invalid writer-type type: " ) + branchType);
82+ }
83+ }
5784
5885 void init (InitContext& ic) final
5986 {
@@ -68,6 +95,22 @@ class FileWriterDevice : public Task
6895 }
6996 }
7097
98+ template <typename T>
99+ void fillBranch (int sector, InputRecord& inputs, const DataRef& inputRef)
100+ {
101+ auto inData = inputs.get <std::vector<T>>(inputRef);
102+ auto dataPtr = &inData;
103+
104+ if (!mDataBranches [sector]) {
105+ mDataBranches [sector] = mTreeOut ->Branch (fmt::format (" {}_{}" , BranchName.at (mBranchType ), sector).data (), &inData);
106+ } else {
107+ mDataBranches [sector]->SetAddress (&dataPtr);
108+ }
109+
110+ mDataBranches [sector]->Fill ();
111+ mDataBranches [sector]->ResetAddress ();
112+ }
113+
71114 void run (ProcessingContext& pc) final
72115 {
73116 const std::string NAStr = " NA" ;
@@ -79,8 +122,6 @@ class FileWriterDevice : public Task
79122 mPresentTF = dh->tfCounter ;
80123 mFirstTForbit = dh->firstTForbit ;
81124
82- LOGP (info, " run: {}, TF: {}, Orbit: {} ({}), info branches: {}" , mRun , mPresentTF , mFirstTForbit , oldOrbit, mInfoBranches .size ());
83-
84125 auto oldEnv = mEnvironmentID ;
85126 {
86127 auto envN = pc.services ().get <RawDeviceService>().device ()->fConfig ->GetProperty <std::string>(" environment_id" , NAStr);
@@ -92,6 +133,7 @@ class FileWriterDevice : public Task
92133 LOGP (WARNING , " RunNumber/Environment changed from {}/{} to {}/{}" , oldRun, oldEnv, mRun , mEnvironmentID );
93134 closeTreeAndFile ();
94135 }
136+
95137 // check for the LHCPeriod
96138 if (mLHCPeriod .empty ()) {
97139 auto LHCPeriodStr = pc.services ().get <RawDeviceService>().device ()->fConfig ->GetProperty <std::string>(" LHCPeriod" , NAStr);
@@ -112,13 +154,11 @@ class FileWriterDevice : public Task
112154
113155 for (auto br : mInfoBranches ) {
114156 br->Fill ();
115- LOGP (info, " Filling branch {}" , br->GetName ());
116157 }
117158 mTFOrbits .push_back (mFirstTForbit );
118159 ++mNTFs ;
119160 }
120161
121- bool hasData = false ;
122162 for (auto const & inputRef : InputRecordWalker (pc.inputs ())) {
123163 auto const * sectorHeader = DataRefUtils::getHeader<TPCSectorHeader*>(inputRef);
124164 if (sectorHeader == nullptr ) {
@@ -127,30 +167,12 @@ class FileWriterDevice : public Task
127167 }
128168
129169 const int sector = sectorHeader->sector ();
130- auto inData = pc.inputs ().get <std::vector<o2::tpc::KrCluster>>(inputRef);
131- auto dataPtr = &inData;
132-
133- if (!mDataBranches [sector]) {
134- mDataBranches [sector] = mTreeOut ->Branch (fmt::format (" TPCBoxCluster_{}" , sector).data (), &inData);
135- } else {
136- mDataBranches [sector]->SetAddress (&dataPtr);
170+ if (mBranchType == BranchType::Krypton) {
171+ fillBranch<KrCluster>(sector, pc.inputs (), inputRef);
172+ } else if (mBranchType == BranchType::Digits) {
173+ fillBranch<Digit>(sector, pc.inputs (), inputRef);
137174 }
138- mDataBranches [sector]->Fill ();
139- mDataBranches [sector]->ResetAddress ();
140-
141- LOGP (info, " getting data for sector {}" , sector);
142- hasData = true ;
143175 }
144-
145- // if (hasData && mTreeOut) {
146- // LOGP(info, "fill tree");
147- // mTreeOut->Fill();
148- // }
149- // for (auto br : mDataBranches) {
150- // if (br) {
151- // br->ResetAddress();
152- // }
153- // }
154176 }
155177
156178 void endOfStream (EndOfStreamContext& ec) final
@@ -185,6 +207,7 @@ class FileWriterDevice : public Task
185207 bool mStoreMetaFile = false ; // /< store the meata data file?
186208 bool mWrite = true ; // /< write data
187209 bool mCreateRunEnvDir = true ; // /< create the output directory structure?
210+ BranchType mBranchType ; // /< output branch type
188211
189212 static constexpr std::string_view TMPFileEnding{" .part" };
190213
@@ -232,17 +255,11 @@ void FileWriterDevice::prepareTreeAndFile(const o2::header::DataHeader* dh)
232255 mCurrentFileName = o2::base::NameConf::getCTFFileName (mRun , dh->firstTForbit , dh->tfCounter , " tpc_krypton" );
233256 mCurrentFileNameFull = fmt::format (" {}{}" , ctfDir, mCurrentFileName );
234257 mFileOut .reset (TFile::Open (fmt::format (" {}{}" , mCurrentFileNameFull , TMPFileEnding).c_str (), " recreate" )); // to prevent premature external usage, use temporary name
235- mTreeOut = std::make_unique<TTree>(" Clusters " , " O2 tree" );
258+ mTreeOut = std::make_unique<TTree>(TreeName. at ( mBranchType ). data () , " O2 tree" );
236259 mInfoBranches .emplace_back (mTreeOut ->Branch (" run" , &mRun ));
237260 mInfoBranches .emplace_back (mTreeOut ->Branch (" tfCounter" , &mPresentTF ));
238261 mInfoBranches .emplace_back (mTreeOut ->Branch (" firstOrbit" , &mFirstTForbit ));
239262 LOGP (info, " created {} info branches" , mInfoBranches .size ());
240- // for (int iSec = 0; iSec < Sector::MAXSECTOR; ++iSec) {
241- // std::vector<KrCluster> clusters;
242- // LOGP(info, "creating branch for sector {} on tree {} with name {}", iSec, (void*)mTreeOut.get(), fmt::format("TPCBoxCluster_{}", iSec).data());
243- // mDataBranches[iSec] = mTreeOut->Branch(fmt::format("TPCBoxCluster_{}", iSec).data(), &clusters);
244- // mDataBranches[iSec]->ResetAddress();
245- // }
246263 if (mStoreMetaFile ) {
247264 mFileMetaData = std::make_unique<o2::dataformats::FileMetaData>();
248265 }
@@ -302,13 +319,13 @@ void FileWriterDevice::closeTreeAndFile()
302319 // removeLockFile();
303320}
304321
305- DataProcessorSpec getFileWriterSpec (const std::string inputSpec)
322+ DataProcessorSpec getFileWriterSpec (const std::string inputSpec, const std::string branchType )
306323{
307324 return DataProcessorSpec{
308325 " file-writer" ,
309326 select (inputSpec.data ()),
310327 Outputs{},
311- AlgorithmSpec{adaptFromTask<FileWriterDevice>()},
328+ AlgorithmSpec{adaptFromTask<FileWriterDevice>(branchType )},
312329 Options{
313330 {" output-dir" , VariantType::String, " none" , {" output directory, must exist" }},
314331 {" meta-output-dir" , VariantType::String, " /dev/null" , {" metadata output directory, must exist (if not /dev/null)" }},
0 commit comments