source: tags/Mars-V0.7/mbase/MWriteRootFile.cc

Last change on this file was 1116, checked in by tbretz, 23 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 12.5 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@uni-sw.gwdg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2001
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
37#include "MWriteRootFile.h"
38
39#include <TFile.h>
40#include <TTree.h>
41
42#include "MLog.h"
43#include "MLogManip.h"
44
45#include "MParList.h"
46
47ClassImp(MRootFileBranch);
48ClassImp(MWriteRootFile);
49
50// --------------------------------------------------------------------------
51//
52// Specify the name of the root file. You can also give an option ("UPDATE"
53// and "RECREATE" would make sense only) as well as the file title and
54// compression factor. To a more detaild description of the options see
55// TFile.
56//
57MWriteRootFile::MWriteRootFile(const char *fname,
58 const Option_t *opt,
59 const char *ftitle,
60 const Int_t comp,
61 const char *name,
62 const char *title)
63{
64 fName = name ? name : "MWriteRootFile";
65 fTitle = title ? title : "Task which writes a root-output file";
66
67 //
68 // Set the Arrays the owner of its entries. This means, that the
69 // destructor of the arrays will delete all its entries.
70 //
71 fBranches.SetOwner();
72
73 //
74 // Believing the root user guide, TTree instanced are owned by the
75 // directory (file) in which they are. This means we don't have to
76 // care about their destruction.
77 //
78 //fTrees.SetOwner();
79
80 //
81 // Open the rootfile
82 //
83 fOut = new TFile(fname, opt, ftitle, comp);
84}
85
86// --------------------------------------------------------------------------
87//
88// Prints some statistics about the file to the screen. And closes the file
89// properly.
90//
91MWriteRootFile::~MWriteRootFile()
92{
93 //
94 // Print some statistics to the looging out.
95 //
96 Print();
97
98 //
99 // If the file is still open (no error) write the keys. This is necessary
100 // for appearance of the all trees and branches.
101 //
102 if (IsFileOpen())
103 fOut->Write();
104
105 //
106 // Delete the file. This'll also close the file (if open)
107 //
108 delete fOut;
109
110 //
111 // Remark:
112 // - Trees are automatically deleted by the the file
113 // (unless file.SetDirectory(0) was called)
114 // - Branches are automatically deleted by the tree destructor
115 //
116}
117
118// --------------------------------------------------------------------------
119//
120// Prints all trees with the actually number of written entries to log-out.
121//
122void MWriteRootFile::Print(Option_t *) const
123{
124 *fLog << all << " File: " << GetFileName() << endl;
125 *fLog << setfill('-') << setw(strlen(GetFileName())+8) << "" << endl;
126 *fLog << setfill(' '); // FIXME: not resetting setfill results in strange output???
127
128 TTree *t = NULL;
129 TIter Next(&fTrees);
130 while ((t=(TTree*)Next()))
131 *fLog << t->GetName() << ": \t" << t->GetEntries() << " entries." << endl;
132}
133
134// --------------------------------------------------------------------------
135//
136// Add a new Container to list of containers which should be written to the
137// file. Give the name of the container which will identify the container
138// in the parameterlist. tname is the name of the tree to which the
139// container should be written (Remark: one tree can hold more than one
140// container). The default is the same name as the container name.
141// You can slso specify a title for the tree. This is only
142// used the first time this tree in 'mentioned'. As default the title
143// is the name of the tree.
144//
145void MWriteRootFile::AddContainer(const char *cname, const char *tname, const char *ttitle)
146{
147 //
148 // create a new entry in the list of branches to write and
149 // add the entry to the list.
150 //
151 MRootFileBranch *entry = new MRootFileBranch(cname, tname, ttitle);
152 fBranches.AddLast(entry);
153}
154
155// --------------------------------------------------------------------------
156//
157// Add a new Container to list of containers which should be written to the
158// file. Give the pointer to the container. tname is the name of the tree to
159// which the container should be written (Remark: one tree can hold more than
160// one container). The default is the same name as the container name.
161// You can slso specify a title for the tree. This is only
162// used the first time this tree in 'mentioned'. As default the title
163// is the name of the tree.
164//
165void MWriteRootFile::AddContainer(MParContainer *cont, const char *tname,
166 const char *ttitle)
167{
168 //
169 // create a new entry in the list of branches to write and
170 // add the entry to the list.
171 //
172 MRootFileBranch *entry = new MRootFileBranch(cont, tname, ttitle);
173 fBranches.AddLast(entry);
174}
175
176// --------------------------------------------------------------------------
177//
178// Add a new Container to list of containers which should be written to the
179// file. Give the pointer to the container. tname is the name of the tree to
180// which the container should be written (Remark: one tree can hold more than
181// one container). The default is the same name as the container name.
182// You can slso specify a title for the tree. This is only
183// used the first time this tree in 'mentioned'. As default the title
184// is the name of the tree.
185//
186Bool_t MWriteRootFile::GetContainer(MParList *pList)
187{
188 MRootFileBranch *entry;
189
190 //
191 // loop over all branches which are 'marked' as branches to get written.
192 //
193 TIter Next(&fBranches);
194 while ((entry=(MRootFileBranch*)Next()))
195 {
196 //
197 // Get the pointer to the container. If the pointer is NULL it seems,
198 // that the user identified the container by name.
199 //
200 MParContainer *cont = entry->GetContainer();
201 if (!cont)
202 {
203 //
204 // Get the name and try to find a container with this name
205 // in the parameter list.
206 //
207 const char *cname = entry->GetContName();
208 cont = (MParContainer*)pList->FindObject(cname);
209 if (!cont)
210 {
211 //
212 // No corresponding container is found
213 //
214 *fLog << dbginf << "Cannot find parameter container '" << cname << "'." << endl;
215 return kFALSE;
216 }
217 //
218 // The container is found. Put the pointer into the entry.
219 //
220 entry->SetContainer(cont);
221 }
222
223 //
224 // Get container name, tree name and tree title of this entry.
225 //
226 const char *cname = cont->GetName();
227 const char *tname = entry->GetName();
228 const char *ttitle = entry->GetTitle();
229
230 //
231 // if the tree name is NULL this idetifies it to use the default:
232 // the container name.
233 //
234 if (tname[0] == '\0')
235 tname = cname;
236
237 //
238 // Check if the tree is already existing (part of the file)
239 //
240 TTree *tree = (TTree*)fOut->Get(tname);
241 if (!tree)
242 {
243 //
244 // if the tree doesn't exist create a new tree. Use the tree
245 // name as title if title is NULL.
246 // And add the tree to the list of trees
247 //
248 tree = new TTree(tname, ttitle ? ttitle : tname);
249 fTrees.AddLast(tree);
250
251 *fLog << "Created Tree " << tname << "." << endl;
252 }
253
254 //
255 // Now we have a valid tree. Search the list of trees for this tree
256 // (either it is already there, or we created and add it previously)
257 // Add a pointer to the entry in the tree list to this branch-entry
258 //
259 TObject *obj;
260 TIter NextTree(&fTrees);
261 while ((obj=NextTree()))
262 {
263 if (obj == tree)
264 entry->SetTree((TTree*)obj);
265 }
266
267 //
268 // Try to get the branch from the file.
269 // If the branch already exists the user specified one branch twice.
270 //
271 TBranch *branch = tree->GetBranch(cname);
272 if (branch)
273 {
274 *fLog << dbginf << "Branch '" << cname << "' is already existing." << endl;
275 return kFALSE;
276 }
277
278 //
279 // Create a new branch in the actual tree. The branch has the name
280 // container name. The type of the container is given by the
281 // ClassName entry in the container. The Address is the address of a
282 // pointer to the container (gotten from the branch entry). As
283 // Basket size we specify a (more or less) common default value.
284 // The containers should be written in Splitlevel=1
285 //
286 TString branchname(cname);
287 branchname.Append(".");
288 branch = tree->Branch(branchname, cont->ClassName(), entry->GetAddress());
289
290 *fLog << "Created Branch " << cname << " of " << cont->ClassName() << "." << endl;
291
292 //
293 // If the branch couldn't be created we have a problem.
294 //
295 if (!branch)
296 {
297 *fLog << dbginf << "Unable to create branch '" << cname << "'." << endl;
298 return kFALSE;
299 }
300 }
301 return kTRUE;
302}
303
304// --------------------------------------------------------------------------
305//
306// Checks all given containers (branch entries) for the write flag.
307// If the write flag is set the corresponding Tree is marked to get filled.
308// All Trees which are marked to be filled are filled with the corresponding
309// branches.
310// Be carefull: If only one container (corresponding to a branch) of a tree
311// has the write flag, all containers in this tree are filled!
312//
313void MWriteRootFile::CheckAndWrite() const
314{
315 const Int_t kFillTree = BIT(14);
316
317 TObject *obj;
318
319 //
320 // Loop over all branch entries
321 //
322 TIter NextBranch(&fBranches);
323 while ((obj=NextBranch()))
324 {
325 MRootFileBranch *b = (MRootFileBranch*)obj;
326
327 //
328 // Check for the Write flag
329 //
330 if (!b->GetContainer()->IsReadyToSave())
331 continue;
332
333 //
334 // If the write flag of the branch entry is set, set the write flag of
335 // the corresponding tree entry.
336 //
337 b->GetTree()->SetBit(kFillTree);
338 }
339
340 //
341 // Loop over all tree entries
342 //
343 TIter NextTree(&fTrees);
344 while ((obj=NextTree()))
345 {
346 TTree *t = (TTree*)obj;
347
348 //
349 // Check the write flag of the tree
350 //
351 if (!t->TestBit(kFillTree))
352 continue;
353
354 //
355 // If the write flag is set, fill the tree (with the corresponding
356 // branches/containers), delete the write flag and increase the number
357 // of written/filled entries.
358 //
359 t->Fill();
360 t->ResetBit(kFillTree);
361 }
362}
363
364// --------------------------------------------------------------------------
365//
366// return open state of the root file.
367//
368Bool_t MWriteRootFile::IsFileOpen() const
369{
370 return fOut->IsOpen();
371}
372
373// --------------------------------------------------------------------------
374//
375// return name of the root-file
376//
377const char *MWriteRootFile::GetFileName() const
378{
379 return fOut->GetName();
380}
381
Note: See TracBrowser for help on using the repository browser.