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

Last change on this file since 9910 was 9576, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 37.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: 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 << "New file '" << name << "' ";
162 if (!TString(title).IsNull())
163 *fLog << "<Title=" << title << "> ";
164 *fLog << "created." << 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 TObject *obj;
693
694 //
695 // Loop over all branch entries
696 //
697 TIter NextBranch(&fBranches);
698 while ((obj=NextBranch()))
699 {
700 MRootFileBranch *b = (MRootFileBranch*)obj;
701
702 //
703 // Check for the Write flag
704 //
705 if (!b->GetContainer()->IsReadyToSave())
706 continue;
707
708 //
709 // If the write flag of the branch entry is set, set the write flag of
710 // the corresponding tree entry.
711 //
712 if (b->GetTree()->TestBit(kIsNewTree))
713 b->GetTree()->SetBit(kFillTree);
714 else
715 {
716 if (!b->GetBranch()->Fill())
717 {
718 *fLog << err << "ERROR - MWriteRootFile: Zero bytes written to branch '" << b->GetBranch()->GetName() << "'... abort." << endl;
719 return kFALSE;
720 }
721 }
722 }
723
724 //
725 // Loop over all tree entries
726 //
727 const Int_t n = fTrees.GetEntriesFast();
728
729 for (int idx=0; idx<n; idx++)
730 {
731 TTree *t = (TTree*)fTrees[idx];
732 if (!t)
733 {
734 *fLog << err << "ERROR - MWriteRootFile: The Tree with index " << idx << endl;
735 *fLog << " in fTrees is NULL. This should never happen. Please send" << endl;
736 *fLog << " a bug report." << endl;
737 return kFALSE;
738 }
739
740 //
741 // Check the write flag of the tree
742 //
743 if (!t->TestBit(kFillTree))
744 continue;
745
746 //
747 // If the write flag is set, fill the tree (with the corresponding
748 // branches/containers), delete the write flag and increase the number
749 // of written/filled entries.
750 //
751 t->ResetBit(kFillTree);
752
753 if (!t->Fill())
754 {
755 *fLog << err << "ERROR - MWriteRootFiole: Zero bytes written to tree '" << t->GetName() << "'... abort." << endl;
756 return kFALSE;
757 }
758 }
759
760 //
761 // If we are writing into memory we don't split into seperate files
762 //
763 if (!fOut || TestBit(kIsNotOwner))
764 return kTRUE;
765
766 //
767 // For more information see TTree:ChangeFile()
768 //
769 TTree *t0 = (TTree*)fTrees[0];
770 if (!t0 || fOut==t0->GetCurrentFile())
771 return kTRUE;
772
773 // FIXME: THIS IS EMITTED FOR ALL CONSEQUTIVE EVENTS!
774 *fLog << warn << endl;
775 *fLog << "WARNING - MWriteRootFile: Root's TTree/TFile has opened a new file" << endl;
776 *fLog << " automatically. You can change this behaviour using TTree::SetMaxTreeSize." << endl;
777 *fLog << " You won't be able to read splitted files correctly with MReadMarsFile if" << endl;
778 *fLog << " they have more than one entry in 'RunHeaders' or you try to read more than" << endl;
779 *fLog << " one of such sequences at once." << endl;
780 *fLog << endl;
781
782 return kTRUE;
783}
784
785// --------------------------------------------------------------------------
786//
787// Open a new file with the name fname. Move all trees and branches from the
788// old file to the new file.
789//
790Bool_t MWriteRootFile::ChangeFile(const char *fname)
791{
792 const Int_t compr = fOut ? fOut->GetCompressionLevel() : 0;
793 const TString title = fOut ? fOut->GetTitle() : "";
794 const TString opt = fOut ? fOut->GetOption() : "";
795
796 // Open new file with old setup
797 TFile *newfile = OpenFile(fname, opt, title, compr);
798 if (newfile && newfile==fOut)
799 {
800 *fLog << inf << "Found open file " << fname << "... using." << endl;
801 return kTRUE;
802 }
803 if (!newfile)
804 {
805 *fLog << err << "ERROR - Cannot open new file " << fname << endl;
806 return kFALSE;
807 }
808
809 if (!fOut)
810 {
811 *fLog << err << "ERROR - MWriteRootFile::ChangeFile... something went terribly wrong!" << endl;
812 *fLog << " fname: " << fname << endl;
813 *fLog << " Please start debugging!" << endl;
814 return kFALSE;
815 }
816
817 *fLog << inf << "Open new file " << fname << " (Title=" << title << ", Option=" << opt << ", Compression=" << compr << ")" << endl;
818
819 // Print statistics of old file
820 const TString n = GetFileName();
821 if (!n.IsNull() && n!=TString("/dev/null"))
822 Print();
823
824 if (fOut->IsOpen())
825 fOut->Write();
826
827 // Move all trees from the old file to the new file
828 TObject *obj=0;
829 while ((obj = fOut->GetList()->First()))
830 {
831 // Remove obj from old file (otherwise deleting
832 // the old file will delete the objs)
833 fOut->GetList()->Remove(obj);
834
835 // If this is not a tree do nothing.
836 if (!obj->InheritsFrom(TTree::Class()))
837 continue;
838
839 // process all trees in the old file
840 TTree *t = (TTree*)obj;
841
842 // reset and move to new file (this is done implicitly for all branches)
843 t->Reset();
844 t->SetDirectory(newfile);
845 }
846
847 // Close/delete the old file (keys already written above)
848 *fLog << inf3 << "Closing file " << fOut->GetName() << "." << endl;
849 delete fOut;
850
851 // Replace current with new file
852 fOut = newfile;
853
854 // Change current directory to new file
855 gFile = fOut;
856
857 return kTRUE;
858}
859
860// --------------------------------------------------------------------------
861//
862// A rule looks like:
863// "s/source/destination/"
864//
865// For more details on regular expression see a proper documentation,
866// for example, "man 7 regex" if installed, or TPRegexp.
867//
868// Here is an example:
869//
870// Example:
871// inputfile: /data/MAGIC/Period016/rootdata/20040621_23210_D_Mkn421_E.root
872// rule: /([0-9]+_[0-9]+)_D_(.*[.]root)/\\/outpath\\/$1_Y_$2/
873// outfile: /outpath/20040621_23210_Y_Mkn421_E.root
874//
875// Please make sure that all / in your rules are correctly escaped, i.e.
876// in the string stored in memory it must look like \/ and in the string
877// your set in your program it must look \\/.
878//
879// Note, that this function has been made static to allow your to
880// test your split rules, i.e. regular expressions.
881//
882TString MWriteRootFile::SubstituteName(const char *regexp, TString fname)
883{
884 // Remove the path from the filename
885 if (fname.Last('/')>=0)
886 fname.Remove(0, fname.Last('/')+1);
887
888 // Regular expression to split the rule into its contents
889 static const TString sed("s/((\\\\/|[^/])*)/((\\\\/|[^/])*)/([gimosxd]*)");
890
891 // Do splitting
892 TObjArray *subStrL = TPRegexp(sed).MatchS(regexp);
893 if (subStrL->GetEntries()!=6)
894 {
895 gLog << err << "ERROR - SubstituteName: Evaluating regexp " << regexp << " failed." << endl;
896 subStrL->Print();
897 delete subStrL;
898 return "";
899 }
900
901 /*const*/ TString reg = (*subStrL)[1]->GetName(); // Regular expression to search for
902 /*const*/ TString tar = (*subStrL)[3]->GetName(); // Regular expression for replacing
903 const TString mod = (*subStrL)[5]->GetName(); // Possible modifiers (e.g. 'a')
904
905 delete subStrL;
906
907 // Unescpae slashes in paths
908 reg.ReplaceAll("\\/", "/");
909 tar.ReplaceAll("\\/", "/");
910
911 // Do substitution
912 const Int_t nrSub = TPRegexp(reg).Substitute(fname, tar, mod);
913 if (nrSub==0)
914 {
915 gLog << err << "ERROR - Substituting due to SplitRule failed." << endl;
916 gLog << " Source FileName: " << fname << endl;
917 gLog << " Search Rexexp: " << reg << endl;
918 gLog << " Replace Rexexp: " << tar << endl;
919 gLog << " Modifiers: " << mod << endl;
920 return "";
921 }
922
923 return fname;
924}
925
926// --------------------------------------------------------------------------
927//
928// Writes a copy of the TTree t to the currently open file using
929// TTree::CloneTree()
930//
931void MWriteRootFile::CopyTree(TTree &t) const
932{
933 TString out = "Copy of tree ";
934 out += t.GetName();
935 out += " in progress...";
936
937 if (fDisplay)
938 fDisplay->SetStatusLine2(out);
939
940 *fLog << inf << out << flush;
941
942 // When a new file has been opened the old clone (if existing) has
943 // been moved to the new file. We could now use CopyTree but then
944 // we would have to unpack all data and repack it. Instead
945 // we delete the moved old tree.
946 // FIXME: In priciple we could delete a "wrong" tree with the same name.
947 // Should we flag the clones and delete them in ChangeFile?
948 TObject *old = fOut->GetList()->Remove(fOut->GetList()->FindObject(t.GetName()));
949 delete old;
950
951 // Now we clone the tree without unpacking and repacking.
952 // When it has not been moved it will be deleted in the TFile destructor
953 /*TTree *clone =*/ t.CloneTree(-1, "fast");
954 //clone->Write();
955 //delete clone;
956
957 *fLog << "done." << endl;
958
959 if (fDisplay)
960 {
961 out += " done.";
962 fDisplay->SetStatusLine2(out);
963 }
964}
965
966// --------------------------------------------------------------------------
967//
968// Make all copies requested from the currently open file into the new
969// file.
970//
971Bool_t MWriteRootFile::MakeCopies(const char *fname) const
972{
973 if (fCopies.GetEntries()==0)
974 return kTRUE;
975
976 TFile *file = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(fname));
977 if (!file)
978 {
979 *fLog << err << "ERROR - MakeCopies: File " << fname << " not found in gROOT->GetListOfFiles()... abort." << endl;
980 return kFALSE;
981 }
982
983 TIter Next(&fCopies);
984 TObject *o=0;
985 TTree *t=0;
986
987 fOut->cd();
988 while ((o=Next()))
989 {
990 TTree *gettree = dynamic_cast<TTree*>(file->Get(o->GetName()));
991 if (!gettree)
992 {
993 const Bool_t force = o->TestBit(kForced);
994 if (force)
995 *fLog << err << "ERROR - ";
996 else
997 *fLog << inf3;
998
999 *fLog << "MakeCopies: Tree " << o->GetName() << " not found in file " << fname << ".";
1000 if (force)
1001 *fLog << ".. abort.";
1002 *fLog << endl;
1003
1004 if (force)
1005 return kFALSE;
1006
1007 continue;
1008 }
1009
1010 gettree->SetBranchStatus(o->GetTitle(), 1);
1011
1012 // First Execution
1013 if (t==gettree)
1014 continue;
1015
1016 // Check if its the first call
1017 if (t)
1018 CopyTree(*t);
1019 t = gettree;
1020 }
1021
1022 if (t)
1023 CopyTree(*t);
1024
1025 return kTRUE;
1026}
1027
1028// --------------------------------------------------------------------------
1029//
1030// ReInit. If file splitting is not allowed call MWriteFile::ReInit.
1031//
1032// In other cases get MRead from the TaskList (splitting is switched of if
1033// this is impossible).
1034//
1035// Convert the input- into a new output file-name.
1036//
1037// Open a new file, change all trees to the new file (calling ChangeFile()),
1038// and close the old one.
1039//
1040// Call MWriteFile::ReInit()
1041//
1042Bool_t MWriteRootFile::ReInit(MParList *pList)
1043{
1044 MRead *read = (MRead*)pList->FindTask("MRead");
1045 if (fSplitRule.IsNull() && fCopies.GetEntries()>0 && fOut)
1046 {
1047 if (!read)
1048 {
1049 *fLog << err;
1050 *fLog << "ERROR: No Task 'MRead' found in the tasklist. This task is" << endl;
1051 *fLog << " necessary to get the filename. Without a filename file" << endl;
1052 *fLog << " AddCopySource cannot be used... abort." << endl;
1053 *fLog << endl;
1054 return kFALSE;
1055 }
1056 if (!MakeCopies(read->GetFullFileName()))
1057 return kFALSE;
1058
1059 }
1060
1061 if (fSplitRule.IsNull() || !(fOut || TestBit(kIsNotOwner)))
1062 return MWriteFile::ReInit(pList);
1063
1064 if (!read)
1065 {
1066 *fLog << warn;
1067 *fLog << "WARNING: No Task 'MRead' found in the tasklist. This task is" << endl;
1068 *fLog << " necessary to get the filename. Without a filename file" << endl;
1069 *fLog << " file splitting is not allowed... disabled!" << endl;
1070 *fLog << endl;
1071 fSplitRule = "";
1072 return kTRUE;
1073 }
1074
1075
1076 const TString oldname = read->GetFullFileName();
1077 const TString newname = SubstituteName(fSplitRule, oldname);
1078 if (!ChangeFile(newname))
1079 return kFALSE;
1080
1081 if (!MakeCopies(oldname))
1082 return kFALSE;
1083
1084 return MWriteFile::ReInit(pList);
1085}
1086
1087// --------------------------------------------------------------------------
1088//
1089// return open state of the root file. If the file is 'memory' kTRUE is
1090// returned.
1091//
1092Bool_t MWriteRootFile::IsFileOpen() const
1093{
1094 if (!fOut)
1095 return kTRUE;
1096
1097 const char *n = fOut->GetName();
1098 return n==0 || *n==0 ? kTRUE : fOut->IsOpen();
1099}
1100
1101// --------------------------------------------------------------------------
1102//
1103// return name of the root-file. If the file is "memory" "<memory>" is
1104// returned.
1105//
1106const char *MWriteRootFile::GetFileName() const
1107{
1108 if (!fOut)
1109 return "<memory>";
1110
1111 const char *n = fOut->GetName();
1112 return n==0 || *n==0 ? "<dummy>" : n;
1113}
1114
1115// --------------------------------------------------------------------------
1116//
1117// cd into file. See TFile::cd(). If the file is "memory" kTRUE is returned.
1118//
1119Bool_t MWriteRootFile::cd(const char *path)
1120{
1121 return fOut ? fOut->cd(path) : kTRUE;
1122}
1123
1124// --------------------------------------------------------------------------
1125//
1126// If the output file is deleted set fOut to NULL.
1127// Call MTask::RecursiveRemove
1128//
1129void MWriteRootFile::RecursiveRemove(TObject *obj)
1130{
1131 if (obj==fOut)
1132 fOut=NULL;
1133
1134 MWriteFile::RecursiveRemove(obj);
1135}
1136
1137// --------------------------------------------------------------------------
1138//
1139// Implementation of SavePrimitive. Used to write the call to a constructor
1140// to a macro. In the original root implementation it is used to write
1141// gui elements to a macro-file.
1142//
1143void MWriteRootFile::StreamPrimitive(ostream &out) const
1144{
1145 out << " MWriteRootFile " << GetUniqueName();
1146 if (fOut)
1147 {
1148 out << "(\"";
1149 out << fOut->GetName() << "\", \"";
1150 out << fOut->GetOption() << "\", \"";
1151 out << fOut->GetTitle() << "\", ";
1152 out << fOut->GetCompressionLevel();
1153 out << ")";
1154 }
1155 out << ";" << endl;
1156
1157 if (fName!=gsDefName)
1158 out << " " << GetUniqueName() << ".SetName(\"" << fName << "\");" << endl;
1159 if (fTitle!=gsDefTitle)
1160 out << " " << GetUniqueName() << ".SetTitle(\"" << fTitle << "\");" << endl;
1161
1162 MRootFileBranch *entry;
1163 TIter Next(&fBranches);
1164 while ((entry=(MRootFileBranch*)Next()))
1165 {
1166 out << " " << GetUniqueName() << ".AddContainer(";
1167
1168 if (entry->GetContainer())
1169 {
1170 entry->GetContainer()->SavePrimitive(out);
1171 out << "&" << entry->GetContainer()->GetUniqueName();
1172 }
1173 else
1174 out << "\"" << entry->GetContName() << "\"";
1175
1176 out << ", \"" << entry->GetName() << "\"";
1177 if (!entry->MustHave())
1178 out << ", kFALSE";
1179
1180 out <<");" << endl;
1181 }
1182}
1183
Note: See TracBrowser for help on using the repository browser.