Logo ROOT   6.12/07
Reference Guide
mergeSelective.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_io
3 /// \notebook -nodraw
4 /// Merge only part of the content of a set of files.
5 /// This macro demonstrates how to merge only a part of the content of a set
6 /// of input files, specified via the interface.
7 /// ~~~{.cpp}
8 /// TFileMerger::AddObjectNames(const char *names)
9 /// ~~~
10 /// The method can be called several times to add object names, or using a single
11 /// string with names separated by a blank. Directory names contained in the files
12 /// to be merged are accepted.
13 ///
14 /// Two modes are supported:
15 /// 1. kOnlyListed: via <tt>TFileMerger::PartialMerge(kOnlyListed)</tt>
16 /// This will merge only the objects in the files having the names in the
17 /// specified list. If a folder is specified, its whole content will be merged
18 ///
19 /// 2. kSkipListed: via <tt>TFileMerger::PartialMerge(kSkipListed)</tt>
20 /// This will skip merging of specified objects. If a folder is specified, its
21 /// whole content will be skipped.
22 ///
23 /// Important note: the kOnlyListed and kSkipListed flags have to be bitwise OR-ed
24 /// on top of the merging defaults: kAll | kIncremental (as in the example)
25 ///
26 /// The files to be merged have the following structure:
27 /// - hpx (TH1F)
28 /// - hpxpy (TH2F)
29 /// - hprof (TProfile)
30 /// - ntuple (TNtuple)
31 /// - folder (TDirectory)
32 /// - hpx1 (TH1F)
33 ///
34 /// The example first merges exclusively hprof and the content of "folder",
35 /// producing the file exclusive.root, then merges all content but skipping
36 /// hprof and the content of "folder". The result can be inspected in the
37 /// browser.
38 ///
39 /// \macro_code
40 ///
41 /// \author The Root Team
42 
43 
44 void CreateFile(const char *);
45 
46 void mergeSelective(Int_t nfiles=5)
47 {
48 
49  // Create the files to be merged
50  TStopwatch timer;
51  timer.Start();
52  TString tutdir = gROOT->GetTutorialDir();
53  if (gROOT->LoadMacro(tutdir + "/hsimple.C")) return;
54  Int_t i;
55  for (i=0; i<nfiles; i++) CreateFile(Form("tomerge%03d.root",i));
56 
57  //------------------------------------
58  // Merge only the listed objects
59  //------------------------------------
60  TFileMerger *fm;
61  fm = new TFileMerger(kFALSE);
62  fm->OutputFile("exclusive.root");
63  fm->AddObjectNames("hprof folder");
64  for (i=0; i<nfiles; i++) fm->AddFile(Form("tomerge%03d.root",i));
65  // Must add new merging flag on top of the the default ones
67  Int_t mode = default_mode | TFileMerger::kOnlyListed;
68  fm->PartialMerge(mode);
69  fm->Reset();
70 
71  //------------------------------------
72  // Skip merging of the listed objects
73  //------------------------------------
74  fm->OutputFile("skipped.root");
75  fm->AddObjectNames("hprof folder");
76  for (i=0; i<nfiles; i++) fm->AddFile(Form("tomerge%03d.root",i));
77  // Must add new merging flag on top of the the default ones
78  mode = default_mode | TFileMerger::kSkipListed;
79  fm->PartialMerge(mode);
80  delete fm;
81 
82 
83  // Cleanup initial files
84  for (i=0; i<nfiles; i++) gSystem->Unlink(Form("tomerge%03d.root",i));
85  // Open files to inspect in the browser
86  TFile::Open("exclusive.root");
87  TFile::Open("skipped.root");
88  new TBrowser();
89  timer.Stop();
90  timer.Print();
91 }
92 
93 void CreateFile(const char *fname)
94 {
95  TFile *example = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
96  if (!example) return;
97  TH1F *hpx = (TH1F*)example->Get("hpx");
98  hpx->SetName("hpx1");
99  TFile::Cp(example->GetName(), fname);
100  TFile *file = TFile::Open(fname, "UPDATE");
101  file->mkdir("folder")->cd();
102  hpx->Write();
103  file->Close();
104  example->Close();
105  TString sname(fname);
106  if (sname.Contains("000")) {
107  TFile::Cp(fname, "original.root");
108  TFile::Open("original.root");
109  }
110 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:785
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:219
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:402
Basic string class.
Definition: TString.h:125
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
int Int_t
Definition: RtypesCore.h:41
Only the objects specified in fObjectNames list.
Definition: TFileMerger.h:73
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3950
virtual Bool_t Cp(const char *dst, Bool_t progressbar=kTRUE, UInt_t buffersize=1000000)
Allows to copy this file to the dst URL.
Definition: TFile.cxx:4848
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1357
Merge all type of objects (default)
Definition: TFileMerger.h:70
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
Skip objects specified in fObjectNames list.
Definition: TFileMerger.h:74
Merge the input file with the content of the output file (if already exising).
Definition: TFileMerger.h:66
void AddObjectNames(const char *name)
Definition: TFileMerger.h:95
virtual Bool_t OutputFile(const char *url, Bool_t force)
Open merger output file.
virtual Bool_t AddFile(TFile *source, Bool_t own, Bool_t cpProgress)
Add the TFile to this file merger and give ownership of the TFile to this object (unless kFALSE is re...
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
This class provides file copy and merging services.
Definition: TFileMerger.h:30
char * Form(const char *fmt,...)
const Bool_t kFALSE
Definition: RtypesCore.h:88
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8217
virtual Bool_t PartialMerge(Int_t type=kAll|kIncremental)
Merge the files.
Definition: file.py:1
virtual void Reset()
Reset merger file list.
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:916
Stopwatch class.
Definition: TStopwatch.h:28