1 | // $Id: SimuRootFileManager.cc 2863 2011-02-18 11:00:44Z biktem $ |
---|
2 | // Author: A.Thea 29/06/2004 |
---|
3 | |
---|
4 | /***************************************************************************** |
---|
5 | * ESAF: Euso Simulation and Analysis Framework * |
---|
6 | * * |
---|
7 | * Id: SimuRootFileManager * |
---|
8 | * Package: Gui * |
---|
9 | * Coordinator: Alessandro.Thea, Marco.Pallavicini * |
---|
10 | * * |
---|
11 | *****************************************************************************/ |
---|
12 | |
---|
13 | //_____________________________________________________________________________ |
---|
14 | // |
---|
15 | // Simu Rootfile Manager |
---|
16 | // ===================== |
---|
17 | // |
---|
18 | // Manager of the simulation root file. This object handles the creation and |
---|
19 | // the filling of the rootfile according to its configuration file. |
---|
20 | // |
---|
21 | // The manager controls creation and branching of the TTree and also is in |
---|
22 | // charge to split the file when its size is bigger than fMaxSize. |
---|
23 | // |
---|
24 | // Config file parameters |
---|
25 | // ====================== |
---|
26 | // |
---|
27 | // fSaveShower [bool]: enable/disable EShower object in EEvent |
---|
28 | // |
---|
29 | // fSaveAtmosphere [bool]: enable/disable EAtmosphere object in EEvent |
---|
30 | // |
---|
31 | // fSaveDetector [bool]: enable/disable EDetector object in EEvent |
---|
32 | // |
---|
33 | // fSaveChipTrackTrigger [bool]: enable/disable EChipTrackTrigger in EEvent |
---|
34 | // |
---|
35 | // fSaveLblTrackTrigger [bool]: enable/disable ELblTrackTrigger in EEvent |
---|
36 | // |
---|
37 | // fDetector.fPhotonFillable [bool]: enable/disable EPhotons in EEvent |
---|
38 | // |
---|
39 | // fDetector.fNightGlowFillable [bool]: enable/disable all NightGlow EFee |
---|
40 | // hits in EEvent |
---|
41 | // |
---|
42 | // fSaveRunTree [bool]: enable/disable runtree in rootfile |
---|
43 | // |
---|
44 | // fMaxFileSize [Mbyte]: Maximum file size. |
---|
45 | // |
---|
46 | |
---|
47 | #include "SimuRootFileManager.hh" |
---|
48 | #include "EEvent.hh" |
---|
49 | #include "EDetector.hh" |
---|
50 | #include "ESimpleDetector.hh" |
---|
51 | #include "ERunParameters.hh" |
---|
52 | #include "Config.hh" |
---|
53 | |
---|
54 | #include "TTree.h" |
---|
55 | #include "TBranchRef.h" |
---|
56 | #include "TSystem.h" |
---|
57 | #include "TTimeStamp.h" |
---|
58 | |
---|
59 | ClassImp (SimuRootFileManager) |
---|
60 | //_____________________________________________________________________________ |
---|
61 | SimuRootFileManager::SimuRootFileManager ():fEvent (0), fRunPars (0), |
---|
62 | fTree (0), fFile (0), |
---|
63 | fFileNumber (0) |
---|
64 | { //fRunTree(0) |
---|
65 | // |
---|
66 | // Constructor |
---|
67 | // |
---|
68 | |
---|
69 | fEventBranches = EEvent::kAll; |
---|
70 | fDetectorPhotonFillingMode = EDetector::kAllPhotons; |
---|
71 | |
---|
72 | // |
---|
73 | ConfigFileParser * |
---|
74 | pConfig = Config::Get ()->GetCF ("LightToEuso", "StandardLightToEuso"); |
---|
75 | string |
---|
76 | noevent = pConfig->GetStr ("StandardLightToEuso.fGenerator"); |
---|
77 | |
---|
78 | // read setting for the event |
---|
79 | if (!Conf ()->GetBool ("SimuRootFileManager.fSaveShower") |
---|
80 | || (noevent == "noevent")) |
---|
81 | fEventBranches &= ~EEvent::kShower; |
---|
82 | if (!Conf ()->GetBool ("SimuRootFileManager.fSaveAtmosphere")) |
---|
83 | fEventBranches &= ~EEvent::kAtmosphere; |
---|
84 | if (!Conf ()->GetBool ("SimuRootFileManager.fSaveDetector")) |
---|
85 | fEventBranches &= ~EEvent::kDetector; |
---|
86 | if (!Conf ()->GetBool ("SimuRootFileManager.fSaveChipTrackTrigger")) |
---|
87 | fEventBranches &= ~EEvent::kChipTrackTrigger; |
---|
88 | if (!Conf ()->GetBool ("SimuRootFileManager.fSaveLblTrackTrigger")) |
---|
89 | fEventBranches &= ~EEvent::kLblTrackTrigger; |
---|
90 | if (!Conf ()->GetBool ("SimuRootFileManager.fSaveSimpleDetector")) |
---|
91 | fEventBranches &= ~EEvent::kSimpleDetector; |
---|
92 | if (!Conf ()->GetBool ("SimuRootFileManager.fSavePTTTrigger")) |
---|
93 | fEventBranches &= ~EEvent::kPTTTrigger; |
---|
94 | if (!Conf ()->GetBool ("SimuRootFileManager.fSaveLTTTrigger")) |
---|
95 | fEventBranches &= ~EEvent::kLTTTrigger; |
---|
96 | if (!Conf ()->GetBool ("SimuRootFileManager.fSaveCCB_LTTTrigger")) |
---|
97 | fEventBranches &= ~EEvent::kCCB_LTTTrigger; |
---|
98 | |
---|
99 | |
---|
100 | // default: save photons in EDetector |
---|
101 | string |
---|
102 | phmode = |
---|
103 | Conf ()->GetStr ("SimuRootFileManager.fDetector.fPhotonFillingMode"); |
---|
104 | if (phmode == "All") |
---|
105 | fDetectorPhotonFillingMode = EDetector::kAllPhotons; |
---|
106 | else if (phmode == "MadeSignalOnly") |
---|
107 | fDetectorPhotonFillingMode = EDetector::kMadeSignalOnly; |
---|
108 | else if (phmode == "None") |
---|
109 | fDetectorPhotonFillingMode = EDetector::kNoPhotons; |
---|
110 | else |
---|
111 | { |
---|
112 | Msg (EsafMsg::Error) << "In file " << Conf ()->GetCfgDir () + "/" + |
---|
113 | Conf ()->GetPath () << MsgDispatch; |
---|
114 | FatalError |
---|
115 | ("Variable fPhotonFillingMode must be chosen among the following values:"); |
---|
116 | } |
---|
117 | |
---|
118 | fDetectorNightGlowFillable = |
---|
119 | Conf ()->GetBool ("SimuRootFileManager.fDetector.fNightGlowFillable"); |
---|
120 | |
---|
121 | fMaxFileSize = |
---|
122 | (Long64_t) Conf ()->GetNum ("SimuRootFileManager.fMaxFileSize") * 1024 * |
---|
123 | 1024; |
---|
124 | |
---|
125 | if (fMaxFileSize > TTree::GetMaxTreeSize ()) |
---|
126 | { |
---|
127 | // if fMaxFileSize exceeds the maximum size of a TTree, reset the size |
---|
128 | // 100 Mb smaller than fgMaxTreeSize to avoid conflicts |
---|
129 | |
---|
130 | TTree::SetMaxTreeSize (fMaxFileSize + 100 * 1024 * 1024); |
---|
131 | MsgForm (EsafMsg::Warning, |
---|
132 | "fMaxFileSize(=%ld) exceeds TTree maximum size", fMaxFileSize); |
---|
133 | MsgForm (EsafMsg::Warning, "Re-setting TTree max size to %ld", |
---|
134 | TTree::GetMaxTreeSize ()); |
---|
135 | } |
---|
136 | |
---|
137 | // build data containers |
---|
138 | Build (); |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | //_____________________________________________________________________________ |
---|
143 | SimuRootFileManager::~SimuRootFileManager () |
---|
144 | { |
---|
145 | // |
---|
146 | // Destructor |
---|
147 | // |
---|
148 | |
---|
149 | SafeDelete (fEvent); |
---|
150 | SafeDelete (fTree); |
---|
151 | |
---|
152 | SafeDelete (fFile); |
---|
153 | } |
---|
154 | |
---|
155 | //_____________________________________________________________________________ |
---|
156 | Bool_t |
---|
157 | SimuRootFileManager::Build () |
---|
158 | { |
---|
159 | // |
---|
160 | // build event and run parameters and set the static pointers |
---|
161 | // |
---|
162 | |
---|
163 | if (fEvent) |
---|
164 | delete fEvent; |
---|
165 | fEvent = 0; |
---|
166 | |
---|
167 | fEvent = new EEvent (fEventBranches); |
---|
168 | EEvent::SetCurrent (fEvent); |
---|
169 | |
---|
170 | fRunPars = fEvent->GetRunPars (); |
---|
171 | |
---|
172 | if (fEvent->GetDetector ()) |
---|
173 | { |
---|
174 | fEvent->GetDetector ()-> |
---|
175 | SetPhotonFillingMode (fDetectorPhotonFillingMode); |
---|
176 | fEvent->GetDetector ()-> |
---|
177 | SetNightGlowFillable (fDetectorNightGlowFillable); |
---|
178 | } |
---|
179 | |
---|
180 | if (fEvent->GetSimpleDetector ()) |
---|
181 | { |
---|
182 | fEvent->GetSimpleDetector ()-> |
---|
183 | SetPhotonFillingMode (fDetectorPhotonFillingMode); |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | return kTRUE; |
---|
188 | } |
---|
189 | |
---|
190 | //_____________________________________________________________________________ |
---|
191 | void |
---|
192 | SimuRootFileManager::Clear () |
---|
193 | { |
---|
194 | // |
---|
195 | // Clear all objects |
---|
196 | // |
---|
197 | |
---|
198 | fEvent->Clear ("C"); |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | //_____________________________________________________________________________ |
---|
203 | Bool_t |
---|
204 | SimuRootFileManager::Open (const char *name) |
---|
205 | { |
---|
206 | // |
---|
207 | // Open a new rootfile and set the trees up |
---|
208 | // |
---|
209 | |
---|
210 | TString ext = ".root"; |
---|
211 | fFileName = |
---|
212 | name ? name : Conf ()->GetStr ("SimuRootFileManager.fRootOutputFile"); |
---|
213 | |
---|
214 | if (!fFileName.EndsWith (ext)) |
---|
215 | fFileName += ext; |
---|
216 | |
---|
217 | if (!gSystem->AccessPathName (fFileName)) |
---|
218 | { |
---|
219 | // fname.root exists. try to add current date |
---|
220 | |
---|
221 | MsgForm (EsafMsg::Warning, "Error opening file"); |
---|
222 | MsgForm (EsafMsg::Warning, |
---|
223 | "Probably it already exists or there is no "); |
---|
224 | MsgForm (EsafMsg::Warning, "write access permission to this directory"); |
---|
225 | |
---|
226 | TTimeStamp now; |
---|
227 | |
---|
228 | UInt_t hour, min, sec, year, month, day; |
---|
229 | now.GetDate (kFALSE, 0, &year, &month, &day); |
---|
230 | now.GetTime (kFALSE, 0, &hour, &min, &sec); |
---|
231 | TString time = |
---|
232 | Form (".%4d-%02d-%02d-%02dh%02dm%02ds", year, month, day, hour, min, |
---|
233 | sec); |
---|
234 | fFileName.Insert (fFileName.Length () - ext.Length (), time); |
---|
235 | |
---|
236 | } |
---|
237 | |
---|
238 | fFile = new TFile (fFileName, "CREATE"); |
---|
239 | if (fFile->IsZombie ()) |
---|
240 | MsgForm (EsafMsg::Panic, "File %s cannot be opened", fFileName.Data ()); |
---|
241 | |
---|
242 | MsgForm (EsafMsg::Info, "Root file %s successfully opened", |
---|
243 | fFileName.Data ()); |
---|
244 | |
---|
245 | if (fTree) |
---|
246 | delete fTree; |
---|
247 | fTree = new TTree ("etree", "Events Tree"); |
---|
248 | |
---|
249 | return kTRUE; |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | //______________________________________________________________________________ |
---|
254 | void |
---|
255 | SimuRootFileManager::LockEvent () |
---|
256 | { |
---|
257 | // |
---|
258 | // Branch the tree but not the run parameters. |
---|
259 | // The run parameters will be added by hand in the Unlock |
---|
260 | // |
---|
261 | if (!fTree) |
---|
262 | return; |
---|
263 | |
---|
264 | fEvent->BranchTree (fTree, kFALSE); |
---|
265 | |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | //______________________________________________________________________________ |
---|
270 | void |
---|
271 | SimuRootFileManager::UnlockEvent () |
---|
272 | { |
---|
273 | // |
---|
274 | // Disconnects the Event from the tree. |
---|
275 | // |
---|
276 | |
---|
277 | if (!fTree) |
---|
278 | return; |
---|
279 | |
---|
280 | // check the current file |
---|
281 | if ((fFile != fTree->GetCurrentFile ())) |
---|
282 | MsgForm (EsafMsg::Warning, "Mismatch among tree files and opened file"); |
---|
283 | |
---|
284 | if (fEvent->IsLinked ()) |
---|
285 | MsgForm (EsafMsg::Warning, "The event is linked with the tree"); |
---|
286 | |
---|
287 | // Clone parameters and store them in the tree |
---|
288 | TObject *pars = fEvent->GetRunPars ()->Clone (); |
---|
289 | fTree->GetUserInfo ()->Add (pars); |
---|
290 | |
---|
291 | // Save the tree into the file |
---|
292 | fFile->cd (); |
---|
293 | fTree->Write (); |
---|
294 | |
---|
295 | // Delete the tree |
---|
296 | SafeDelete (fTree); |
---|
297 | } |
---|
298 | |
---|
299 | //_____________________________________________________________________________ |
---|
300 | Bool_t |
---|
301 | SimuRootFileManager::Close () |
---|
302 | { |
---|
303 | // |
---|
304 | // Save all trees and close current rootfile |
---|
305 | // |
---|
306 | |
---|
307 | if (!fFile) |
---|
308 | return kFALSE; |
---|
309 | |
---|
310 | // check file |
---|
311 | // if ( ( fTree && fFile != fTree->GetCurrentFile() ) ) |
---|
312 | // MsgForm(EsafMsg::Warning, "Mismatch among tree files and opened file"); |
---|
313 | |
---|
314 | fFile->cd (); |
---|
315 | fFile->Write (); |
---|
316 | |
---|
317 | // SafeDelete(fTree); |
---|
318 | //SafeDelete(fRunTree); |
---|
319 | |
---|
320 | fFile->Close (); |
---|
321 | MsgForm (EsafMsg::Info, "%s closed.", fFile->GetName ()); |
---|
322 | |
---|
323 | SafeDelete (fFile) return kTRUE; |
---|
324 | } |
---|
325 | |
---|
326 | //______________________________________________________________________________ |
---|
327 | Int_t |
---|
328 | SimuRootFileManager::FillEvent () |
---|
329 | { |
---|
330 | // |
---|
331 | // Calls fTree->Fill and updates fFile if nedded. |
---|
332 | // It is forseen that it will also handle automatic file splitting when |
---|
333 | // approacing to size limit. |
---|
334 | // |
---|
335 | |
---|
336 | TString ext = ".root"; |
---|
337 | |
---|
338 | if (!fTree) |
---|
339 | return 0; |
---|
340 | |
---|
341 | if (fFile->GetEND () > fMaxFileSize) |
---|
342 | { |
---|
343 | Msg (EsafMsg:: |
---|
344 | Warning) << |
---|
345 | "Current root file has reached the maximum allowed size." << |
---|
346 | " Switching to a new TFile" << MsgDispatch; |
---|
347 | fFile->cd (); |
---|
348 | fFile->Write (); |
---|
349 | |
---|
350 | fTree->Reset (); |
---|
351 | |
---|
352 | // open the new file |
---|
353 | fFileNumber++; |
---|
354 | TString newfilename = fFileName; |
---|
355 | newfilename.Insert (newfilename.Length () - ext.Length (), |
---|
356 | Form ("_%d", fFileNumber)); |
---|
357 | TFile *newfile = new TFile (newfilename, "CREATE"); |
---|
358 | |
---|
359 | if (newfile->IsZombie ()) |
---|
360 | MsgForm (EsafMsg::Panic, "Cannot open %s", newfilename.Data ()); |
---|
361 | |
---|
362 | Msg (EsafMsg:: |
---|
363 | Warning) << newfilename << " succesfully open." << MsgDispatch; |
---|
364 | |
---|
365 | //WARNING: does TTree:Clone() mantain the connecton with the branched objects? |
---|
366 | //TTree* newruntree = (TTree*)fRunTree->CloneTree(); |
---|
367 | |
---|
368 | // move events tree to the new file |
---|
369 | TBranch *branch; |
---|
370 | fTree->SetDirectory (newfile); |
---|
371 | TIter nextb (fTree->GetListOfBranches ()); |
---|
372 | while ((branch = (TBranch *) nextb ())) |
---|
373 | { |
---|
374 | branch->SetFile (newfile); |
---|
375 | } |
---|
376 | |
---|
377 | if (fTree->GetBranchRef ()) |
---|
378 | fTree->GetBranchRef ()->SetFile (newfile); |
---|
379 | |
---|
380 | delete fFile; |
---|
381 | fFile = newfile; |
---|
382 | |
---|
383 | //WARNING: what shall I do with the old tree? |
---|
384 | //fRunTree = newruntree; |
---|
385 | |
---|
386 | } |
---|
387 | fFile->cd (); |
---|
388 | Int_t bytes = fTree->Fill (); |
---|
389 | fFile = fTree->GetCurrentFile (); |
---|
390 | return bytes; |
---|
391 | } |
---|
392 | |
---|
393 | //______________________________________________________________________________ |
---|
394 | void |
---|
395 | SimuRootFileManager::ResetRunPars () |
---|
396 | { |
---|
397 | // |
---|
398 | // Reset run parameters and their reference |
---|
399 | // |
---|
400 | |
---|
401 | if (!fRunPars) |
---|
402 | return; |
---|
403 | |
---|
404 | // clear object |
---|
405 | fRunPars->Clear (); |
---|
406 | |
---|
407 | // reset unique id |
---|
408 | fRunPars->SetUniqueID (0); |
---|
409 | fRunPars->ResetBit (kHasUUID); |
---|
410 | |
---|
411 | // generate a new UID |
---|
412 | fRunParsRef = fRunPars; |
---|
413 | } |
---|