FDasEventFile.cc
Go to the documentation of this file.
1 
9 #include <AugerEvent.h>
10 #include <io/FDasEventFile.h>
11 #include <io/VROOTFile.h>
12 #include <io/FDasToOfflineEventConverter.h>
13 #include <EyeEvent.hh>
14 #include <EyeEventFile.hh>
15 #include <evt/Event.h>
16 #include <fevt/FEvent.h>
17 #include <fevt/Eye.h>
18 #include <fevt/EyeHeader.h>
19 
20 #include <utl/ErrorLogger.h>
21 
22 #include <cstddef>
23 #include <sstream>
24 #include <map>
25 
26 using namespace std;
27 using namespace io;
28 using namespace fevt;
29 
30 
31 FDasEventFile::~FDasEventFile()
32 {
33  for (const auto& f : fEyeEventFile)
34  delete f.second;
35  delete fCurEyeEvent;
36 }
37 
38 
39 FDasEventFile::FDasEventFile(const std::string& filename, const Mode mode, utl::Branch* const b) :
40  VEventFile(),
41  fCurEyeEvent(new TEyeEvent)
42 {
43  Open(filename, mode, b);
44 }
45 
46 
47 void
48 FDasEventFile::Open(const std::string& filename, const Mode mode, utl::Branch* const /*b*/)
49 {
51  fMode = mode;
52 
53  if (fMode == io::eRead) {
54 
55  TEyeEventFile* const tmpFile =
56  new TEyeEventFile(filename.c_str(),
57  VROOTFile::GetRootMode(fMode).c_str());
58 
59  if (tmpFile->IsZombie()) {
60  std::ostringstream mess;
61  mess << "Unable to open file " << filename;
62  throw utl::IOFailureException(mess.str());
63  }
64 
65  if (!tmpFile->GetNEvents()) {
66  std::ostringstream mess;
67  mess << "File is empty " << filename;
68  throw utl::IOFailureException(mess.str());
69  }
70 
71  TEyeEvent* tmpEvent = new TEyeEvent();
72  tmpFile->ReadEvent(&tmpEvent);
73  const unsigned int eyeId = tmpEvent->GetEventHeader()->GetEyeNo();
74  delete tmpEvent;
75 
76  tmpFile->Rewind();
77 
78  fEyeEventFile[eyeId] = tmpFile;
79 
80  }
81 }
82 
83 
84 void
86 {
87  for (const auto& f : fEyeEventFile) {
88  f.second->Close();
89  delete f.second;
90  }
91  fEyeEventFile.clear();
92 }
93 
94 
95 Status
97 {
98  int countEvents = 0;
99 
100  for (const auto& f : fEyeEventFile) {
101 
102  if (!event.HasFEvent())
103  event.MakeFEvent();
104 
105  fCurEyeEvent->Clear();
106 
107  if (f.second->ReadEvent(&fCurEyeEvent)) {
108 
109  /*
110  copy the TEyeEvent into the AugerEvent embedded into the Offline
111  evt::Event, that should be the only source of raw data
112 
113  Take care, because AugerEvent::PushEvent makes no check
114  for duplicate-eyes, do this here, to prevent big lists of EyeEvents.
115  */
116 
117  const unsigned int eyeId = fCurEyeEvent->GetEventHeader()->GetEyeNo();
118 
119  if (!event.HasRawEvent())
120  event.MakeRawEvent();
121 
122  AugerEvent& raw = event.GetRawEvent();
123 
124  AugerEvent::EyeIterator eyeIter;
125  for (eyeIter = raw.EyesBegin(); eyeIter != raw.EyesEnd(); ++eyeIter)
126  if (eyeIter->GetEventHeader()->GetEyeNo() == eyeId)
127  break;
128 
129  if (eyeIter == raw.EyesEnd())
130  raw.PushEvent(*fCurEyeEvent);
131  else
132  *eyeIter = *fCurEyeEvent;
133 
134  event << *fCurEyeEvent;
135 
136  ++countEvents;
137  }
138 
139  //return eSuccess;
140 
141  }
142 
143  return (countEvents > 0) ? eSuccess : eEOF;
144 }
145 
146 
148 void
150 {
151  if (fMode == eRead) {
152  ERROR("File is ReadOnly");
153  throw utl::IOFailureException("File is Read Only");
154  }
155 
156  fCurEyeEvent->Clear();
157 
158  evt::Event& ncEvent = const_cast<evt::Event&>(event);
159 
160  if (!ncEvent.HasRawEvent() ||
161  (ncEvent.GetRawEvent().EyesBegin() == ncEvent.GetRawEvent().EyesEnd())) {
162  INFO("No raw FD event to write");
163  return;
164  }
165 
166  AugerEvent& raw = ncEvent.GetRawEvent();
167 
168  for (AugerEvent::EyeIterator eyeIter = raw.EyesBegin();
169  eyeIter != raw.EyesEnd(); ++eyeIter) {
170 
171  unsigned int eyeId = eyeIter->GetEventHeader()->GetEyeNo();
172 
173  if (!fEyeEventFile.count(eyeId)) {
174 
175  ostringstream eyeFileName;
176  eyeFileName << fFilename.substr(0, fFilename.rfind('.'))
177  << ".eye" << eyeId << ".root";
178 
179  fEyeEventFile[eyeId] =
180  new TEyeEventFile(eyeFileName.str().c_str(),
181  VROOTFile::GetRootMode(fMode).c_str());
182 
183  }
184 
185  *fCurEyeEvent = *eyeIter;
186  fEyeEventFile[eyeId]->WriteEvent(&fCurEyeEvent);
187 
188  }
189 }
190 
191 
192 int
194 {
195  int nEvents = 0;
196  bool first = true;
197  for (const auto& f : fEyeEventFile) {
198  const int n = f.second->GetNEvents();
199  if (first) {
200  nEvents = n;
201  first = false;
202  } else if (n < nEvents)
203  nEvents = n;
204  }
205  return nEvents;
206 }
207 
208 
209 Status
210 FDasEventFile::GotoPosition(const unsigned int position)
211 {
212  for (const auto& f : fEyeEventFile)
213  f.second->Seek(position);
214  return eSuccess;
215 }
216 
217 
218 Status
219 FDasEventFile::FindEvent(const unsigned int eventid)
220 {
221  for (const auto& f : fEyeEventFile) {
222  auto& file = *f.second;
223  file.Rewind();
224  unsigned int posInTree = 0;
225  TEyeEventHeader* header = nullptr;
226  while (file.ReadEventHeader(&header)) {
227  if (eventid == header->GetEventNo()) {
228  file.Seek(posInTree);
229  return io::eSuccess;
230  }
231  ++posInTree;
232  }
233  }
234  return eFail;
235 }
static std::string GetRootMode(const Mode mode)
Definition: VROOTFile.cc:123
std::string fFilename
Definition: VEventFile.h:74
Status Read(evt::Event &event) override
clear /par event read current event advance cursor by 1
void Open(const std::string &filename, const Mode mode, utl::Branch *const b) override
int raw
Definition: dump1090.h:270
std::map< unsigned int, TEyeEventFile * > fEyeEventFile
Definition: FDasEventFile.h:49
bool HasFEvent() const
Mode
Available open modes.
Definition: IoCodes.h:16
void Close() override
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
AugerEvent & GetRawEvent()
Base class to report exceptions in IO.
Class representing a document branch.
Definition: Branch.h:107
int GetNEvents() override
Status
Return code for seek operation.
Definition: IoCodes.h:24
void Write(const evt::Event &event) override
const string file
bool HasRawEvent() const
Status FindEvent(const unsigned int eventid) override
seek Event id set cursor there
char * filename
Definition: dump1090.h:266
Status GotoPosition(const unsigned int position) override
goto by position in the file
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
TEyeEvent * fCurEyeEvent
Definition: FDasEventFile.h:50

, generated on Tue Sep 26 2023.