source: trunk/MagicSoft/Mars/mbase/MWriteRootFile.cc@ 1048

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