Logo ROOT  
Reference Guide
mergeSelective.C File Reference

Detailed Description

View in nbviewer Open in SWAN Merge only part of the content of a set of files.

This macro demonstrates how to merge only a part of the content of a set of input files, specified via the interface.

TFileMerger::AddObjectNames(const char *names)

The method can be called several times to add object names, or using a single string with names separated by a blank. Directory names contained in the files to be merged are accepted.

Two modes are supported:

  1. kOnlyListed: via TFileMerger::PartialMerge(kOnlyListed) This will merge only the objects in the files having the names in the specified list. If a folder is specified, its whole content will be merged
  2. kSkipListed: via TFileMerger::PartialMerge(kSkipListed) This will skip merging of specified objects. If a folder is specified, its whole content will be skipped.

Important note: the kOnlyListed and kSkipListed flags have to be bitwise OR-ed on top of the merging defaults: kAll | kIncremental (as in the example)

The files to be merged have the following structure:

The example first merges exclusively hprof and the content of "folder", producing the file exclusive.root, then merges all content but skipping hprof and the content of "folder". The result can be inspected in the browser.

void CreateFile(const char *);
void mergeSelective(Int_t nfiles=5)
{
// Create the files to be merged
TStopwatch timer;
timer.Start();
TString tutdir = gROOT->GetTutorialDir();
if (gROOT->LoadMacro(tutdir + "/hsimple.C")) return;
Int_t i;
for (i=0; i<nfiles; i++) CreateFile(Form("tomerge%03d.root",i));
//------------------------------------
// Merge only the listed objects
//------------------------------------
fm = new TFileMerger(kFALSE);
fm->OutputFile("exclusive.root");
fm->AddObjectNames("hprof folder");
for (i=0; i<nfiles; i++) fm->AddFile(Form("tomerge%03d.root",i));
// Must add new merging flag on top of the the default ones
Int_t mode = default_mode | TFileMerger::kOnlyListed;
fm->PartialMerge(mode);
fm->Reset();
//------------------------------------
// Skip merging of the listed objects
//------------------------------------
fm->OutputFile("skipped.root");
fm->AddObjectNames("hprof folder");
for (i=0; i<nfiles; i++) fm->AddFile(Form("tomerge%03d.root",i));
// Must add new merging flag on top of the the default ones
mode = default_mode | TFileMerger::kSkipListed;
fm->PartialMerge(mode);
delete fm;
// Cleanup initial files
for (i=0; i<nfiles; i++) gSystem->Unlink(Form("tomerge%03d.root",i));
// Open files to inspect in the browser
TFile::Open("exclusive.root");
TFile::Open("skipped.root");
new TBrowser();
timer.Stop();
timer.Print();
}
void CreateFile(const char *fname)
{
TFile *example = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
if (!example) return;
TH1F *hpx = (TH1F*)example->Get("hpx");
hpx->SetName("hpx1");
TFile::Cp(example->GetName(), fname);
TFile *file = TFile::Open(fname, "UPDATE");
file->mkdir("folder")->cd();
hpx->Write();
file->Close();
example->Close();
TString sname(fname);
if (sname.Contains("000")) {
TFile::Cp(fname, "original.root");
TFile::Open("original.root");
}
}
Author
The Root Team

Definition in file mergeSelective.C.

TSystem::Unlink
virtual int Unlink(const char *name)
Unlink, i.e.
Definition: TSystem.cxx:1380
TBrowser
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
TFileMerger::Reset
virtual void Reset()
Reset merger file list.
Definition: TFileMerger.cxx:117
Form
char * Form(const char *fmt,...)
TFileMerger::kIncremental
@ kIncremental
Merge the input file with the content of the output file (if already existing).
Definition: TFileMerger.h:71
TFileMerger::kOnlyListed
@ kOnlyListed
Only the objects specified in fObjectNames list.
Definition: TFileMerger.h:79
TFileMerger::kAll
@ kAll
Merge all type of objects (default)
Definition: TFileMerger.h:76
TFile::Open
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3998
Int_t
int Int_t
Definition: RtypesCore.h:45
TFile::Cp
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:4895
TH1::SetName
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8794
TString
Basic string class.
Definition: TString.h:136
TFileMerger::AddFile
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...
Definition: TFileMerger.cxx:217
TFileMerger::kSkipListed
@ kSkipListed
Skip objects specified in fObjectNames list.
Definition: TFileMerger.h:80
example
Definition: example.py:1
TFileMerger::AddObjectNames
void AddObjectNames(const char *name)
Definition: TFileMerger.h:101
TStopwatch::Print
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:219
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:101
TFileMerger::OutputFile
virtual Bool_t OutputFile(const char *url, Bool_t force)
Open merger output file.
Definition: TFileMerger.cxx:287
TStopwatch::Start
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
file
Definition: file.py:1
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
TObject::Write
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:798
TStopwatch
Stopwatch class.
Definition: TStopwatch.h:28
TFileMerger
This class provides file copy and merging services.
Definition: TFileMerger.h:30
TFileMerger::PartialMerge
virtual Bool_t PartialMerge(Int_t type=kAll|kIncremental)
Merge the files.
Definition: TFileMerger.cxx:917
TStopwatch::Stop
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
gROOT
#define gROOT
Definition: TROOT.h:404