1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz, 6/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2003
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | // //
|
---|
27 | // MWriteRootFile //
|
---|
28 | // //
|
---|
29 | // This is a writer to store several containers to a root file. //
|
---|
30 | // The containers are added with AddContainer. //
|
---|
31 | // To understand how it works, see base class MWriteFile //
|
---|
32 | // //
|
---|
33 | // Warning: Checkout the Warning in MTaskList. //
|
---|
34 | // //
|
---|
35 | /////////////////////////////////////////////////////////////////////////////
|
---|
36 | #include "MWriteRootFile.h"
|
---|
37 |
|
---|
38 | #include <fstream>
|
---|
39 |
|
---|
40 | #include <TFile.h>
|
---|
41 | #include <TTree.h>
|
---|
42 |
|
---|
43 | #include "MLog.h"
|
---|
44 | #include "MLogManip.h"
|
---|
45 |
|
---|
46 | #include "MParList.h"
|
---|
47 |
|
---|
48 | ClassImp(MRootFileBranch);
|
---|
49 | ClassImp(MWriteRootFile);
|
---|
50 |
|
---|
51 | using namespace std;
|
---|
52 |
|
---|
53 | static const TString gsDefName = "MWriteRootFile";
|
---|
54 | static const TString gsDefTitle = "Task which writes a root-output file";
|
---|
55 |
|
---|
56 | // --------------------------------------------------------------------------
|
---|
57 | //
|
---|
58 | // Default constructor. It is there to support some root stuff.
|
---|
59 | // Don't use it.
|
---|
60 | //
|
---|
61 | MWriteRootFile::MWriteRootFile() : fOut(NULL)
|
---|
62 | {
|
---|
63 | fName = gsDefName;
|
---|
64 | fTitle = gsDefTitle;
|
---|
65 |
|
---|
66 | fBranches.SetOwner();
|
---|
67 | }
|
---|
68 |
|
---|
69 | // --------------------------------------------------------------------------
|
---|
70 | //
|
---|
71 | // Specify the name of the root file. You can also give an option ("UPDATE"
|
---|
72 | // and "RECREATE" would make sense only) as well as the file title and
|
---|
73 | // compression factor. To a more detaild description of the options see
|
---|
74 | // TFile.
|
---|
75 | //
|
---|
76 | MWriteRootFile::MWriteRootFile(const char *fname,
|
---|
77 | const Option_t *opt,
|
---|
78 | const char *ftitle,
|
---|
79 | const Int_t comp,
|
---|
80 | const char *name,
|
---|
81 | const char *title)
|
---|
82 | {
|
---|
83 | fName = name ? name : gsDefName.Data();
|
---|
84 | fTitle = title ? title : gsDefTitle.Data();
|
---|
85 |
|
---|
86 | //
|
---|
87 | // Set the Arrays the owner of its entries. This means, that the
|
---|
88 | // destructor of the arrays will delete all its entries.
|
---|
89 | //
|
---|
90 | fBranches.SetOwner();
|
---|
91 |
|
---|
92 | //
|
---|
93 | // Believing the root user guide, TTree instanced are owned by the
|
---|
94 | // directory (file) in which they are. This means we don't have to
|
---|
95 | // care about their destruction.
|
---|
96 | //
|
---|
97 | //fTrees.SetOwner();
|
---|
98 |
|
---|
99 | TString str(fname);
|
---|
100 | if (!str.EndsWith(".root", TString::kIgnoreCase))
|
---|
101 | str += ".root";
|
---|
102 |
|
---|
103 | //
|
---|
104 | // Open the rootfile
|
---|
105 | //
|
---|
106 | fOut = new TFile(str, opt, ftitle, comp);
|
---|
107 | }
|
---|
108 |
|
---|
109 | // --------------------------------------------------------------------------
|
---|
110 | //
|
---|
111 | // Prints some statistics about the file to the screen. And closes the file
|
---|
112 | // properly.
|
---|
113 | //
|
---|
114 | MWriteRootFile::~MWriteRootFile()
|
---|
115 | {
|
---|
116 | //
|
---|
117 | // Print some statistics to the looging out.
|
---|
118 | //
|
---|
119 | Print();
|
---|
120 |
|
---|
121 | //
|
---|
122 | // If the file is still open (no error) write the keys. This is necessary
|
---|
123 | // for appearance of the all trees and branches.
|
---|
124 | //
|
---|
125 | if (IsFileOpen())
|
---|
126 | fOut->Write();
|
---|
127 |
|
---|
128 | //
|
---|
129 | // Delete the file. This'll also close the file (if open)
|
---|
130 | //
|
---|
131 | delete fOut;
|
---|
132 |
|
---|
133 | //
|
---|
134 | // Remark:
|
---|
135 | // - Trees are automatically deleted by the the file
|
---|
136 | // (unless file.SetDirectory(0) was called)
|
---|
137 | // - Branches are automatically deleted by the tree destructor
|
---|
138 | //
|
---|
139 |
|
---|
140 | *fLog << inf << "Output File closed and object deleted." << endl;
|
---|
141 | }
|
---|
142 |
|
---|
143 | // --------------------------------------------------------------------------
|
---|
144 | //
|
---|
145 | // Prints all trees with the actually number of written entries to log-out.
|
---|
146 | //
|
---|
147 | void MWriteRootFile::Print(Option_t *) const
|
---|
148 | {
|
---|
149 | *fLog << all << underline << "File: " << GetFileName() << endl;
|
---|
150 |
|
---|
151 | if (fTrees.GetEntries()==0)
|
---|
152 | {
|
---|
153 | *fLog << " No contents." << endl;
|
---|
154 | return;
|
---|
155 | }
|
---|
156 |
|
---|
157 | TObject *obj;
|
---|
158 | TIter NextBranch(&fBranches);
|
---|
159 | while ((obj=NextBranch()))
|
---|
160 | {
|
---|
161 | MRootFileBranch *b = (MRootFileBranch*)obj;
|
---|
162 |
|
---|
163 | if (b->GetTree()->TestBit(kIsNewTree))
|
---|
164 | continue;
|
---|
165 |
|
---|
166 | TBranch *branch = b->GetBranch();
|
---|
167 |
|
---|
168 | TString name = b->GetTree()->GetName();
|
---|
169 | name += '.';
|
---|
170 | name += branch->GetName();
|
---|
171 |
|
---|
172 | *fLog << " " << name.Strip(TString::kTrailing, '.') << ": \t" << branch->GetEntries() << " entries." << endl;
|
---|
173 | }
|
---|
174 |
|
---|
175 | TTree *t = NULL;
|
---|
176 | TIter NextTree(&fTrees);
|
---|
177 | while ((t=(TTree*)NextTree()))
|
---|
178 | if (t->TestBit(kIsNewTree))
|
---|
179 | *fLog << " " << t->GetName() << ": \t" << t->GetEntries() << " entries." << endl;
|
---|
180 | *fLog << endl;
|
---|
181 | }
|
---|
182 |
|
---|
183 | // --------------------------------------------------------------------------
|
---|
184 | //
|
---|
185 | // Add a new Container to list of containers which should be written to the
|
---|
186 | // file. Give the name of the container which will identify the container
|
---|
187 | // in the parameterlist. tname is the name of the tree to which the
|
---|
188 | // container should be written (Remark: one tree can hold more than one
|
---|
189 | // container). The default is the same name as the container name.
|
---|
190 | // You can slso specify a title for the tree. This is only
|
---|
191 | // used the first time this tree in 'mentioned'. As default the title
|
---|
192 | // is the name of the tree.
|
---|
193 | //
|
---|
194 | void MWriteRootFile::AddContainer(const char *cname, const char *tname, const char *ttitle)
|
---|
195 | {
|
---|
196 | //
|
---|
197 | // create a new entry in the list of branches to write and
|
---|
198 | // add the entry to the list.
|
---|
199 | //
|
---|
200 | MRootFileBranch *entry = new MRootFileBranch(cname, tname, ttitle);
|
---|
201 | fBranches.AddLast(entry);
|
---|
202 |
|
---|
203 | if (tname && tname[0])
|
---|
204 | AddToBranchList(Form("%s.%s", cname, tname));
|
---|
205 | }
|
---|
206 |
|
---|
207 | // --------------------------------------------------------------------------
|
---|
208 | //
|
---|
209 | // Add a new Container to list of containers which should be written to the
|
---|
210 | // file. Give the pointer to the container. tname is the name of the tree to
|
---|
211 | // which the container should be written (Remark: one tree can hold more than
|
---|
212 | // one container). The default is the same name as the container name.
|
---|
213 | // You can slso specify a title for the tree. This is only
|
---|
214 | // used the first time this tree in 'mentioned'. As default the title
|
---|
215 | // is the name of the tree.
|
---|
216 | //
|
---|
217 | void MWriteRootFile::AddContainer(MParContainer *cont, const char *tname,
|
---|
218 | const char *ttitle)
|
---|
219 | {
|
---|
220 | //
|
---|
221 | // create a new entry in the list of branches to write and
|
---|
222 | // add the entry to the list.
|
---|
223 | //
|
---|
224 | MRootFileBranch *entry = new MRootFileBranch(cont, tname, ttitle);
|
---|
225 | fBranches.AddLast(entry);
|
---|
226 | }
|
---|
227 |
|
---|
228 | // --------------------------------------------------------------------------
|
---|
229 | //
|
---|
230 | // Add a new Container to list of containers which should be written to the
|
---|
231 | // file. Give the pointer to the container. tname is the name of the tree to
|
---|
232 | // which the container should be written (Remark: one tree can hold more than
|
---|
233 | // one container). The default is the same name as the container name.
|
---|
234 | // You can slso specify a title for the tree. This is only
|
---|
235 | // used the first time this tree in 'mentioned'. As default the title
|
---|
236 | // is the name of the tree.
|
---|
237 | //
|
---|
238 | Bool_t MWriteRootFile::GetContainer(MParList *pList)
|
---|
239 | {
|
---|
240 | //
|
---|
241 | // loop over all branches which are 'marked' as branches to get written.
|
---|
242 | //
|
---|
243 | MRootFileBranch *entry;
|
---|
244 |
|
---|
245 | TIter Next(&fBranches);
|
---|
246 | while ((entry=(MRootFileBranch*)Next()))
|
---|
247 | {
|
---|
248 | //
|
---|
249 | // Get the pointer to the container. If the pointer is NULL it seems,
|
---|
250 | // that the user identified the container by name.
|
---|
251 | //
|
---|
252 | MParContainer *cont = entry->GetContainer();
|
---|
253 | if (!cont)
|
---|
254 | {
|
---|
255 | //
|
---|
256 | // Get the name and try to find a container with this name
|
---|
257 | // in the parameter list.
|
---|
258 | //
|
---|
259 | const char *cname = entry->GetContName();
|
---|
260 | cont = (MParContainer*)pList->FindObject(cname);
|
---|
261 | if (!cont)
|
---|
262 | {
|
---|
263 | //
|
---|
264 | // No corresponding container is found
|
---|
265 | //
|
---|
266 | *fLog << dbginf << "Cannot find parameter container '" << cname << "'." << endl;
|
---|
267 | return kFALSE;
|
---|
268 | }
|
---|
269 | //
|
---|
270 | // The container is found. Put the pointer into the entry.
|
---|
271 | //
|
---|
272 | entry->SetContainer(cont);
|
---|
273 | }
|
---|
274 |
|
---|
275 | //
|
---|
276 | // Get container name, tree name and tree title of this entry.
|
---|
277 | //
|
---|
278 | const char *cname = cont->GetName();
|
---|
279 | const char *tname = entry->GetName();
|
---|
280 | const char *ttitle = entry->GetTitle();
|
---|
281 |
|
---|
282 | //
|
---|
283 | // if the tree name is NULL this idetifies it to use the default:
|
---|
284 | // the container name.
|
---|
285 | //
|
---|
286 | if (tname[0] == '\0')
|
---|
287 | tname = cname;
|
---|
288 |
|
---|
289 | //
|
---|
290 | // Check if the tree is already existing (part of the file)
|
---|
291 | //
|
---|
292 | TTree *tree = (TTree*)fOut->Get(tname);
|
---|
293 | if (!tree)
|
---|
294 | {
|
---|
295 | //
|
---|
296 | // if the tree doesn't exist create a new tree. Use the tree
|
---|
297 | // name as title if title is NULL.
|
---|
298 | // And add the tree to the list of trees
|
---|
299 | //
|
---|
300 | TDirectory *save = gDirectory;
|
---|
301 | fOut->cd();
|
---|
302 |
|
---|
303 | tree = new TTree(tname, ttitle ? ttitle : tname);
|
---|
304 | fTrees.AddLast(tree);
|
---|
305 |
|
---|
306 | //
|
---|
307 | // If the tree does not already exist in the file mark this
|
---|
308 | // tree as a branch created by MWriteRootFile
|
---|
309 | //
|
---|
310 | tree->SetBit(kIsNewTree);
|
---|
311 |
|
---|
312 | gDirectory = save;
|
---|
313 |
|
---|
314 | *fLog << "Created Tree " << tname << "." << endl;
|
---|
315 | }
|
---|
316 |
|
---|
317 | //
|
---|
318 | // In case the file is opened as 'UPDATE' the tree may still not
|
---|
319 | // be in the list. Because it neither was created previously,
|
---|
320 | // nor this time, so the corresponding entries is marked as a
|
---|
321 | // single branch to be filled. --> Add it to the list of trees.
|
---|
322 | //
|
---|
323 | if (!fTrees.FindObject(tree))
|
---|
324 | fTrees.AddLast(tree);
|
---|
325 |
|
---|
326 | //
|
---|
327 | // Now we have a valid tree. Search the list of trees for this tree
|
---|
328 | // Add a pointer to the entry in the tree list to this branch-entry
|
---|
329 | //
|
---|
330 | entry->SetTree(tree);
|
---|
331 |
|
---|
332 | //
|
---|
333 | // Try to get the branch from the file.
|
---|
334 | // If the branch already exists the user specified one branch twice.
|
---|
335 | //
|
---|
336 | TBranch *branch = tree->GetBranch(cname);
|
---|
337 | if (branch)
|
---|
338 | {
|
---|
339 | *fLog << dbginf << "Branch '" << cname << "' is already existing." << endl;
|
---|
340 | return kFALSE;
|
---|
341 | }
|
---|
342 |
|
---|
343 | //
|
---|
344 | // Create a new branch in the actual tree. The branch has the name
|
---|
345 | // container name. The type of the container is given by the
|
---|
346 | // ClassName entry in the container. The Address is the address of a
|
---|
347 | // pointer to the container (gotten from the branch entry). As
|
---|
348 | // Basket size we specify a (more or less) common default value.
|
---|
349 | // The containers should be written in Splitlevel=1
|
---|
350 | //
|
---|
351 | TString branchname(cname);
|
---|
352 | branchname.Append(".");
|
---|
353 | branch = tree->Branch(branchname, cont->ClassName(), entry->GetAddress());
|
---|
354 |
|
---|
355 | //
|
---|
356 | // If the branch couldn't be created we have a problem.
|
---|
357 | //
|
---|
358 | if (!branch)
|
---|
359 | {
|
---|
360 | *fLog << dbginf << "Unable to create branch '" << cname << "'." << endl;
|
---|
361 | return kFALSE;
|
---|
362 | }
|
---|
363 |
|
---|
364 | //
|
---|
365 | // Tell the entry also which branch belongs to it (this is necessary
|
---|
366 | // for branches belonging to already existing tree, UPDATE-mode)
|
---|
367 | //
|
---|
368 | entry->SetBranch(branch);
|
---|
369 | *fLog << "Created Branch " << cname << " of " << cont->ClassName() << "." << endl;
|
---|
370 | }
|
---|
371 | return kTRUE;
|
---|
372 | }
|
---|
373 |
|
---|
374 | // --------------------------------------------------------------------------
|
---|
375 | //
|
---|
376 | // Checks all given containers (branch entries) for the write flag.
|
---|
377 | // If the write flag is set the corresponding Tree is marked to get filled.
|
---|
378 | // All Trees which are marked to be filled are filled with all their
|
---|
379 | // branches.
|
---|
380 | // In case of a file opened in 'UPDATE' mode, single branches can be
|
---|
381 | // filled, too. WARNING - for the moment there is no check whether
|
---|
382 | // you filled the correct number of events into the branch, so that
|
---|
383 | // each of the other branches in the tree has the correct corresponding
|
---|
384 | // number of new entries in the new branch!
|
---|
385 | // Be carefull: If only one container (corresponding to a branch) of a tree
|
---|
386 | // has the write flag, all containers in this tree are filled!
|
---|
387 | //
|
---|
388 | void MWriteRootFile::CheckAndWrite() const
|
---|
389 | {
|
---|
390 | TObject *obj;
|
---|
391 |
|
---|
392 | //
|
---|
393 | // Loop over all branch entries
|
---|
394 | //
|
---|
395 | TIter NextBranch(&fBranches);
|
---|
396 | while ((obj=NextBranch()))
|
---|
397 | {
|
---|
398 | MRootFileBranch *b = (MRootFileBranch*)obj;
|
---|
399 |
|
---|
400 | //
|
---|
401 | // Check for the Write flag
|
---|
402 | //
|
---|
403 | if (!b->GetContainer()->IsReadyToSave())
|
---|
404 | continue;
|
---|
405 |
|
---|
406 | //
|
---|
407 | // If the write flag of the branch entry is set, set the write flag of
|
---|
408 | // the corresponding tree entry.
|
---|
409 | //
|
---|
410 | if (b->GetTree()->TestBit(kIsNewTree))
|
---|
411 | b->GetTree()->SetBit(kFillTree);
|
---|
412 | else
|
---|
413 | b->GetBranch()->Fill();
|
---|
414 | }
|
---|
415 |
|
---|
416 | //
|
---|
417 | // Loop over all tree entries
|
---|
418 | //
|
---|
419 | TIter NextTree(&fTrees);
|
---|
420 | while ((obj=NextTree()))
|
---|
421 | {
|
---|
422 | TTree *t = (TTree*)obj;
|
---|
423 |
|
---|
424 | //
|
---|
425 | // Check the write flag of the tree
|
---|
426 | //
|
---|
427 | if (!t->TestBit(kFillTree))
|
---|
428 | continue;
|
---|
429 |
|
---|
430 | //
|
---|
431 | // If the write flag is set, fill the tree (with the corresponding
|
---|
432 | // branches/containers), delete the write flag and increase the number
|
---|
433 | // of written/filled entries.
|
---|
434 | //
|
---|
435 | t->Fill();
|
---|
436 | t->ResetBit(kFillTree);
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | // --------------------------------------------------------------------------
|
---|
441 | //
|
---|
442 | // return open state of the root file.
|
---|
443 | //
|
---|
444 | Bool_t MWriteRootFile::IsFileOpen() const
|
---|
445 | {
|
---|
446 | return fOut->IsOpen();
|
---|
447 | }
|
---|
448 |
|
---|
449 | // --------------------------------------------------------------------------
|
---|
450 | //
|
---|
451 | // return name of the root-file
|
---|
452 | //
|
---|
453 | const char *MWriteRootFile::GetFileName() const
|
---|
454 | {
|
---|
455 | return fOut->GetName();
|
---|
456 | }
|
---|
457 |
|
---|
458 | // --------------------------------------------------------------------------
|
---|
459 | //
|
---|
460 | // Implementation of SavePrimitive. Used to write the call to a constructor
|
---|
461 | // to a macro. In the original root implementation it is used to write
|
---|
462 | // gui elements to a macro-file.
|
---|
463 | //
|
---|
464 | void MWriteRootFile::StreamPrimitive(ofstream &out) const
|
---|
465 | {
|
---|
466 | out << " MWriteRootFile " << GetUniqueName() << "(\"";
|
---|
467 | out << fOut->GetName() << "\", \"";
|
---|
468 | out << fOut->GetOption() << "\", \"";
|
---|
469 | out << fOut->GetTitle() << "\", ";
|
---|
470 | out << fOut->GetCompressionLevel();
|
---|
471 |
|
---|
472 | if (fName!=gsDefName || fTitle!=gsDefTitle)
|
---|
473 | {
|
---|
474 | out << ", \"" << fName << "\"";
|
---|
475 | if (fTitle!=gsDefTitle)
|
---|
476 | out << ", \"" << fTitle << "\"";
|
---|
477 | }
|
---|
478 | out << ");" << endl;
|
---|
479 |
|
---|
480 |
|
---|
481 | MRootFileBranch *entry;
|
---|
482 | TIter Next(&fBranches);
|
---|
483 | while ((entry=(MRootFileBranch*)Next()))
|
---|
484 | {
|
---|
485 | out << " " << GetUniqueName() << ".AddContainer(";
|
---|
486 |
|
---|
487 | if (entry->GetContainer())
|
---|
488 | {
|
---|
489 | entry->GetContainer()->SavePrimitive(out);
|
---|
490 | out << "&" << entry->GetContainer()->GetUniqueName();
|
---|
491 | }
|
---|
492 | else
|
---|
493 | out << "\"" << entry->GetContName() << "\"";
|
---|
494 |
|
---|
495 | out << ", \"" << entry->GetName() << "\"";
|
---|
496 | if ((TString)entry->GetTitle()!="")
|
---|
497 | out << ", \"" << entry->GetTitle() << "\"";
|
---|
498 |
|
---|
499 | out <<");" << endl;
|
---|
500 | }
|
---|
501 | }
|
---|