Project
Loading...
Searching...
No Matches
DigitsToRawSpec.cxx
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
18
19#include <random>
20#include <iostream>
21#include <fstream>
22#include <stdexcept>
23#include <array>
24#include <functional>
25#include <vector>
26#include <algorithm>
27#include <filesystem>
33#include "Framework/Lifetime.h"
34#include "Framework/Output.h"
35#include "Framework/Task.h"
37
38#include <fairlogger/Logger.h> // for LOG
39#include "Framework/Logger.h"
43
44#include "TFile.h"
45#include "TTree.h"
46#include <TSystem.h>
50
53#include "HMPIDBase/Geo.h"
56
57namespace o2
58{
59namespace hmpid
60{
61
62using namespace o2;
63using namespace o2::framework;
65
66//=======================
67// Data decoder
69{
70 LOG(info) << "HMPID Write Raw File From Root sim Digits vector - init()";
71 mDigitsReceived = 0;
72 mEventsReceived = 0;
73 mBaseRootFileName = ic.options().get<std::string>("in-file");
74 mBaseFileName = ic.options().get<std::string>("outfile");
75 mDirectoryName = ic.options().get<std::string>("outdir");
76 mFileFor = ic.options().get<std::string>("file-for");
77 mDumpDigits = ic.options().get<bool>("dump-digits"); // Debug flags
78 mSkipEmpty = ic.options().get<bool>("skip-empty");
79
80 // if needed, create output directory
81 if (!std::filesystem::exists(mDirectoryName)) {
82 if (!std::filesystem::create_directories(mDirectoryName)) {
83 LOG(fatal) << "could not create output directory " << mDirectoryName;
84 } else {
85 LOG(info) << "created output directory " << mDirectoryName;
86 }
87 }
88
89 std::string fullFName = o2::utils::Str::concat_string(mDirectoryName, "/", mBaseFileName);
90
91 // Setup the Coder
93 mCod->setSkipEmptyEvents(mSkipEmpty);
94 mCod->openOutputStream(fullFName, mFileFor);
95 std::string inputGRP = o2::base::NameConf::getGRPFileName();
96 std::unique_ptr<o2::parameters::GRPObject> grp{o2::parameters::GRPObject::loadFrom(inputGRP)};
97 mCod->getWriter().setContinuousReadout(grp->isDetContinuousReadOut(o2::detectors::DetID::HMP)); // must be set explicitly
98
99 // Open the ROOT file
100 TFile* fdig = TFile::Open(mBaseRootFileName.data());
101 assert(fdig != nullptr);
102 LOG(info) << "Open Root digits file " << mBaseRootFileName.data();
103 mDigTree = (TTree*)fdig->Get("o2sim");
104
105 // Ready to operate
106 mCod->getWriter().writeConfFile("HMP", "RAWDATA", o2::utils::Str::concat_string(mDirectoryName, '/', "HMPraw.cfg"));
107 mExTimer.start();
108}
109
110void DigitsToRawSpec::readRootFile()
111{
112 std::vector<o2::hmpid::Digit> digitsPerEvent;
113 std::vector<o2::hmpid::Digit> digits, *hmpBCDataPtr = &digits;
114 std::vector<o2::hmpid::Trigger> interactions, *interactionsPtr = &interactions;
115
116 // Keeps the Interactions !
117 mDigTree->SetBranchAddress("InteractionRecords", &interactionsPtr);
118 LOG(info) << "Number of Interaction Records vectors in the simulation file :" << mDigTree->GetEntries();
119 for (int ient = 0; ient < mDigTree->GetEntries(); ient++) {
120 mDigTree->GetEntry(ient);
121 LOG(info) << "Interactions records in simulation :" << interactions.size();
122 for (auto a : interactions) {
123 LOG(info) << a;
124 }
125 }
126
127 mDigTree->SetBranchAddress("HMPDigit", &hmpBCDataPtr);
128 LOG(debug) << "Number of entries in the simulation file :" << mDigTree->GetEntries();
129
130 // Loops in the Entry of ROOT Branch
131 for (int ient = 0; ient < mDigTree->GetEntries(); ient++) {
132 mDigTree->GetEntry(ient);
133 int nbc = digits.size();
134 if (nbc == 0) { // exit for empty
135 LOG(info) << "The Entry :" << ient << " doesn't have digits !";
136 continue;
137 }
138 if (mDumpDigits) { // we want the dump of digits ?
139 std::ofstream dumpfile;
140 dumpfile.open("/tmp/hmpDumpDigits.dat");
141 for (o2::hmpid::Trigger& e : interactions) {
142 dumpfile << "Trigger Orbit=" << e.getOrbit() << " BC=" << e.getBc() << std::endl;
143 for (int i = e.getFirstEntry(); i <= e.getLastEntry(); i++) {
144 dumpfile << digits.at(i) << std::endl;
145 }
146 }
147 dumpfile.close();
148 }
149 // ready to operate
150 LOG(info) << "For the entry = " << ient << " there are " << nbc << " DIGITS stored.";
151 for (o2::hmpid::Trigger& e : interactions) {
152 mEventsReceived++;
153 digitsPerEvent.clear();
154 for (int i = e.getFirstEntry(); i <= e.getLastEntry(); i++) {
155 digitsPerEvent.push_back(digits[i]);
156 }
157 LOG(debug) << "Orbit =" << e.getOrbit() << " BC =" << e.getBc() << " Digits =" << digitsPerEvent.size();
158 if (digitsPerEvent.size() == 0) {
159 LOG(info) << "Empty event !" << e;
160 }
161 mCod->codeEventChunkDigits(digitsPerEvent, e.getIr());
162 mDigitsReceived += digitsPerEvent.size();
163 }
164 if (mDigitsReceived != digits.size()) {
165 LOG(warning) << "Digits outside the events defined !";
166 }
167 }
168 mExTimer.logMes("End of Write raw file Job !");
169 return;
170}
171
173{
174 // Arrange Files path
175 readRootFile();
176 mCod->closeOutputStream();
177 mCod->dumpResults(mBaseRootFileName);
178 mExTimer.logMes("Raw File created ! Digits = " + std::to_string(mDigitsReceived) + " for Events =" + std::to_string(mEventsReceived));
179 mExTimer.stop();
181}
182
187
188//_________________________________________________________________________________________________
190{
191 std::vector<o2::framework::InputSpec> inputs;
192 std::vector<o2::framework::OutputSpec> outputs;
193
194 return DataProcessorSpec{
195 "digits-to-raw",
196 inputs,
197 outputs,
198 AlgorithmSpec{adaptFromTask<DigitsToRawSpec>()},
199 Options{{"outdir", VariantType::String, "./", {"base dir for output file"}},
200 {"file-for", VariantType::String, "all", {"single file per: all,flp,link,crorcendpoint"}},
201 {"outfile", VariantType::String, "HMP", {"base name for output file"}},
202 {"in-file", VariantType::String, "hmpiddigits.root", {"name of the input sim root file"}},
203 {"dump-digits", VariantType::Bool, false, {"out the digits file in /tmp/hmpDumpDigits.dat"}},
204 {"skip-empty", VariantType::Bool, false, {"skip empty events"}}}};
205}
206
207} // namespace hmpid
208} // end namespace o2
A raw page parser for DPL input.
std::ostringstream debug
int32_t i
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char e
Header of the General Run Parameters object.
A helper class to iteratate over all parts of all input routes.
Definition of the Names Generator class.
Definition of the RAW Data Header.
static std::string getGRPFileName(const std::string_view prefix=STANDARDSIMPREFIX)
Definition NameConf.cxx:59
ConfigParamRegistry const & options()
Definition InitContext.h:33
ServiceRegistryRef services()
The services registry associated with this processing context.
void endOfStream(framework::EndOfStreamContext &ec) override
This is invoked whenever we have an EndOfStream event.
void run(framework::ProcessingContext &pc) final
void init(framework::InitContext &ic) final
void stop()
stop : stops the timer
Definition Common.h:73
void start()
start : starts the timer
Definition Common.h:64
void logMes(std::string const message)
Definition Common.h:81
static constexpr int MAXEQUIPMENTS
Definition Geo.h:79
void codeEventChunkDigits(std::vector< o2::hmpid::Digit > &digits, InteractionRecord ir)
o2::raw::RawFileWriter & getWriter()
void dumpResults(const std::string &outputFileName)
Dumps the results of the last coding.
void setSkipEmptyEvents(bool Skip)
void openOutputStream(const std::string &outputFileName, const std::string &fileFor)
void closeOutputStream()
Close and flush the output streams.
HMPID Trigger declaration.
Definition Trigger.h:32
static GRPObject * loadFrom(const std::string &grpFileName="")
void writeConfFile(std::string_view origin="FLP", std::string_view description="RAWDATA", std::string_view cfgname="raw.cfg", bool fullPath=true) const
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
@ Me
Only quit this data processor.
o2::framework::DataProcessorSpec getDigitsToRawSpec()
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
static std::string concat_string(Ts const &... ts)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::vector< Digit > digits