source: trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc@ 2772

Last change on this file since 2772 was 2604, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 16.2 KB
Line 
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
48ClassImp(MRootFileBranch);
49ClassImp(MWriteRootFile);
50
51using namespace std;
52
53static const TString gsDefName = "MWriteRootFile";
54static 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//
61MWriteRootFile::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//
76MWriteRootFile::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//
114MWriteRootFile::~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//
147void 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() || 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//
194void MWriteRootFile::AddContainer(const char *cname, const char *tname, Bool_t must)
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, must);
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//
217void MWriteRootFile::AddContainer(MParContainer *cont, const char *tname,
218 Bool_t must)
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, must);
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//
238Bool_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 if (entry->MustHave())
267 {
268 *fLog << err << "Cannot find parameter container '" << cname << "'." << endl;
269 return kFALSE;
270 }
271
272 *fLog << inf << "Unnecessary parameter container '" << cname << "' not found..." << endl;
273 delete fBranches.Remove(entry);
274 continue;
275 }
276
277 //
278 // The container is found. Put the pointer into the entry.
279 //
280 entry->SetContainer(cont);
281 }
282
283 //
284 // Get container name, tree name and tree title of this entry.
285 //
286 const char *cname = cont->GetName();
287 const char *tname = entry->GetName();
288 const TString ttitle(Form("Tree containing %s", cont->GetDescriptor()));
289
290 //
291 // if the tree name is NULL this idetifies it to use the default:
292 // the container name.
293 //
294 if (tname[0] == '\0')
295 tname = cname;
296
297 //
298 // Check if the tree is already existing (part of the file)
299 //
300 TTree *tree = (TTree*)fOut->Get(tname);
301 if (!tree)
302 {
303 //
304 // if the tree doesn't exist create a new tree. Use the tree
305 // name as title if title is NULL.
306 // And add the tree to the list of trees
307 //
308 TDirectory *save = gDirectory;
309 fOut->cd();
310
311 tree = new TTree(tname, ttitle);
312 fTrees.AddLast(tree);
313
314 //
315 // If the tree does not already exist in the file mark this
316 // tree as a branch created by MWriteRootFile
317 //
318 tree->SetBit(kIsNewTree);
319
320 gDirectory = save;
321
322 *fLog << inf << "Tree " << tname << " created." << endl;
323 }
324
325 //
326 // In case the file is opened as 'UPDATE' the tree may still not
327 // be in the list. Because it neither was created previously,
328 // nor this time, so the corresponding entries is marked as a
329 // single branch to be filled. --> Add it to the list of trees.
330 //
331 if (!fTrees.FindObject(tree))
332 fTrees.AddLast(tree);
333
334 //
335 // Now we have a valid tree. Search the list of trees for this tree
336 // Add a pointer to the entry in the tree list to this branch-entry
337 //
338 entry->SetTree(tree);
339
340 //
341 // Try to get the branch from the file.
342 // If the branch already exists the user specified one branch twice.
343 //
344 TBranch *branch = tree->GetBranch(cname);
345 if (branch)
346 {
347 *fLog << err << "Branch '" << cname << "' already existing." << endl;
348 return kFALSE;
349 }
350
351 //
352 // Create a new branch in the actual tree. The branch has the name
353 // container name. The type of the container is given by the
354 // ClassName entry in the container. The Address is the address of a
355 // pointer to the container (gotten from the branch entry). As
356 // Basket size we specify a (more or less) common default value.
357 // The containers should be written in Splitlevel=1
358 //
359 TString branchname(cname);
360 branchname.Append(".");
361 branch = tree->Branch(branchname, cont->ClassName(), entry->GetAddress());
362
363 //
364 // If the branch couldn't be created we have a problem.
365 //
366 if (!branch)
367 {
368 *fLog << err << "Unable to create branch '" << cname << "'." << endl;
369 return kFALSE;
370 }
371
372 //
373 // Tell the entry also which branch belongs to it (this is necessary
374 // for branches belonging to already existing tree, UPDATE-mode)
375 //
376 entry->SetBranch(branch);
377 *fLog << inf << "Created Branch " << cname << " of " << cont->ClassName() << "." << endl;
378 }
379 return kTRUE;
380}
381
382// --------------------------------------------------------------------------
383//
384// Checks all given containers (branch entries) for the write flag.
385// If the write flag is set the corresponding Tree is marked to get filled.
386// All Trees which are marked to be filled are filled with all their
387// branches.
388// In case of a file opened in 'UPDATE' mode, single branches can be
389// filled, too. WARNING - for the moment there is no check whether
390// you filled the correct number of events into the branch, so that
391// each of the other branches in the tree has the correct corresponding
392// number of new entries in the new branch!
393// Be carefull: If only one container (corresponding to a branch) of a tree
394// has the write flag, all containers in this tree are filled!
395//
396void MWriteRootFile::CheckAndWrite() const
397{
398 TObject *obj;
399
400 //
401 // Loop over all branch entries
402 //
403 TIter NextBranch(&fBranches);
404 while ((obj=NextBranch()))
405 {
406 MRootFileBranch *b = (MRootFileBranch*)obj;
407
408 //
409 // Check for the Write flag
410 //
411 if (!b->GetContainer()->IsReadyToSave())
412 continue;
413
414 //
415 // If the write flag of the branch entry is set, set the write flag of
416 // the corresponding tree entry.
417 //
418 if (b->GetTree()->TestBit(kIsNewTree))
419 b->GetTree()->SetBit(kFillTree);
420 else
421 b->GetBranch()->Fill();
422 }
423
424 //
425 // Loop over all tree entries
426 //
427 TIter NextTree(&fTrees);
428 while ((obj=NextTree()))
429 {
430 TTree *t = (TTree*)obj;
431
432 //
433 // Check the write flag of the tree
434 //
435 if (!t->TestBit(kFillTree))
436 continue;
437
438 //
439 // If the write flag is set, fill the tree (with the corresponding
440 // branches/containers), delete the write flag and increase the number
441 // of written/filled entries.
442 //
443 t->Fill();
444 t->ResetBit(kFillTree);
445 }
446}
447
448// --------------------------------------------------------------------------
449//
450// return open state of the root file.
451//
452Bool_t MWriteRootFile::IsFileOpen() const
453{
454 return fOut->IsOpen();
455}
456
457// --------------------------------------------------------------------------
458//
459// return name of the root-file
460//
461const char *MWriteRootFile::GetFileName() const
462{
463 return fOut->GetName();
464}
465
466// --------------------------------------------------------------------------
467//
468// Implementation of SavePrimitive. Used to write the call to a constructor
469// to a macro. In the original root implementation it is used to write
470// gui elements to a macro-file.
471//
472void MWriteRootFile::StreamPrimitive(ofstream &out) const
473{
474 out << " MWriteRootFile " << GetUniqueName() << "(\"";
475 out << fOut->GetName() << "\", \"";
476 out << fOut->GetOption() << "\", \"";
477 out << fOut->GetTitle() << "\", ";
478 out << fOut->GetCompressionLevel();
479
480 if (fName!=gsDefName || fTitle!=gsDefTitle)
481 {
482 out << ", \"" << fName << "\"";
483 if (fTitle!=gsDefTitle)
484 out << ", \"" << fTitle << "\"";
485 }
486 out << ");" << endl;
487
488
489 MRootFileBranch *entry;
490 TIter Next(&fBranches);
491 while ((entry=(MRootFileBranch*)Next()))
492 {
493 out << " " << GetUniqueName() << ".AddContainer(";
494
495 if (entry->GetContainer())
496 {
497 entry->GetContainer()->SavePrimitive(out);
498 out << "&" << entry->GetContainer()->GetUniqueName();
499 }
500 else
501 out << "\"" << entry->GetContName() << "\"";
502
503 out << ", \"" << entry->GetName() << "\"";
504 if (!entry->MustHave())
505 out << ", kFALSE";
506
507 out <<");" << endl;
508 }
509}
Note: See TracBrowser for help on using the repository browser.