ATLAS Offline Software
Loading...
Searching...
No Matches
FileReaderWriter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "FileReaderWriter.h"
6#include "EventStorage/RawFileName.h"
7
8DFEF::FileReaderWriter::FileReaderWriter(const boost::property_tree::ptree &args) {
9 ERS_DEBUG(1, "Configuring FileReaderWriter");
10 m_stride=args.get("stride",1);
11 m_currFile=args.get("fileOffset",-1);
12 m_nMaxEvents=args.get("numEvents",-1);
13 m_nSkip=args.get("skipEvents",0);
14 m_loopFiles=(args.get("loopOverFiles","false")!="False");
15 m_nEvents=0;
17 try{
18 auto of=args.get_child("outputFileName");
19 m_outFileName=std::string(of.data());
20 ERS_LOG("Output file name: \"" << m_outFileName << "\"");
21 }catch(boost::property_tree::ptree_bad_path &ex){
22 ERS_LOG("Failed to get outputFileName");
23 }
24 auto r(args.get_child("fileList").equal_range("file"));
25 for (auto it = r.first; it != r.second; ++it) {
26 m_fileNames.push_back(it->second.get_value<std::string>());
27 ERS_LOG(" Adding file "+it->second.get_value<std::string>());
28 }
29 if(m_fileNames.size()==0){
30 ERS_LOG("No input files specified");
31 throw BadConfiguration();
32 }
33
34 // Get correct run number and detector_mask from prepareForRun ptree
35 const auto & run_params = args.get_child("RunParams");
36 std::string detmask = run_params.get("det_mask", "");
37 uint64_t detmask_LS(0), detmask_MS(0);
38 if (detmask.size() == 32) { // Detmask is supposed to be a 32 digit hex number. Otherwise ignore
39 detmask_LS = std::stoull(detmask.substr(16, 32), 0, 16);
40 detmask_MS = std::stoull(detmask.substr(0, 16), 0, 16);
41 }
42 if(!m_outFileName.empty()){
43 std::unique_ptr<EventStorage::run_parameters_record> runParams;
44 runParams=std::make_unique<EventStorage::run_parameters_record>();
45 runParams->run_number = std::stoul(run_params.get("run_number", "0"));
46 runParams->max_events = 0;
47 runParams->rec_enable = 0;
48 runParams->trigger_type = std::stoul(run_params.get("trigger_type", "0"));
49 runParams->detector_mask_LS = detmask_LS;
50 runParams->detector_mask_MS = detmask_MS;
51 runParams->beam_type = std::stoul(run_params.get("beam_type", "0"));
52 runParams->beam_energy = std::stoul(run_params.get("beam_energy", "0"));
53 ERS_LOG("Opening outputfile with base name: " << m_outFileName);
54
55 std::string path = "."; // use work Directory
56 std::string project = run_params.get("T0_project_tag", "");
57 std::string streamType = "unknown";
58 std::string streamName = "SingleStream";
59 std::string stream = run_params.get("stream", "");
60 unsigned int lumiBlockNumber = run_params.get("lumiblock", 0);;
61 std::string applicationName = std::string("DFEF_") + m_outFileName;
62
63 std::vector<std::string> fmdStrings = {};
64
65 ERS_LOG("Going to open raw file");
66 auto rawfilename = boost::shared_ptr<daq::RawFileName>
67 (new daq::RawFileName(project, runParams->run_number,streamType, streamName, lumiBlockNumber, applicationName));
68
69 //FIXME just for debug
70 ERS_LOG("Raw file name: ");
71 rawfilename->print();
72
73 m_writer=std::make_unique<EventStorage::DataWriter>(path,
74 rawfilename,
75 *runParams,
76 project,
77 streamType,
78 streamName,
79 stream,
80 lumiBlockNumber,
81 applicationName,
82 fmdStrings);
83 m_writer->setMaxFileMB(2000);
84 }
85 nextFile(); // Open next file
86
87 uint eventsInFile=m_currReader->eventsInFile();
88 ERS_LOG("Events in file: " << eventsInFile);
89 // Skip events if necessary
90 if (m_nSkip >= eventsInFile ) {
91 ERS_LOG("Events to skip: " + std::to_string(m_nSkip) + ", total events in file: " + std::to_string(eventsInFile));
92 throw BadConfiguration();
93 }
94 if (m_nSkip) {
95 ERS_LOG("Skipping " << m_nSkip << " events");
97 }
98
99 print();
100}
101
103 std::cout << "FileReaderWriter: " << std::endl;
104 std::cout << " Loop? : " << m_loopFiles << std::endl;
105 std::cout << " stride : " << m_stride << std::endl;
106 std::cout << " currEventInFile : " << m_currEventInFile << std::endl;
107 std::cout << " nSkip : " << m_nSkip << std::endl;
108 std::cout << " nMaxEvents : " << m_nMaxEvents << std::endl;
109 for (auto & file : m_fileNames) {
110 std::cout << " file: " << file << std::endl;
111 }
112}
113
115 ERS_DEBUG(2,"Going to skip " << num << " events in file: " << m_currFile);
116 for (uint i = 0; i < num; i++) {
117 auto blob = getEventFromFile();
118 }
119 m_currEventInFile += num;
120}
121
122std::unique_ptr<uint32_t[]> DFEF::FileReaderWriter::getNextEvent(){
123 ERS_DEBUG(2, "FileReaderWriter::Enter, event: " << m_nEvents);
124 if((m_nMaxEvents>0) && (m_nEvents>=m_nMaxEvents)){
125 std::string message = "Event count reached, max events to read: " + std::to_string(m_nMaxEvents);
126 ERS_DEBUG(2, message);
127 throw NoMoreEventsInFile();
128 }
129
130 uint eventsInFile=m_currReader->eventsInFile();
131 std::unique_ptr<uint32_t[]> blob;
132 if((uint)(m_currEventInFile+m_stride)<=eventsInFile){
133 unsigned int target=m_currEventInFile+m_stride-1;
134 ERS_DEBUG(2, "Current event in file: " << m_currEventInFile << ", stride: " << m_stride << ", target: " << target);
136 blob=getEventFromFile();
138 }else{
139 int newoffset=m_currEventInFile+m_stride-eventsInFile;
140 if(nextFile()){
141 eventsInFile=m_currReader->eventsInFile();
142 if(eventsInFile>=(uint)newoffset){
143 if(newoffset>1){
144 ERS_DEBUG(2, "Skipping for offset " << newoffset-1 << " events in file: " << m_currFile);
145 skipEvents(newoffset-1);
146 }
147 blob=getEventFromFile();
148 m_currEventInFile=newoffset;
149 }else{
150 throw NoMoreEventsInFile();
151 }
152 }else{
153 throw NoMoreEventsInFile();
154 }
155 }
156 m_nEvents++;
157 return blob;
158}
159
160std::unique_ptr<uint32_t[]> DFEF::FileReaderWriter::getEventFromFile(){
161 ERS_DEBUG(2, "FileReaderWriter::getEventFromFile, event: " << m_nEvents << ", current file: " << m_currFile);
162 char *buff = nullptr;
163 unsigned int size=0;
164 try{
165 int error_code=m_currReader->getData(size,&buff);
166 while (error_code == DRWAIT) {
167 usleep(500000);
168 ERS_DEBUG(2, " Waiting for more data.");
169 error_code = m_currReader->getData(size, &buff);
170 }
171 if (error_code == DRNOOK) {
172 ERS_DEBUG(2, " Reading of data NOT OK!");
173 delete[] buff;
175 }
176 if (error_code == DROK) {
177 ERS_DEBUG(2, " Event OK");
178 }
179 } catch(std::exception &ex){
180 ERS_LOG("Failed reading event. Caught exception \""<<ex.what()<<"\"");
181 std::cerr<<"Reading event failed. Caught exception \""<<ex.what()<<"\""<<std::endl;
182 } catch (...) {
183 ERS_LOG("Unexpected exception!");
184 }
185 uint32_t *blob=reinterpret_cast<uint32_t*>(buff);
186 return std::unique_ptr<uint32_t[]>(blob);
187}
188
190 if(m_fileNames.size()==0){
191 throw std::runtime_error("No input files defined!");
192 }
193 ERS_DEBUG(2, "m_loopFiles: " << m_loopFiles);
194 if(m_currFile>=(int)m_fileNames.size()){
195 if(m_loopFiles){
196 m_currFile=-1;
197 }else{
198 throw NoMoreEventsInFile();
199 }
200 }
201 ERS_DEBUG(2, "Current file: " << m_currFile);
202 if(m_currFile<(int)m_fileNames.size()){
203 if(m_currReader){
204 m_currReader.reset();
205 }
206 m_currFile++;
207 if(m_currFile==(int)m_fileNames.size()){
208 if(!m_loopFiles){
209 throw NoMoreEventsInFile();
210 }else{
211 m_currFile=0;
212 }
213 }
214 ERS_LOG("Opening file: " << m_fileNames.at(m_currFile));
215 try{
216 std::unique_ptr<EventStorage::DataReader> temp (pickDataReader(m_fileNames.at(m_currFile)));
217 m_currReader = std::move(temp);
218 }catch(ers::Issue &e){
219 ERS_LOG("Caught issue "<<e.what());
220 }
221 if (!m_currReader) {
222 ERS_LOG("Failed to open file \""<<m_fileNames.at(m_currFile)<<"\" (DataReader not created)");
223 throw std::runtime_error("Can't open file");
224 }
225 if (!m_currReader->good()) {
226 ERS_LOG("Failed to open file \""<<m_fileNames.at(m_currFile)<<"\" (DataReader->good()==false)");
227 m_currReader.reset();
228 throw std::runtime_error("Can't open file");
229 }
230 // Reset event index for the newly opened file
232 ERS_LOG("Opened file \""<<m_fileNames.at(m_currFile)<<"\"");
233 return true;
234 }
235 ERS_DEBUG(1,"m_currFile="<<m_currFile<<", m_fileNames.size()="<<m_fileNames.size());
236 throw NoMoreEventsInFile();
237}
238
239void DFEF::FileReaderWriter::writeEvent(const unsigned int & size_in_bytes, const uint32_t *event){
240 if (!m_outFileName.empty()) {
241 ERS_DEBUG(2, "Writing event to disk, " << size_in_bytes << " bytes, " << *event);
242 auto wres=m_writer->putData(size_in_bytes, event);
243 if (wres) {
244 ERS_DEBUG(2, "File writing failed, code: " << wres);
245 }
246 }
247}
unsigned int uint
T_ResultType project(ParameterMapping::type< N > parameter_map, const T_Matrix &matrix)
FileReaderWriter(const boost::property_tree::ptree &tree)
std::unique_ptr< EventStorage::DataWriter > m_writer
std::vector< std::string > m_fileNames
void print()
Print configuration of the FileReaderWriter.
void writeEvent(const unsigned int &size_in_bytes, const uint32_t *event)
Write event to file.
std::unique_ptr< uint32_t[]> getNextEvent()
Get serialized next event and put the FullEventFragment in the memory location.
std::unique_ptr< uint32_t[]> getEventFromFile()
Read the current Event from File.
std::unique_ptr< EventStorage::DataReader > m_currReader
int r
Definition globals.cxx:22
TFile * file