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