source: trunk/Mars/mfileio/MWriteRootFile.cc@ 17033

Last change on this file since 17033 was 14029, checked in by tbretz, 12 years ago
Fixed a problem with root 5.32 and no-tree tasks (like in WriteMC)
File size: 37.3 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: Software Development, 2000-2009
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: Look at the Warning in MTaskList.
34//
35// There is a special mode of operation which opens a new file for each new
36// file read by the reading task (opening the new file is initiated by
37// ReInit()) For more details see the corresponding constructor.
38//
39// Memory based trees
40// ------------------
41// It is possible to store the data into memory (TTrees) instead of
42// writing the data into a file. To do this either call the default
43// constructor or specify 'memory' as option in the constructor.
44//
45// Afterwards the tree can be found using
46// gROOT->GetListOfFiles()->FindObject("treename")
47//
48// Currently(!) the tree is not deleted at all! Please make sure to
49// delete it if it is not used anymore - otherwise you could wast a LOT
50// of memory. Please consider that this behaviour might change in the
51// future.
52//
53// Such trees are usefull if you want to use more basic root-tools
54// like TMultiLayerPerceptron or TEventList.
55//
56// If you want to process such memory based Trees using Mars make sure,
57// that you don't need data from the RunHeader tree because you can
58// only use MReadTree but not MReadMarsFile with such a tree.
59//
60/////////////////////////////////////////////////////////////////////////////
61#include "MWriteRootFile.h"
62
63#include <fstream>
64
65#include <TFile.h>
66#include <TTree.h>
67#include <TPRegexp.h>
68
69#include "MLog.h"
70#include "MLogManip.h"
71
72#include "MRead.h"
73#include "MParList.h"
74#include "MStatusDisplay.h"
75
76ClassImp(MRootFileBranch);
77ClassImp(MWriteRootFile);
78
79using namespace std;
80
81const TString MWriteRootFile::gsDefName = "MWriteRootFile";
82const TString MWriteRootFile::gsDefTitle = "Task which writes a root-output file";
83
84void MWriteRootFile::Init(const char *name, const char *title)
85{
86 fName = name ? name : gsDefName.Data();
87 fTitle = title ? title : gsDefTitle.Data();
88
89 //
90 // Set the Arrays the owner of its entries. This means, that the
91 // destructor of the arrays will delete all its entries.
92 //
93 fBranches.SetOwner();
94 fCopies.SetOwner();
95
96 //
97 // Believing the root user guide, TTree instances are owned by the
98 // directory (file) in which they are. This means we don't have to
99 // care about their destruction.
100 //
101 //fTrees.SetOwner();
102
103 gROOT->GetListOfCleanups()->Add(this); // To remove fOut if deleted
104 SetBit(kMustCleanup);
105}
106
107// --------------------------------------------------------------------------
108//
109// Try to get the file from gROOT->GetListOfFiles. (In case the file name
110// is /dev/null we look for a file with name /dev/null and the given title)
111// If it is found fOut is set to it and returned.
112// Otherwise a new file is opened and returned.
113//
114TFile *MWriteRootFile::OpenFile(const char *name, Option_t *option, const char *title, Int_t comp)
115{
116 TFile *file = 0;
117
118 if (TString(name)=="/dev/null")
119 {
120 TIter Next(gROOT->GetListOfFiles());
121 TObject *obj = 0;
122 while ((obj=Next()))
123 if (TString(obj->GetName())=="/dev/null" && TString(obj->GetTitle())==title)
124 {
125 *fLog << inf2 << "Found open file '/dev/null' <Title=" << title << ">... re-using." << endl;
126 file = dynamic_cast<TFile*>(obj);
127 break;
128 }
129 }
130 else
131 {
132 file = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(name));
133
134 // If the file was not found with its name try its expanded name
135 if (!file)
136 {
137 TString fqp(name);
138 gSystem->ExpandPathName(fqp);
139 file = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(fqp));
140 }
141
142 if (file)
143 {
144 *fLog << inf2;
145 *fLog << "Found open file '" << name << "'... re-using." << endl;
146 *fLog << inf3;
147 *fLog << "Make sure that you do NOT write to trees which are" << endl;
148 *fLog << "scheduled already by a different MWriteRootFile..." << endl;
149 }
150 }
151
152 if (!file)
153 {
154 file = new TFile(name, option, title, comp);
155 if (!file->IsOpen())
156 {
157 delete file;
158 return NULL;
159 }
160
161 *fLog << inf3 << "File '" << name << "' ";
162 if (!TString(title).IsNull())
163 *fLog << "<Title=" << title << "> ";
164 *fLog << "opened [" << option << "]" << endl;
165
166 file->SetOption(option); // IMPORTANT!
167 file->SetBit(kMustCleanup);
168 ResetBit(kIsNotOwner);
169 return file;
170 }
171
172 fOut = file;
173 fOut->SetBit(kMustCleanup);
174 SetBit(kIsNotOwner);
175
176 return fOut;
177}
178
179// --------------------------------------------------------------------------
180//
181// Default constructor. It is there to support some root stuff.
182// Don't use it.
183//
184MWriteRootFile::MWriteRootFile() : fOut(NULL)
185{
186 Init();
187}
188
189// --------------------------------------------------------------------------
190//
191// Use this constructor to run in a special mode.
192//
193// In this mode for each input file a new output file is written. This
194// happens in ReInit.
195//
196// comp: Compression Level (see TFile, TBranch)
197// rule: Rule to create output file name (see SubstituteName())
198// overwrite: Allow newly created file to overwrite old files ("RECREATE")
199// ftitle: File title stored in the file (see TFile)
200// name, title: Name and title of this object
201//
202// Until the first file is opened a dummy file with name /dev/null is
203// opened to allow creation of trees and branches in the file.
204// To distinguish between different /dev/null-files the given title is used.
205//
206MWriteRootFile::MWriteRootFile(const Int_t comp,
207 const char *rule,
208 const Option_t *option,
209 const char *ftitle,
210 const char *name,
211 const char *title) : fSplitRule(rule)
212{
213 Init(name, title);
214
215 //
216 // Open a TFile in dummy mode! This is necessary to be able to create
217 // the trees and branches, which are then (in ReInit) moved to
218 // a valid file. (Stupid workaround - but does a good job)
219 //
220 fOut = OpenFile("/dev/null", option, ftitle, comp);
221}
222
223// --------------------------------------------------------------------------
224//
225// Specify the name of the root file. You can also give an option ("UPDATE"
226// and "RECREATE" would make sense only) as well as the file title and
227// compression factor. To a more detaild description of the options see
228// TFile.
229//
230// To create a memory based TTree use
231// fname = name of TTree
232// option = "memory"
233// Make sure you do not read from a tree with the same name!
234//
235MWriteRootFile::MWriteRootFile(const char *fname,
236 const Option_t *option,
237 const char *ftitle,
238 const Int_t comp,
239 const char *name,
240 const char *title) : fOut(NULL)
241{
242 Init(name, title);
243
244 TString opt(option);
245 opt.ToLower();
246
247 //
248 // Check if we are writing to memory
249 //
250 if (opt=="memory")
251 {
252 fSplitRule = fname;
253 return;
254 }
255
256 //
257 // If no name is given we open the TFile in some kind of dummy mode...
258 //
259 TString str(fname);
260 if (str.IsNull())
261 {
262 fOut = new TFile("/dev/null", "READ", ftitle, comp);
263 fOut->SetBit(kMustCleanup);
264 return;
265 }
266
267 if (!str.EndsWith(".root", TString::kIgnoreCase))
268 str += ".root";
269
270 //
271 // Open the rootfile
272 //
273 fOut = OpenFile(str, opt, ftitle, comp);
274}
275
276// --------------------------------------------------------------------------
277//
278// Prints some statistics about the file to the screen. And closes the file
279// properly.
280//
281void MWriteRootFile::Close()
282{
283 //
284 // Print some statistics to the looging out.
285 //
286 if (fOut && !TestBit(kIsNotOwner))
287 {
288 Print();
289
290 //
291 // If the file is still open (no error) write the keys. This is necessary
292 // for appearance of the all trees and branches.
293 //
294 if (IsFileOpen())
295 fOut->Write();
296
297 //
298 // Delete the file. This'll also close the file (if open)
299 //
300 *fLog << inf3 << "Closing file " << fOut->GetName() << "." << endl;
301 delete fOut;
302
303 //
304 // Remark:
305 // - Trees are automatically deleted by the the file
306 // (unless file.SetDirectory(0) was called)
307 // - Branches are automatically deleted by the tree destructor
308 //
309 }
310
311 fOut = 0;
312}
313
314// --------------------------------------------------------------------------
315//
316// call Close()
317//
318MWriteRootFile::~MWriteRootFile()
319{
320 Close();
321}
322
323// --------------------------------------------------------------------------
324//
325// Prints all trees with the actually number of written entries to log-out.
326//
327void MWriteRootFile::Print(Option_t *) const
328{
329 if (!fOut)
330 return;
331
332 *fLog << all << underline << "File: " << GetFileName() << dec << endl;
333
334 Bool_t cont = kFALSE;
335
336 TObject *obj;
337 TIter NextBranch(&fBranches);
338 while ((obj=NextBranch()))
339 {
340 MRootFileBranch *b = (MRootFileBranch*)obj;
341
342 if (!b->GetTree() || b->GetTree()->TestBit(kIsNewTree))
343 continue;
344
345 TBranch *branch = b->GetBranch();
346
347 TString name = b->GetTree()->GetName();
348 name += '.';
349 name += branch->GetName();
350
351 *fLog << " + " << name.Strip(TString::kTrailing, '.') << ": \t" << (ULong_t)branch->GetEntries() << " entries." << endl;
352 cont = kTRUE;
353 }
354
355 TTree *t = NULL;
356 TIter NextTree(&fTrees);
357 while ((t=(TTree*)NextTree()))
358 if (t->TestBit(kIsNewTree))
359 {
360 *fLog << " + " << t->GetName() << ": \t" << (ULong_t)t->GetEntries() << " entries." << endl;
361 cont = kTRUE;
362 }
363
364 TIter NextKey(fOut->GetList());
365
366 while ((obj=NextKey()))
367 {
368 if (!obj->InheritsFrom(TTree::Class()))
369 continue;
370
371 if (fTrees.FindObject(obj) && obj->TestBit(kIsNewTree))
372 continue;
373
374 *fLog << " - " << obj->GetName() << ": \t" << (ULong_t)((TTree*)obj)->GetEntries() << " entries." << endl;
375 cont = kTRUE;
376 }
377
378 if (!cont)
379 *fLog << " No contents." << endl;
380
381 *fLog << endl;
382}
383
384// --------------------------------------------------------------------------
385//
386// Add a new Container to list of containers which should be written to the
387// file. Give the name of the container which will identify the container
388// in the parameterlist. tname is the name of the tree to which the
389// container should be written (Remark: one tree can hold more than one
390// container). The default is the same name as the container name.
391// You can slso specify a title for the tree. This is only
392// used the first time this tree in 'mentioned'. As default the title
393// is the name of the tree.
394//
395void MWriteRootFile::AddContainer(const char *cname, const char *tname, Bool_t must)
396{
397 if (!fOut && !tname)
398 tname = fSplitRule;
399
400 TIter Next(&fBranches);
401 TObject *o=0;
402 while ((o=Next()))
403 if (TString(o->GetName())==TString(tname) && TString(o->GetTitle())==TString(cname))
404 {
405 *fLog << warn;
406 *fLog << "WARNING - Container '" << cname << "' in Tree '" << tname << "' already scheduled... ignored." << endl;
407 return;
408 }
409
410 //
411 // create a new entry in the list of branches to write and
412 // add the entry to the list.
413 //
414 MRootFileBranch *entry = new MRootFileBranch(AddSerialNumber(cname), tname, must);
415 fBranches.AddLast(entry);
416
417 if (tname && tname[0])
418 AddToBranchList(Form("%s.%s", (const char*)AddSerialNumber(cname), tname));
419}
420
421// --------------------------------------------------------------------------
422//
423// Add a new Container to list of containers which should be written to the
424// file. Give the pointer to the container. tname is the name of the tree to
425// which the container should be written (Remark: one tree can hold more than
426// one container). The default is the same name as the container name.
427// You can slso specify a title for the tree. This is only
428// used the first time this tree in 'mentioned'. As default the title
429// is the name of the tree.
430//
431void MWriteRootFile::AddContainer(MParContainer *cont, const char *tname, Bool_t must)
432{
433 if (!fOut && !tname)
434 tname = fSplitRule;
435
436 TIter Next(&fBranches);
437 TObject *o=0;
438 while ((o=Next()))
439 if (TString(o->GetName())==TString(tname) &&
440 static_cast<MRootFileBranch*>(o)->GetContainer()==cont)
441 {
442 *fLog << warn;
443 *fLog << "WARNING - Container " << cont << " in Tree '" << tname << "' already scheduled... ignored." << endl;
444 return;
445 }
446
447 //
448 // create a new entry in the list of branches to write and
449 // add the entry to the list.
450 //
451 MRootFileBranch *entry = new MRootFileBranch(cont, tname, must);
452 fBranches.AddLast(entry);
453}
454
455// --------------------------------------------------------------------------
456//
457// If you want to copy a full tree (or some branches of some trees)
458// completely from one file to another one you can use this
459//
460void MWriteRootFile::AddCopySource(const char *tname, const char *bname, Bool_t force)
461{
462 TObject *obj = new TNamed(tname, bname?bname:"*");
463 if (force)
464 obj->SetBit(kForced);
465
466 fCopies.Add(obj);
467 fCopies.Sort();
468}
469
470// --------------------------------------------------------------------------
471//
472// Add a new Container to list of containers which should be written to the
473// file. Give the pointer to the container. tname is the name of the tree to
474// which the container should be written (Remark: one tree can hold more than
475// one container). The default is the same name as the container name.
476// You can slso specify a title for the tree. This is only
477// used the first time this tree in 'mentioned'. As default the title
478// is the name of the tree.
479//
480Bool_t MWriteRootFile::GetContainer(MParList *pList)
481{
482 //
483 // loop over all branches which are 'marked' as branches to get written.
484 //
485 MRootFileBranch *entry;
486
487 TIter Next(&fBranches);
488 while ((entry=(MRootFileBranch*)Next()))
489 {
490 //
491 // Get the pointer to the container. If the pointer is NULL it seems,
492 // that the user identified the container by name.
493 //
494 MParContainer *cont = entry->GetContainer();
495 if (!cont)
496 {
497 //
498 // Get the name and try to find a container with this name
499 // in the parameter list.
500 //
501 const char *cname = entry->GetContName();
502 cont = (MParContainer*)pList->FindObject(cname);
503 if (!cont)
504 {
505 //
506 // No corresponding container is found
507 //
508 if (entry->MustHave())
509 {
510 *fLog << err << "Cannot find parameter container '" << cname << "'." << endl;
511 return kFALSE;
512 }
513
514 *fLog << inf2 << "Unnecessary parameter container '" << cname << "' not found..." << endl;
515 delete fBranches.Remove(entry);
516 continue;
517 }
518
519 //
520 // The container is found. Put the pointer into the entry.
521 //
522 entry->SetContainer(cont);
523 }
524
525 //
526 // Get container name, tree name and tree title of this entry.
527 //
528 const char *cname = cont->GetName();
529 const char *tname = entry->GetName();
530 const TString ttitle(Form("Tree containing %s", cont->GetDescriptor().Data()));
531
532 //
533 // if the tree name is NULL this idetifies it to use the default:
534 // the container name.
535 //
536 if (tname[0] == '\0')
537 tname = cname;
538
539 //
540 // Check if the tree is already existing (part of the file or memory)
541 //
542 TTree *tree = fOut ? (TTree*)fOut->Get(tname) : dynamic_cast<TTree*>(gROOT->FindObject(tname));
543 if (!fOut && tree)
544 {
545 if (tree->GetCurrentFile())
546 {
547 *fLog << err;
548 *fLog << "ERROR - You are trying to write data into a memory stored root tree," << endl;
549 *fLog << " because you either called the default constructor or have" << endl;
550 *fLog << " instantiated MWriteRootFile using the write option 'memory'." << endl;
551 *fLog << " This tree '" << tname << "' is already existing in" << endl;
552 *fLog << " memory (gROOT->FindObject) and is already belonging to a" << endl;
553 *fLog << " file (" << tree->GetCurrentFile()->GetName() << ")." << endl;
554 *fLog << " This can - for example - happen if you are reading from a" << endl;
555 *fLog << " tree with the same name. The easiest solution in this case" << endl;
556 *fLog << " is to change the name of the tree you want to write to." << endl;
557 *fLog << endl;
558 return kFALSE;
559 }
560 *fLog << inf << "Tree '" << tname << "' found already in Memory... using." << endl;
561 }
562
563 if (!tree)
564 {
565 //
566 // if the tree doesn't exist create a new tree. Use the tree
567 // name as title if title is NULL.
568 // And add the tree to the list of trees
569 //
570 TDirectory *save = gDirectory;
571 if (fOut)
572 fOut->cd();
573 else
574 gROOT->cd();
575
576 tree = new TTree(tname, ttitle, fOut ? 99 : 1);
577 fTrees.AddLast(tree);
578
579 //
580 // If the tree does not already exist in the file mark this
581 // tree as a branch created by MWriteRootFile
582 //
583 tree->SetBit(kIsNewTree);
584
585 *fLog << inf << "Tree " << tname << " created in " << gDirectory->GetName() << endl;
586
587 gDirectory = save;
588 }
589
590 //
591 // In case the file is opened as 'UPDATE' the tree may still not
592 // be in the list. Because it neither was created previously,
593 // nor this time, so the corresponding entries is marked as a
594 // single branch to be filled. --> Add it to the list of trees.
595 //
596 if (!fTrees.FindObject(tree))
597 fTrees.AddLast(tree);
598
599 //
600 // Now we have a valid tree. Search the list of trees for this tree
601 // Add a pointer to the entry in the tree list to this branch-entry
602 //
603 entry->SetTree(tree);
604
605 TString branchname(cname);
606 branchname.Append(".");
607
608 //
609 // Try to get the branch from the file.
610 // If the branch already exists the user specified one branch twice.
611 //
612 TBranch *branch = tree->GetBranch(branchname);
613 if (branch)
614 {
615 *fLog << inf << "Branch '" << cname << "' already existing... updating." << endl;
616 branch->SetAddress(entry->GetAddress());
617
618 if (!fSplitRule.IsNull() && fOut)
619 {
620 *fLog << warn << endl;
621 *fLog << "WARNING: You are updating an existing branch. For this case" << endl;
622 *fLog << " file-splitting mode is not allowed... disabled!" << endl;
623 *fLog << endl;
624 fSplitRule = "";
625 }
626 }
627 else
628 {
629 //
630 // Create a new branch in the actual tree. The branch has the name
631 // container name. The type of the container is given by the
632 // ClassName entry in the container. The Address is the address of a
633 // pointer to the container (gotten from the branch entry). As
634 // Basket size we specify a (more or less) common default value.
635 // The containers should be written in Splitlevel=1
636 //
637 *fLog << inf << "Creating Branch '" << cname << "' ";
638 if ((TString)cname!=(TString)cont->ClassName())
639 *fLog << "[" << cont->ClassName() << "] ";
640 *fLog << "in tree " << tree->GetName() << "... " << flush;
641
642 branch = tree->Branch(branchname, cont->ClassName(), entry->GetAddress());
643
644 //
645 // If the branch couldn't be created we have a problem.
646 //
647 if (!branch)
648 {
649 *fLog << endl;
650 *fLog << err << "Unable to create branch '" << cname << "'." << endl;
651 return kFALSE;
652 }
653
654 *fLog << "done." << endl;
655
656 if (!tree->TestBit(kIsNewTree) && !fSplitRule.IsNull())
657 {
658 *fLog << warn << endl;
659 *fLog << "WARNING: You have created a new branch in an existing tree." << endl;
660 *fLog << " For this case file-splitting mode is not allowed... disabled!" << endl;
661 *fLog << endl;
662 fSplitRule= "";
663 }
664 }
665
666 //
667 // Tell the entry also which branch belongs to it (this is necessary
668 // for branches belonging to already existing tree, UPDATE-mode)
669 //
670 entry->SetBranch(branch);
671 }
672
673 return kTRUE;
674}
675
676// --------------------------------------------------------------------------
677//
678// Checks all given containers (branch entries) for the write flag.
679// If the write flag is set the corresponding Tree is marked to get filled.
680// All Trees which are marked to be filled are filled with all their
681// branches.
682// In case of a file opened in 'UPDATE' mode, single branches can be
683// filled, too. WARNING - for the moment there is no check whether
684// you filled the correct number of events into the branch, so that
685// each of the other branches in the tree has the correct corresponding
686// number of new entries in the new branch!
687// Be carefull: If only one container (corresponding to a branch) of a tree
688// has the write flag, all containers in this tree are filled!
689//
690Bool_t MWriteRootFile::CheckAndWrite()
691{
692 // This is the special case if we only copy a tre but do not
693 // write containers to it
694 const Int_t n = fTrees.GetEntriesFast();
695 if (n==0)
696 return kTRUE;
697
698 TObject *obj;
699
700 //
701 // Loop over all branch entries
702 //
703 TIter NextBranch(&fBranches);
704 while ((obj=NextBranch()))
705 {
706 MRootFileBranch *b = (MRootFileBranch*)obj;
707
708 //
709 // Check for the Write flag
710 //
711 if (!b->GetContainer()->IsReadyToSave())
712 continue;
713
714 //
715 // If the write flag of the branch entry is set, set the write flag of
716 // the corresponding tree entry.
717 //
718 if (b->GetTree()->TestBit(kIsNewTree))
719 b->GetTree()->SetBit(kFillTree);
720 else
721 {
722 if (!b->GetBranch()->Fill())
723 {
724 *fLog << err << "ERROR - MWriteRootFile: Zero bytes written to branch '" << b->GetBranch()->GetName() << "'... abort." << endl;
725 return kFALSE;
726 }
727 }
728 }
729
730 //
731 // Loop over all tree entries
732 //
733 for (int idx=0; idx<n; idx++)
734 {
735 TTree *t = (TTree*)fTrees[idx];
736 if (!t)
737 {
738 *fLog << err << "ERROR - MWriteRootFile: The Tree with index " << idx << endl;
739 *fLog << " in fTrees is NULL. This should never happen. Please send" << endl;
740 *fLog << " a bug report." << endl;
741 return kFALSE;
742 }
743
744 //
745 // Check the write flag of the tree
746 //
747 if (!t->TestBit(kFillTree))
748 continue;
749
750 //
751 // If the write flag is set, fill the tree (with the corresponding
752 // branches/containers), delete the write flag and increase the number
753 // of written/filled entries.
754 //
755 t->ResetBit(kFillTree);
756
757 if (!t->Fill())
758 {
759 *fLog << err << "ERROR - MWriteRootFiole: Zero bytes written to tree '" << t->GetName() << "'... abort." << endl;
760 return kFALSE;
761 }
762 }
763
764 //
765 // If we are writing into memory we don't split into seperate files
766 //
767 if (!fOut || TestBit(kIsNotOwner))
768 return kTRUE;
769
770 //
771 // For more information see TTree:ChangeFile()
772 //
773 TTree *t0 = (TTree*)fTrees[0];
774 if (!t0 || fOut==t0->GetCurrentFile())
775 return kTRUE;
776
777 // FIXME: THIS IS EMITTED FOR ALL CONSEQUTIVE EVENTS!
778 *fLog << warn << endl;
779 *fLog << "WARNING - MWriteRootFile: Root's TTree/TFile has opened a new file" << endl;
780 *fLog << " automatically. You can change this behaviour using TTree::SetMaxTreeSize." << endl;
781 *fLog << " You won't be able to read splitted files correctly with MReadMarsFile if" << endl;
782 *fLog << " they have more than one entry in 'RunHeaders' or you try to read more than" << endl;
783 *fLog << " one of such sequences at once." << endl;
784 *fLog << endl;
785
786 return kTRUE;
787}
788
789// --------------------------------------------------------------------------
790//
791// Open a new file with the name fname. Move all trees and branches from the
792// old file to the new file.
793//
794Bool_t MWriteRootFile::ChangeFile(const char *fname)
795{
796 const Int_t compr = fOut ? fOut->GetCompressionLevel() : 0;
797 const TString title = fOut ? fOut->GetTitle() : "";
798 const TString opt = fOut ? fOut->GetOption() : "";
799
800 // Open new file with old setup
801 TFile *newfile = OpenFile(fname, opt, title, compr);
802 if (newfile && newfile==fOut)
803 {
804 *fLog << inf << "Found open file '" << fname << "'... using." << endl;
805 return kTRUE;
806 }
807 if (!newfile)
808 {
809 *fLog << err << "ERROR - Cannot open new file '" << fname << "'" << endl;
810 return kFALSE;
811 }
812
813 if (!fOut)
814 {
815 *fLog << err << "ERROR - MWriteRootFile::ChangeFile... something went terribly wrong!" << endl;
816 *fLog << " fname: " << fname << endl;
817 *fLog << " Please start debugging!" << endl;
818 return kFALSE;
819 }
820
821 *fLog << inf << "Open new file " << fname << " (Title=" << title << ", Option=" << opt << ", Compression=" << compr << ")" << endl;
822
823 // Print statistics of old file
824 const TString n = GetFileName();
825 if (!n.IsNull() && n!=TString("/dev/null"))
826 Print();
827
828 if (fOut->IsOpen())
829 fOut->Write();
830
831 // Move all trees from the old file to the new file
832 TObject *obj=0;
833 while ((obj = fOut->GetList()->First()))
834 {
835 // Remove obj from old file (otherwise deleting
836 // the old file will delete the objs)
837 fOut->GetList()->Remove(obj);
838
839 // If this is not a tree do nothing.
840 if (!obj->InheritsFrom(TTree::Class()))
841 continue;
842
843 // process all trees in the old file
844 TTree *t = (TTree*)obj;
845
846 // reset and move to new file (this is done implicitly for all branches)
847 t->Reset();
848 t->SetDirectory(newfile);
849 }
850
851 // Close/delete the old file (keys already written above)
852 *fLog << inf3 << "Closing file " << fOut->GetName() << "." << endl;
853 delete fOut;
854
855 // Replace current with new file
856 fOut = newfile;
857
858 // Change current directory to new file
859 gFile = fOut;
860
861 return kTRUE;
862}
863
864// --------------------------------------------------------------------------
865//
866// A rule looks like:
867// "s/source/destination/"
868//
869// For more details on regular expression see a proper documentation,
870// for example, "man 7 regex" if installed, or TPRegexp.
871//
872// Here is an example:
873//
874// Example:
875// inputfile: /data/MAGIC/Period016/rootdata/20040621_23210_D_Mkn421_E.root
876// rule: /([0-9]+_[0-9]+)_D_(.*[.]root)/\\/outpath\\/$1_Y_$2/
877// outfile: /outpath/20040621_23210_Y_Mkn421_E.root
878//
879// Please make sure that all / in your rules are correctly escaped, i.e.
880// in the string stored in memory it must look like \/ and in the string
881// your set in your program it must look \\/.
882//
883// Note, that this function has been made static to allow your to
884// test your split rules, i.e. regular expressions.
885//
886TString MWriteRootFile::SubstituteName(const char *regexp, TString fname)
887{
888 // Remove the path from the filename
889 if (fname.Last('/')>=0)
890 fname.Remove(0, fname.Last('/')+1);
891
892 // Regular expression to split the rule into its contents
893 static const TString sed("s/((\\\\/|[^/])*)/((\\\\/|[^/])*)/([gimosxd]*)");
894
895 // Do splitting
896 TObjArray *subStrL = TPRegexp(sed).MatchS(regexp);
897 if (subStrL->GetEntries()!=6)
898 {
899 gLog << err << "ERROR - SubstituteName: Evaluating regexp " << regexp << " failed." << endl;
900 subStrL->Print();
901 delete subStrL;
902 return "";
903 }
904
905 /*const*/ TString reg = (*subStrL)[1]->GetName(); // Regular expression to search for
906 /*const*/ TString tar = (*subStrL)[3]->GetName(); // Regular expression for replacing
907 const TString mod = (*subStrL)[5]->GetName(); // Possible modifiers (e.g. 'a')
908
909 delete subStrL;
910
911 // Unescpae slashes in paths
912 reg.ReplaceAll("\\/", "/");
913 tar.ReplaceAll("\\/", "/");
914
915 // Do substitution
916 const Int_t nrSub = TPRegexp(reg).Substitute(fname, tar, mod);
917 if (nrSub==0)
918 {
919 gLog << err << "ERROR - Substituting due to SplitRule failed." << endl;
920 gLog << " Source FileName: " << fname << endl;
921 gLog << " Search Rexexp: " << reg << endl;
922 gLog << " Replace Rexexp: " << tar << endl;
923 gLog << " Modifiers: " << mod << endl;
924 return "";
925 }
926
927 return fname;
928}
929
930// --------------------------------------------------------------------------
931//
932// Writes a copy of the TTree t to the currently open file using
933// TTree::CloneTree()
934//
935void MWriteRootFile::CopyTree(TTree &t) const
936{
937 TString out = "Copy of tree ";
938 out += t.GetName();
939 out += " in progress...";
940
941 if (fDisplay)
942 fDisplay->SetStatusLine2(out);
943
944 *fLog << inf << out << flush;
945
946 // When a new file has been opened the old clone (if existing) has
947 // been moved to the new file. We could now use CopyTree but then
948 // we would have to unpack all data and repack it. Instead
949 // we delete the moved old tree.
950 // FIXME: In priciple we could delete a "wrong" tree with the same name.
951 // Should we flag the clones and delete them in ChangeFile?
952 TObject *old = fOut->GetList()->Remove(fOut->GetList()->FindObject(t.GetName()));
953 delete old;
954
955 // Now we clone the tree without unpacking and repacking.
956 // When it has not been moved it will be deleted in the TFile destructor
957 /*TTree *clone =*/ t.CloneTree(-1, "fast");
958 //clone->Write();
959 //delete clone;
960
961 *fLog << "done." << endl;
962
963 if (fDisplay)
964 {
965 out += " done.";
966 fDisplay->SetStatusLine2(out);
967 }
968}
969
970// --------------------------------------------------------------------------
971//
972// Make all copies requested from the currently open file into the new
973// file.
974//
975Bool_t MWriteRootFile::MakeCopies(const char *fname) const
976{
977 if (fCopies.GetEntries()==0)
978 return kTRUE;
979
980 TFile *file = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(fname));
981 if (!file)
982 {
983 *fLog << err << "ERROR - MakeCopies: File " << fname << " not found in gROOT->GetListOfFiles()... abort." << endl;
984 return kFALSE;
985 }
986
987 TIter Next(&fCopies);
988 TObject *o=0;
989 TTree *t=0;
990
991 fOut->cd();
992 while ((o=Next()))
993 {
994 TTree *gettree = dynamic_cast<TTree*>(file->Get(o->GetName()));
995 if (!gettree)
996 {
997 const Bool_t force = o->TestBit(kForced);
998 if (force)
999 *fLog << err << "ERROR - ";
1000 else
1001 *fLog << inf3;
1002
1003 *fLog << "MakeCopies: Tree " << o->GetName() << " not found in file " << fname << ".";
1004 if (force)
1005 *fLog << ".. abort.";
1006 *fLog << endl;
1007
1008 if (force)
1009 return kFALSE;
1010
1011 continue;
1012 }
1013
1014 gettree->SetBranchStatus(o->GetTitle(), 1);
1015
1016 // First Execution
1017 if (t==gettree)
1018 continue;
1019
1020 // Check if its the first call
1021 if (t)
1022 CopyTree(*t);
1023 t = gettree;
1024 }
1025
1026 if (t)
1027 CopyTree(*t);
1028
1029 return kTRUE;
1030}
1031
1032// --------------------------------------------------------------------------
1033//
1034// ReInit. If file splitting is not allowed call MWriteFile::ReInit.
1035//
1036// In other cases get MRead from the TaskList (splitting is switched of if
1037// this is impossible).
1038//
1039// Convert the input- into a new output file-name.
1040//
1041// Open a new file, change all trees to the new file (calling ChangeFile()),
1042// and close the old one.
1043//
1044// Call MWriteFile::ReInit()
1045//
1046Bool_t MWriteRootFile::ReInit(MParList *pList)
1047{
1048 MRead *read = (MRead*)pList->FindTask("MRead");
1049 if (fSplitRule.IsNull() && fCopies.GetEntries()>0 && fOut)
1050 {
1051 if (!read)
1052 {
1053 *fLog << err;
1054 *fLog << "ERROR: No Task 'MRead' found in the tasklist. This task is" << endl;
1055 *fLog << " necessary to get the filename. Without a filename file" << endl;
1056 *fLog << " AddCopySource cannot be used... abort." << endl;
1057 *fLog << endl;
1058 return kFALSE;
1059 }
1060 if (!MakeCopies(read->GetFullFileName()))
1061 return kFALSE;
1062
1063 }
1064
1065 if (fSplitRule.IsNull() || !(fOut || TestBit(kIsNotOwner)))
1066 return MWriteFile::ReInit(pList);
1067
1068 if (!read)
1069 {
1070 *fLog << warn;
1071 *fLog << "WARNING: No Task 'MRead' found in the tasklist. This task is" << endl;
1072 *fLog << " necessary to get the filename. Without a filename file" << endl;
1073 *fLog << " file splitting is not allowed... disabled!" << endl;
1074 *fLog << endl;
1075 fSplitRule = "";
1076 return kTRUE;
1077 }
1078
1079
1080 const TString oldname = read->GetFullFileName();
1081 const TString newname = SubstituteName(fSplitRule, oldname);
1082 if (!ChangeFile(newname))
1083 return kFALSE;
1084
1085 if (!MakeCopies(oldname))
1086 return kFALSE;
1087
1088 return MWriteFile::ReInit(pList);
1089}
1090
1091// --------------------------------------------------------------------------
1092//
1093// return open state of the root file. If the file is 'memory' kTRUE is
1094// returned.
1095//
1096Bool_t MWriteRootFile::IsFileOpen() const
1097{
1098 if (!fOut)
1099 return kTRUE;
1100
1101 const char *n = fOut->GetName();
1102 return n==0 || *n==0 ? kTRUE : fOut->IsOpen();
1103}
1104
1105// --------------------------------------------------------------------------
1106//
1107// return name of the root-file. If the file is "memory" "<memory>" is
1108// returned.
1109//
1110const char *MWriteRootFile::GetFileName() const
1111{
1112 if (!fOut)
1113 return "<memory>";
1114
1115 const char *n = fOut->GetName();
1116 return n==0 || *n==0 ? "<dummy>" : n;
1117}
1118
1119// --------------------------------------------------------------------------
1120//
1121// cd into file. See TFile::cd(). If the file is "memory" kTRUE is returned.
1122//
1123Bool_t MWriteRootFile::cd(const char *path)
1124{
1125 return fOut ? fOut->cd(path) : kTRUE;
1126}
1127
1128// --------------------------------------------------------------------------
1129//
1130// If the output file is deleted set fOut to NULL.
1131// Call MTask::RecursiveRemove
1132//
1133void MWriteRootFile::RecursiveRemove(TObject *obj)
1134{
1135 if (obj==fOut)
1136 fOut=NULL;
1137
1138 MWriteFile::RecursiveRemove(obj);
1139}
1140
1141// --------------------------------------------------------------------------
1142//
1143// Implementation of SavePrimitive. Used to write the call to a constructor
1144// to a macro. In the original root implementation it is used to write
1145// gui elements to a macro-file.
1146//
1147void MWriteRootFile::StreamPrimitive(ostream &out) const
1148{
1149 out << " MWriteRootFile " << GetUniqueName();
1150 if (fOut)
1151 {
1152 out << "(\"";
1153 out << fOut->GetName() << "\", \"";
1154 out << fOut->GetOption() << "\", \"";
1155 out << fOut->GetTitle() << "\", ";
1156 out << fOut->GetCompressionLevel();
1157 out << ")";
1158 }
1159 out << ";" << endl;
1160
1161 if (fName!=gsDefName)
1162 out << " " << GetUniqueName() << ".SetName(\"" << fName << "\");" << endl;
1163 if (fTitle!=gsDefTitle)
1164 out << " " << GetUniqueName() << ".SetTitle(\"" << fTitle << "\");" << endl;
1165
1166 MRootFileBranch *entry;
1167 TIter Next(&fBranches);
1168 while ((entry=(MRootFileBranch*)Next()))
1169 {
1170 out << " " << GetUniqueName() << ".AddContainer(";
1171
1172 if (entry->GetContainer())
1173 {
1174 entry->GetContainer()->SavePrimitive(out);
1175 out << "&" << entry->GetContainer()->GetUniqueName();
1176 }
1177 else
1178 out << "\"" << entry->GetContName() << "\"";
1179
1180 out << ", \"" << entry->GetName() << "\"";
1181 if (!entry->MustHave())
1182 out << ", kFALSE";
1183
1184 out <<");" << endl;
1185 }
1186}
1187
Note: See TracBrowser for help on using the repository browser.