CDASEventFile.cc
Go to the documentation of this file.
1 #include <io/CDASEventFile.h>
2 
3 #include <io/CDASToOfflineEventConverter.h>
4 
5 #include <evt/Event.h>
6 #include <evt/Header.h>
7 
8 #include <sevt/Header.h>
9 #include <sevt/SEvent.h>
10 #include <sevt/Station.h>
11 
12 #include <utl/AugerCoordinateSystem.h>
13 #include <utl/ErrorLogger.h>
14 #include <utl/TimeStamp.h>
15 #include <utl/UTMPoint.h>
16 #include <utl/SaveCurrentTDirectory.h>
17 
18 #include <config.h>
19 
20 #include <IoSdData.h>
21 #include <AugerEvent.h>
22 
23 #include <TKey.h>
24 #include <TFile.h>
25 
26 #include <cstddef>
27 #include <iostream>
28 #include <sstream>
29 
30 using namespace std;
31 using namespace io;
32 using namespace evt;
33 using namespace sevt;
34 using namespace utl;
35 
36 
37 void
38 CDASEventFile::Open(const std::string& filename, const Mode mode, utl::Branch* const b)
39 {
40  Close();
41 
42  VROOTFile::Open(filename, mode, b);
43 
44  if (!fFile)
45  throw utl::IOFailureException("Cannot create file for CDAS io");
46 
47  if (fFile->IsZombie())
48  throw utl::IOFailureException("CDAS file in zombie io state");
49 
50  fCurrentEvent = 0;
51 }
52 
53 
54 void
55 CDASEventFile::Close()
56 {
57  if (!fFile)
58  return;
59 
60  VROOTFile::Close();
61 
62  delete fFile;
63  fFile = nullptr;
64 
65  fCurrentEvent = -1;
66 }
67 
68 
69 void
70 CDASEventFile::Write(const evt::Event& event)
71 {
72  if (fMode == eRead)
73  throw utl::IOFailureException("Attempted write into read-only CDAS file");
74 
75  if (fFile) {
76 
77  if (!event.HasRawEvent() || !event.GetRawEvent().HasSd()) {
78 
79  INFO("No raw SD event to write");
80  return;
81 
82  }
83 
84  IoSdEvent* const rawSEvent = &const_cast<evt::Event&>(event).GetRawEvent().Sd();
85 
86  const SaveCurrentTDirectory save;
87  fFile->cd();
88  rawSEvent->Write(rawSEvent->Key());
89  ++fCurrentEvent;
90 
91  }
92 }
93 
94 
95 Status
96 CDASEventFile::Read(evt::Event& event)
97 {
98  if (fMode == eWrite || fMode == eNew || fMode == eAppend)
99  throw utl::IOFailureException("Attempted read from write-only CDAS file");
100 
101  if (fFile) {
102 
103  const SaveCurrentTDirectory save;
104  fFile->cd();
105 
106  TKey* const theKey =
107  dynamic_cast<TKey*>(fFile->GetListOfKeys()->At(fCurrentEvent));
108 
109  if (theKey) {
110 
111  const IoSdEvent* const theRawSdEvent =
112  dynamic_cast<const IoSdEvent*>(fFile->Get(theKey->GetName()));
113 
114  if (theRawSdEvent) {
115 
116  // put the raw in
117  if (!event.HasRawEvent())
118  event.MakeRawEvent();
119 
120  if (event.GetRawEvent().HasSd())
121  event.GetRawEvent().Sd() = *theRawSdEvent;
122  else
123  event.GetRawEvent().PushEvent(*theRawSdEvent);
124 
125  if (!event.HasSEvent())
126  event.MakeSEvent();
127 
128  sevt::SEvent& sEvent = event.GetSEvent();
129 
130  const IoSdT3Trigger* const rawTrigger = &theRawSdEvent->Trigger;
131  const unsigned int second = rawTrigger->Second;
132  const unsigned int microSecond =
133  int(rawTrigger->MicroSecond*microsecond/nanosecond);
134  const TimeStamp currentTime(second, microSecond);
135 
136  evt::Header& header = event.GetHeader();
137  ostringstream id_str;
138  if (!header.GetId().empty())
139  id_str << header.GetId() << "__";
140  id_str << "sd_" << theRawSdEvent->Id;
141 
142  sevt::Header& sHeader = sEvent.GetHeader();
143  sHeader.SetId(theRawSdEvent->Id);
144  sHeader.SetTime(currentTime);
145 
146  header.SetTime(currentTime);
147  header.SetId(id_str.str());
148 
149  ++fCurrentEvent;
150 
151  // Remember to do this because we get handed a new copy of the object
152  // when grabbing it from file. This is not documented within ROOT, so
153  // be careful when doing this.
154 
155  delete theRawSdEvent;
156  return eSuccess;
157 
158  }
159 
160  }
161 
162  }
163 
164  return eEOF;
165 }
166 
167 
168 Status
169 CDASEventFile::FindEvent(const unsigned int id)
170 {
171  if (fFile) {
172 
173  const SaveCurrentTDirectory save;
174  fFile->cd();
175  fFile->Seek(0);
176  fCurrentEvent = 0;
177 
178  while (fCurrentEvent < fFile->GetNkeys()) {
179 
180  TKey* const theKey =
181  dynamic_cast<TKey*>(fFile->GetListOfKeys()->At(fCurrentEvent));
182 
183  if (theKey) {
184 
185  // The key is of the form id#?#.... so just take the number before
186  // the first '#' to get the event id.
187 
188  const string keyName(theKey->GetName());
189  const string eventNumber(keyName.substr(keyName.find_first_of("#")));
190 
191  if ((unsigned long)atol(eventNumber.c_str()) == id)
192  return eSuccess;
193 
194  ++fCurrentEvent;
195 
196  }
197 
198  }
199 
200  }
201 
202  return eEOF;
203 }
204 
205 
206 Status
207 CDASEventFile::GotoPosition(const unsigned int position)
208 {
209  if (fFile) {
210 
211  if (position >= (unsigned int)fFile->GetNkeys())
212  return eEOF;
213 
214  fFile->Seek(position);
215  fCurrentEvent = position;
216  return eSuccess;
217 
218  }
219 
220  return eEOF;
221 }
222 
223 
224 int
225 CDASEventFile::GetNEvents()
226 {
227  return fFile->GetNkeys();
228 }
void SetTime(const utl::TimeStamp &t)
Definition: Event/Header.h:38
Interface class to access to the SD part of an event.
Definition: SEvent.h:39
Open file for write, fail if exists.
Definition: IoCodes.h:19
void SetTime(const utl::TimeStamp &time)
Definition: SEvent/Header.h:22
Mode
Available open modes.
Definition: IoCodes.h:16
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
Header file holding the SD Event Trigger class definition.
Definition: SEvent/Header.h:16
Read Only access.
Definition: IoCodes.h:18
AugerEvent & GetRawEvent()
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
Base class to report exceptions in IO.
void SetId(const std::string &id)
Set the event identifier.
Definition: Event/Header.h:36
Class representing a document branch.
Definition: Branch.h:107
Status
Return code for seek operation.
Definition: IoCodes.h:24
constexpr double nanosecond
Definition: AugerUnits.h:143
const double second
Definition: GalacticUnits.h:32
const std::string & GetId() const
Get the event identifier.
Definition: Event/Header.h:31
void SetId(const int id)
Definition: SEvent/Header.h:23
Overwrite if exist and open for write.
Definition: IoCodes.h:20
bool HasRawEvent() const
sevt::Header & GetHeader()
Definition: SEvent.h:155
char * filename
Definition: dump1090.h:266
constexpr double microsecond
Definition: AugerUnits.h:147
bool HasSEvent() const
Global event header.
Definition: Event/Header.h:27

, generated on Tue Sep 26 2023.