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

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