OfflineROOTFile.cc
Go to the documentation of this file.
1 #include <fwk/CentralConfig.h>
2 
3 #include <utl/ErrorLogger.h>
4 #include <utl/SaveCurrentTDirectory.h>
5 #include <utl/Branch.h>
6 
7 #include <evt/Event.h>
8 
9 #include <io/Event_ROOT.h>
10 #include <io/Config_ROOT.h>
11 #include <io/Eye_ROOT.h>
12 #include <io/Telescope_ROOT.h>
13 #include <io/PMTSimData_ROOT.h>
14 #include <io/PixelSimData_ROOT.h>
15 #include <io/Pixel_ROOT.h>
16 #include <io/ShowerSimData_ROOT.h>
17 #include <io/PMTSimData_ROOT.h>
18 #include <io/StationSimData_ROOT.h>
19 #include <io/Station_ROOT.h>
20 
21 #include <io/OfflineROOTFile.h>
22 
23 #include <AugerEvent.h>
24 
25 #include <TTree.h>
26 #include <TBranch.h>
27 #include <TFile.h>
28 #include <TDataMember.h>
29 #include <TList.h>
30 #include <TStreamerInfo.h>
31 
32 #include <fstream>
33 
34 using namespace io;
35 using namespace std;
36 using evt::Event;
37 
38 
39 const std::string OfflineROOTFile::kOfflineTreeName = "AugerOffline";
40 const std::string OfflineROOTFile::kOfflineBranchName = "Event";
41 const std::string OfflineROOTFile::kConfigBranchName = "Config";
43 
44 
45 OfflineROOTFile::OfflineROOTFile(const std::string& filename, const Mode mode,
46  utl::Branch* const /*branch*/,
47  const io::StreamerSwitch& streamerSwitch) :
48  VROOTFile(filename, mode),
49  fStreamerSwitch(streamerSwitch)
50 {
51  // Note that each ROOT file has exactly 1 configuration object
52  // associated with it. Configuration is currently a once-per-run
53  // concept. A single run producing multuple OfflineROOTFiles will
54  // write the same configuration into each one. Multiple runs
55  // producing 1 OutputROOTFile each will, of course, write
56  // a different configuration into each file.
57 
58  fConfig = new Config_ROOT();
59 }
60 
61 
62 void
63 OfflineROOTFile::Open(const std::string& filename, const Mode mode,
64  utl::Branch* const branch)
65 {
66  // If requested, dump the configuration info found
67  // in the event into a file
68 
69  if (branch && *branch) {
70 
71  utl::Branch dumpConfigB = branch->GetChild("DumpConfig");
72  if (dumpConfigB) {
73  string dumpFileName;
74  dumpConfigB.GetData(dumpFileName);
75  if (fConfigOut) {
76  fConfigOut->close();
77  delete fConfigOut;
78  }
79  fConfigOut = new ofstream(dumpFileName.c_str());
80  ostringstream inf;
81  inf << "Dumping previous Offline config into output: " << dumpFileName;
82  INFO(inf);
83  }
84  }
85 
86  VROOTFile::Open(filename, mode);
87 }
88 
89 
91 {
92  if (fConfigOut) {
93  fConfigOut->close();
94  delete fConfigOut;
95  fConfigOut = nullptr;
96  }
97 
98  delete fCurEvent;
99  delete fConfig;
100 }
101 
102 
103 void
105 {
106  if (fConfigOut) {
107  fConfigOut->close();
108  delete fConfigOut;
109  fConfigOut = nullptr;
110  }
111 
113 }
114 
115 
116 Status
118 {
119  // Create new streaming object (in eRead mode it stays NULL)
120  if (fMode != eRead) {
121  delete fCurEvent;
122  fCurEvent = new Event_ROOT();
123  }
124 
125  // Get existing TTree from file, if there should be one
126  if ((fMode == eRead || fMode == eAppend)) {
127 
128  if (GetTree(kOfflineTreeName) == -1) {
129  ERROR ("Could Not Read because Tree is NULL");
130  return eFail;
131  }
132 
133  // Get TBranch from TTree, or create new TBranch if needed
134  fBranch = fTree->GetBranch(kOfflineBranchName.c_str());
135  fBranch->SetAddress(&fCurEvent);
136 
137  } else { // fMode == eWrite
138 
139  const Int_t persistent = 4;
140 
141  // Set streamers on or off according to fStreamerSwitch
142  if (!fStreamerSwitch.GetRaw())
143  Event_ROOT::Class()->GetDataMember("fRawEvent")->SetBit(persistent, false);
144 
145  if (!fStreamerSwitch.GetFDRaw())
146  AugerEvent::Class()->GetDataMember("Eyes")->SetBit(persistent, false);
147 
148  // this switch removes a bunch of data ONLY used during FD simulation
150  Telescope_ROOT::Class()->GetDataMember("fChannels")->SetBit(persistent, false);
151  Pixel_ROOT::Class()->GetDataMember("fSimData")->SetBit(persistent, false);
152  ShowerSimData_ROOT::Class()->GetDataMember("fFluorescencePhotons")->SetBit(persistent, false);
153  ShowerSimData_ROOT::Class()->GetDataMember("fCherenkovPhotons")->SetBit(persistent, false);
154  ShowerSimData_ROOT::Class()->GetDataMember("fCherenkovBeamPhotons")->SetBit(persistent, false);
155  ShowerSimData_ROOT::Class()->GetDataMember("fCherenkovBeamProductionPhotons")->SetBit(persistent, false);
156  }
157 
159  Telescope_ROOT::Class()->GetDataMember("fSim")->SetBit(persistent, false);
161  PMTSimData_ROOT::Class()->GetDataMember("fPETimeDistribution")->SetBit(persistent, false);
163  PMTSimData_ROOT::Class()->GetDataMember("fBaseSignals")->SetBit(persistent, false);
165  PMTSimData_ROOT::Class()->GetDataMember("fFilterSignals")->SetBit(persistent, false);
167  StationSimData_ROOT::Class()->GetDataMember("fParticles")->SetBit(persistent, false);
168 
169  const utl::SaveCurrentTDirectory save;
170  fFile->cd();
171  delete fTree;
172  fTree = new TTree(kOfflineTreeName.c_str(), "Auger Offline Tree");
173 
174  fBranch = fTree->Branch(kOfflineBranchName.c_str(), Event_ROOT::Class_Name(), &fCurEvent);
175  fConfigBranch = fTree->Branch(kConfigBranchName.c_str(), Config_ROOT::Class_Name(), &fConfig);
177  }
178 
179  if (!fBranch) {
180  ERROR("Could Not Read branch");
181  return eFail;
182  }
183 
184  return eSuccess;
185 }
186 
187 
188 void
190 {
191  if (!fTree && GetEventBranch() == eFail)
192  throw utl::IOFailureException("IO error while writing");
193 
194  *fCurEvent = event;
195  fTree->Fill();
196 }
197 
198 
199 Status
201 {
202  if (!fTree && GetEventBranch() == eFail)
203  return eFail;
204 
205  if (fCurEventNo == (unsigned int)GetNEvents())
206  return eEOF;
207 
208  fBranch->GetEntry(fCurEventNo++);
209  *fCurEvent >> event;
210 
211  // Write out configuration (if requested)
212  if (fConfigOut)
213  *fConfigOut << GetConfig() << endl;
214 
215  return eSuccess;
216 }
217 
218 
219 Status
220 OfflineROOTFile::FindEvent(const unsigned int /*evNo*/)
221 {
222  ERROR("OfflineROOTFile::FindEvent not implemented");
223  return eFail;
224 }
225 
226 
227 Status
228 OfflineROOTFile::GotoPosition(const unsigned int evNo)
229 {
230  if (evNo > (unsigned int)(GetNEvents())) {
231  ERROR("Trying to move past End of File");
232  return eEOF;
233  }
234 
235  fCurEventNo = evNo;
236  return eSuccess;
237 }
238 
239 
240 int
242 {
243  if (!fTree)
244  GetEventBranch();
245  if (!fTree)
246  return 0;
247  return fTree->GetEntries();
248 }
249 
250 
251 string
253 {
254  if (GetTree(kOfflineTreeName) == -1) {
255  ERROR("Could Not Read because Tree is NULL");
256  exit(EXIT_FAILURE);
257  }
258 
259  fConfigBranch = fTree->GetBranch(kConfigBranchName.c_str());
260  if (!fConfigBranch) {
261  INFO("Could not find a configuration branch. "
262  "Is it possible this event was written with Offline version 1.3 or older?");
263  return "No configuration data available.";
264  }
265 
266  fConfigBranch->SetAddress(&fConfig);
267  fConfigBranch->GetEntry();
268  return fConfig->fConfigString;
269 }
std::string GetConfig()
Return a string with the configuration corresponding to the run.
TTree * fTree
Definition: VROOTFile.h:46
int GetNEvents() override
std::ofstream * fConfigOut
switches to allow excluding certain parts of Event from streaming
Holds the run configuration data from CentralConfig.
Definition: Config_ROOT.h:19
Status GotoPosition(const unsigned int evNo) override
goto by position in the file
std::string GetConfig()
Get configuration in a string.
Mode
Available open modes.
Definition: IoCodes.h:16
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
int exit
Definition: dump1090.h:237
virtual void Close()
Definition: VROOTFile.cc:89
unsigned int fCurEventNo
OfflineROOTFile()=default
Event Class being actually streamed when using ROOT IO.
Definition: Event_ROOT.h:33
Base class to report exceptions in IO.
io::StreamerSwitch fStreamerSwitch
bool GetRaw() const
Get value of RAW switch.
bool GetSDSimPETimeDistribution() const
Get value of SDSimPETimeDistribution switch.
Class representing a document branch.
Definition: Branch.h:107
Status
Return code for seek operation.
Definition: IoCodes.h:24
bool GetFDSimPhotonTraces() const
Get value of FDSimPhotonTraces.
void Write(const evt::Event &event) override
void Close() override
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
TFile * fFile
Definition: VROOTFile.h:45
static const std::string kConfigBranchName
int GetTree(const std::string &name)
Definition: VROOTFile.cc:105
bool GetSDSimPMTFilterTimeDistribution() const
Get value of SDPMTFilterTimeDistribution switch.
bool GetSDSimTankParticles() const
void Open(const std::string &filename, const Mode mode, utl::Branch *const b=nullptr) override
virtual void Open(const std::string &filename, const Mode mode=eRead, utl::Branch *const b=nullptr)
Definition: VROOTFile.cc:52
static const std::string kOfflineTreeName
static CentralConfig * GetInstance()
Use this the first time you get an instance of central configuration.
Status FindEvent(const unsigned int eventId) override
On success returns event number in the file.
static const std::string kOfflineBranchName
io::Event_ROOT * fCurEvent
Overwrite if exist and open for write.
Definition: IoCodes.h:20
io::Config_ROOT * fConfig
char * filename
Definition: dump1090.h:266
bool GetFDSimTelescope() const
Get value of FDSimPhotonTraces.
bool GetFDRaw() const
Get value of FDRaw switch.
static const io::StreamerSwitch fgDefaultSwitch
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
bool GetSDSimPMTBaseTimeDistribution() const
Get value of SDSimPMTBaseTimeDistribution switch (from SD simulation)
std::string fConfigString
Definition: Config_ROOT.h:21
Status Read(evt::Event &event) override
Returns -1 if End Of File.

, generated on Tue Sep 26 2023.