Project
Loading...
Searching...
No Matches
TOFDigitWriterSplitterSpec.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef O2_TOFDIGIT_SPLITTER_WRITER_H
13#define O2_TOFDIGIT_SPLITTER_WRITER_H
14
17
22#include "Framework/Task.h"
23#include "TOFBase/Digit.h"
24#include "Framework/Logger.h"
25#include <TTree.h>
26#include <TFile.h>
27#include <gsl/span>
28
29using namespace o2::framework;
30
31namespace o2
32{
33namespace tof
34{
36{
37 using OutputType = std::vector<o2::tof::Digit>;
38 using ReadoutWinType = std::vector<o2::tof::ReadoutWindowData>;
39 using PatternType = std::vector<uint8_t>;
40 using ErrorType = std::vector<uint64_t>;
42
43 std::string mBaseName;
44
45 public:
46 TOFDigitWriterSplitter(int nTF, bool storeErr = false) : mTFthr(nTF), mStoreErrors(storeErr) {}
47
48 void createAndOpenFileAndTree(int ithread = 0)
49 {
50 TString filename = TString::Format("%s_%02d_%06d.root", mBaseName.c_str(), ithread, mCount);
51 LOG(debug) << "opening file " << filename.Data();
52 mfileOut.reset(TFile::Open(TString::Format("%s", filename.Data()), "RECREATE"));
53 mOutputTree = std::make_unique<TTree>("o2sim", "Tree with TOF digits");
54 mOutputTree->Branch("TOFHeader", &mPHeader);
55 mOutputTree->Branch("TOFDigit", &mPDigits);
56 mOutputTree->Branch("TOFReadoutWindow", &mPROW);
57 mOutputTree->Branch("TOFPatterns", &mPDia);
58 if (mStoreErrors) {
59 mOutputTree->Branch("TOFErrors", &mPErr);
60 }
61
62 mNTF = 0;
63 }
64
66 {
67 mBaseName = ic.options().get<std::string>("output-base-name");
68
69 mCount = 0;
70
71 auto instance = ic.services().get<const o2::framework::DeviceSpec>().inputTimesliceId;
72
74 }
75
77 {
78 auto instance = pc.services().get<const o2::framework::DeviceSpec>().inputTimesliceId;
79 //pc.services().get<const o2::framework::DeviceSpec>().maxInputTimeslices;
80
81 auto digits = pc.inputs().get<OutputType>("digits");
82 mPDigits = &digits;
83 auto header = pc.inputs().get<HeaderType>("header");
84 mPHeader = &header;
85 auto row = pc.inputs().get<ReadoutWinType>("rows");
86 mPROW = &row;
87 auto dia = pc.inputs().get<PatternType>("patterns");
88 mPDia = &dia;
89 if (mStoreErrors) {
90 auto error = pc.inputs().get<ErrorType>("errors");
91 mPErr = &error;
92
93 mOutputTree->Fill();
94 } else {
95 mOutputTree->Fill();
96 }
97 mNTF++;
98
99 if (mNTF >= mTFthr) {
100 sendOutput(instance);
101 }
102 }
103
105 {
106 mIsEndOfStream = true;
107 auto instance = ec.services().get<const o2::framework::DeviceSpec>().inputTimesliceId;
108
109 sendOutput(instance);
110 }
111
112 private:
113 int mCount = 0; // how many times we filled the tree
114 int mNTF = 0;
115 int mTFthr = 1;
116 bool mStoreErrors = false;
117 bool mIsEndOfStream = false;
118 OutputType mDigits;
119 const OutputType* mPDigits = &mDigits;
120 ReadoutWinType mROW;
121 const ReadoutWinType* mPROW = &mROW;
122 PatternType mDia;
123 const PatternType* mPDia = &mDia;
124 ErrorType mErr;
125 const ErrorType* mPErr = &mErr;
126 HeaderType mHeader;
127 const HeaderType* mPHeader = &mHeader;
128 std::unique_ptr<TTree> mOutputTree;
129 std::unique_ptr<TFile> mfileOut = nullptr; // file in which to write the output
130
131 //________________________________________________________________
132 void sendOutput(int instance)
133 {
134 // This is to fill the tree.
135 // One file with an empty tree will be created at the end, because we have to have a
136 // tree opened before processing, since we do not know a priori if something else
137 // will still come. The size of this extra file is ~6.5 kB
138
139 mfileOut->cd();
140 mOutputTree->Write();
141 mOutputTree.reset();
142 mfileOut.reset();
143 mCount++;
144 if (!mIsEndOfStream) {
145 createAndOpenFileAndTree(instance);
146 }
147 }
148};
149} // namespace tof
150
151namespace framework
152{
153
154DataProcessorSpec getTOFDigitWriterSplitterSpec(int nTF, bool storeErr = false)
155{
156 std::vector<InputSpec> inputs;
157 inputs.emplace_back("header", o2::header::gDataOriginTOF, "DIGITHEADER");
158 inputs.emplace_back("digits", o2::header::gDataOriginTOF, "DIGITS");
159 inputs.emplace_back("rows", o2::header::gDataOriginTOF, "READOUTWINDOW");
160 inputs.emplace_back("patterns", o2::header::gDataOriginTOF, "PATTERNS");
161
162 if (storeErr) {
163 inputs.emplace_back("errors", o2::header::gDataOriginTOF, "ERRORS");
164 }
165
166 std::vector<OutputSpec> outputs; // empty
167
168 return DataProcessorSpec{
169 "tof-digit-splitter-writer",
170 inputs,
171 outputs,
172 AlgorithmSpec{adaptFromTask<o2::tof::TOFDigitWriterSplitter>(nTF, storeErr)},
173 Options{{"output-base-name", VariantType::String, "tofdigits", {"Name of the input file (root extension will be added)"}}}};
174}
175
176} // namespace framework
177} // namespace o2
178
179#endif
std::ostringstream debug
TOFDigitWriterSplitter(int nTF, bool storeErr=false)
void run(o2::framework::ProcessingContext &pc) final
void endOfStream(o2::framework::EndOfStreamContext &ec) final
This is invoked whenever we have an EndOfStream event.
void init(o2::framework::InitContext &ic) final
constexpr o2::header::DataOrigin gDataOriginTOF
Definition DataHeader.h:575
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::vector< o2::tof::CalibInfoCluster > OutputType
std::vector< o2::tof::ReadoutWindowData > ReadoutWinType
std::vector< uint64_t > ErrorType
std::vector< uint8_t > PatternType
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::string filename()
std::vector< InputRoute > inputs
Definition DeviceSpec.h:62
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::vector< Digit > digits
std::vector< int > row