Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TFileMerger.h
Go to the documentation of this file.
1// @(#)root/io:$Id$
2// Author: Andreas Peters + Fons Rademakers 26/5/2005
3
4/*************************************************************************
5 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TFileMerger
13#define ROOT_TFileMerger
14
15#include "TObject.h"
16#include "TList.h"
17#include "TString.h"
18#include "TStopwatch.h"
19#include <string>
20
21class TFile;
22class TDirectory;
23class THashList;
24class TKey;
25
26namespace ROOT {
27class TIOFeatures;
28} // namespace ROOT
29
30class TFileMerger : public TObject {
31public:
32 enum class EErrorBehavior {
33 /// The merging process will stop and yield failure when encountering invalid objects
35 /// The merging process will skip invalid objects and continue
37 };
38
39private:
41
42 TFileMerger(const TFileMerger&) = delete;
44
45protected:
46 TStopwatch fWatch; ///< Stop watch to measure file copy speed
47 TList fFileList; ///< A list the file (TFile*) which shall be merged
48 TFile *fOutputFile{nullptr}; ///< The outputfile for merging
49 TString fOutputFilename; ///< The name of the outputfile for merging
50 Bool_t fFastMethod{kTRUE}; ///< True if using Fast merging algorithm (default)
51 Bool_t fNoTrees{kFALSE}; ///< True if Trees should not be merged (default is kFALSE)
52 Bool_t fExplicitCompLevel{kFALSE}; ///< True if the user explicitly requested a compression level change (default kFALSE)
53 Bool_t fCompressionChange{kFALSE}; ///< True if the output and input have different compression level (default kFALSE)
54 Int_t fPrintLevel{0}; ///< How much information to print out at run time
55 TString fMergeOptions; ///< Options (in string format) to be passed down to the Merge functions
56 TIOFeatures *fIOFeatures{nullptr}; ///< IO features to use in the output file.
57 TString fMsgPrefix{"TFileMerger"}; ///< Prefix to be used when printing informational message (default TFileMerger)
58 EErrorBehavior fErrBehavior = EErrorBehavior::kFailOnError; ///< What to do in case of errors during merging
59
60 Int_t fMaxOpenedFiles; ///< Maximum number of files opened at the same time by the TFileMerger
61 Bool_t fLocal; ///< Makes local copies of merging files if True (default is kTRUE)
62 Bool_t fHistoOneGo; ///< Merger histos in one go (default is kTRUE)
63 TString fObjectNames; ///< List of object names to be either merged exclusively or skipped
64 TList fMergeList; ///< list of TObjString containing the name of the files need to be merged
65 TList fExcessFiles; ///<! List of TObjString containing the name of the files not yet added to fFileList due to user or system limitation on the max number of files opened.
66
67 bool fOutFileWasExplicitlyClosed = false; ///<! the user has called CloseOutputFile(), so we shouldn't error out in RecursiveRemove
68
70 virtual Bool_t AddFile(TFile *source, Bool_t own, Bool_t cpProgress);
71 virtual Bool_t MergeRecursive(TDirectory *target, TList *sourcelist, Int_t type = kRegular | kAll);
72
73 virtual Bool_t MergeOne(TDirectory *target, TList *sourcelist, Int_t type,
74 TFileMergeInfo &info, TString &oldkeyname, THashList &allNames, Bool_t &status, Bool_t &onlyListed,
75 const TString &path,
76 TDirectory *current_sourcedir, TFile *current_file,
77 TKey *key, TObject *obj, TIter &nextkey);
78public:
79 /// Type of the partial merge
81 kRegular = 0, ///< Normal merge, overwriting the output file.
82 kIncremental = BIT(1), ///< Merge the input file with the content of the output file (if already existing).
83 kResetable = BIT(2), ///< Only the objects with a MergeAfterReset member function.
84 kNonResetable = BIT(3), ///< Only the objects without a MergeAfterReset member function.
85 kDelayWrite = BIT(4), ///< Delay the TFile write (to reduce the number of write when reusing the file)
86
87 kAll = BIT(2)|BIT(3), ///< Merge all type of objects (default)
88 kAllIncremental = kIncremental | kAll, ///< Merge incrementally all type of objects.
89
90 kOnlyListed = BIT(5), ///< Only the objects specified in fObjectNames list
91 kSkipListed = BIT(6), ///< Skip objects specified in fObjectNames list
92 kKeepCompression= BIT(7) ///< Keep compression level unchanged for each input files
93 };
94
95 TFileMerger(Bool_t isLocal = kTRUE, Bool_t histoOneGo = kTRUE);
96 ~TFileMerger() override;
97
98 Int_t GetPrintLevel() const { return fPrintLevel; }
99 void SetPrintLevel(Int_t level) { fPrintLevel = level; }
101 const char *GetOutputFileName() const { return fOutputFilename; }
103 TFile *GetOutputFile() const { return fOutputFile; }
105 void SetMaxOpenedFiles(Int_t newmax);
106 const char *GetMsgPrefix() const { return fMsgPrefix; }
107 void SetMsgPrefix(const char *prefix);
108 const char *GetMergeOptions() { return fMergeOptions; }
109 void SetMergeOptions(const TString &options) { fMergeOptions = options; }
110 void SetMergeOptions(const std::string_view &options) { fMergeOptions = options; }
111 void SetIOFeatures(ROOT::TIOFeatures &features) { fIOFeatures = &features; }
112 /// Add object names for PartialMerge().
113 void AddObjectNames(const char *name) {fObjectNames += name; fObjectNames += " ";}
114 const char *GetObjectNames() const {return fObjectNames.Data();}
116 void CloseOutputFile();
117
118 //--- file management interface
119 virtual Bool_t SetCWD(const char * /*path*/) { MayNotUse("SetCWD"); return kFALSE; }
120 virtual const char *GetCWD() { MayNotUse("GetCWD"); return nullptr; }
121
122 //--- file merging interface
123 virtual void Reset();
124 virtual Bool_t AddFile(const char *url, Bool_t cpProgress = kTRUE);
125 virtual Bool_t AddFile(TFile *source, Bool_t cpProgress = kTRUE);
126 virtual Bool_t AddAdoptFile(TFile *source, Bool_t cpProgress = kTRUE);
127 virtual Bool_t OutputFile(const char *url, Bool_t force);
128 virtual Bool_t OutputFile(const char *url, Bool_t force, Int_t compressionLevel);
129 virtual Bool_t OutputFile(const char *url, const char *mode = "RECREATE");
130 virtual Bool_t OutputFile(const char *url, const char *mode, Int_t compressionLevel);
131 virtual Bool_t OutputFile(std::unique_ptr<TFile> file);
132 virtual void PrintFiles(Option_t *options);
133 virtual Bool_t Merge(Bool_t = kTRUE);
134 virtual Bool_t PartialMerge(Int_t type = kAll | kIncremental);
135 virtual void SetFastMethod(Bool_t fast=kTRUE) {fFastMethod = fast;}
136 Bool_t GetNotrees() const { return fNoTrees; }
137 virtual void SetNotrees(Bool_t notrees=kFALSE) {fNoTrees = notrees;}
138 void RecursiveRemove(TObject *obj) override;
139 /// Determines how the merging process should behave when encontering invalid objects.
140 /// By default the merging will be aborted.
141 /// \sa EErrorBehavior
142 void SetErrorBehavior(EErrorBehavior errBehavior) { fErrBehavior = errBehavior; }
143
144 ClassDefOverride(TFileMerger, 6) // File copying and merging services
145};
146
147#endif
148
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
#define BIT(n)
Definition Rtypes.h:91
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
char name[80]
Definition TGX11.cxx:148
TIOFeatures provides the end-user with the ability to change the IO behavior of data written via a TT...
Describe directory structure in memory.
Definition TDirectory.h:45
A class to pass information from the TFileMerger to the objects being merged.
virtual void SetFastMethod(Bool_t fast=kTRUE)
TString fObjectNames
List of object names to be either merged exclusively or skipped.
Definition TFileMerger.h:63
virtual Bool_t OutputFile(const char *url, Bool_t force)
Open merger output file.
TList fMergeList
list of TObjString containing the name of the files need to be merged
Definition TFileMerger.h:64
Bool_t GetNotrees() const
TFile * GetOutputFile() const
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...
virtual void PrintFiles(Option_t *options)
Print list of files being merged.
const char * GetMsgPrefix() const
Bool_t fHistoOneGo
Merger histos in one go (default is kTRUE).
Definition TFileMerger.h:62
virtual Bool_t MergeRecursive(TDirectory *target, TList *sourcelist, Int_t type=kRegular|kAll)
Merge all objects in a directory.
void RecursiveRemove(TObject *obj) override
Intercept the case where the output TFile is deleted!
TList fFileList
A list the file (TFile*) which shall be merged.
Definition TFileMerger.h:47
void SetErrorBehavior(EErrorBehavior errBehavior)
Determines how the merging process should behave when encontering invalid objects.
virtual Bool_t Merge(Bool_t=kTRUE)
Merge the files.
virtual Bool_t MergeOne(TDirectory *target, TList *sourcelist, Int_t type, TFileMergeInfo &info, TString &oldkeyname, THashList &allNames, Bool_t &status, Bool_t &onlyListed, const TString &path, TDirectory *current_sourcedir, TFile *current_file, TKey *key, TObject *obj, TIter &nextkey)
TList * GetMergeList()
TString fOutputFilename
The name of the outputfile for merging.
Definition TFileMerger.h:49
const char * GetObjectNames() const
ROOT::TIOFeatures TIOFeatures
Definition TFileMerger.h:40
const char * GetOutputFileName() const
TString fMsgPrefix
Prefix to be used when printing informational message (default TFileMerger).
Definition TFileMerger.h:57
void ClearObjectNames()
TIOFeatures * fIOFeatures
IO features to use in the output file.
Definition TFileMerger.h:56
TFileMerger(const TFileMerger &)=delete
const char * GetMergeOptions()
void SetMsgPrefix(const char *prefix)
Set the prefix to be used when printing informational message.
Int_t GetPrintLevel() const
Definition TFileMerger.h:98
Bool_t fNoTrees
True if Trees should not be merged (default is kFALSE).
Definition TFileMerger.h:51
Bool_t HasCompressionChange() const
void SetPrintLevel(Int_t level)
Definition TFileMerger.h:99
bool fOutFileWasExplicitlyClosed
! the user has called CloseOutputFile(), so we shouldn't error out in RecursiveRemove
Definition TFileMerger.h:67
void AddObjectNames(const char *name)
Add object names for PartialMerge().
EPartialMergeType
Type of the partial merge.
Definition TFileMerger.h:80
@ kAll
Merge all type of objects (default).
Definition TFileMerger.h:87
@ kIncremental
Merge the input file with the content of the output file (if already existing).
Definition TFileMerger.h:82
@ kKeepCompression
Keep compression level unchanged for each input files.
Definition TFileMerger.h:92
@ kSkipListed
Skip objects specified in fObjectNames list.
Definition TFileMerger.h:91
@ kNonResetable
Only the objects without a MergeAfterReset member function.
Definition TFileMerger.h:84
@ kResetable
Only the objects with a MergeAfterReset member function.
Definition TFileMerger.h:83
@ kOnlyListed
Only the objects specified in fObjectNames list.
Definition TFileMerger.h:90
@ kAllIncremental
Merge incrementally all type of objects.
Definition TFileMerger.h:88
@ kRegular
Normal merge, overwriting the output file.
Definition TFileMerger.h:81
@ kDelayWrite
Delay the TFile write (to reduce the number of write when reusing the file).
Definition TFileMerger.h:85
void SetMergeOptions(const TString &options)
Bool_t fExplicitCompLevel
True if the user explicitly requested a compression level change (default kFALSE).
Definition TFileMerger.h:52
Bool_t fCompressionChange
True if the output and input have different compression level (default kFALSE).
Definition TFileMerger.h:53
EErrorBehavior fErrBehavior
What to do in case of errors during merging.
Definition TFileMerger.h:58
virtual void SetNotrees(Bool_t notrees=kFALSE)
Int_t fPrintLevel
How much information to print out at run time.
Definition TFileMerger.h:54
void SetMaxOpenedFiles(Int_t newmax)
Set a limit to the number of files that TFileMerger will open simultaneously.
TString fMergeOptions
Options (in string format) to be passed down to the Merge functions.
Definition TFileMerger.h:55
void CloseOutputFile()
Closes output file.
~TFileMerger() override
Cleanup.
@ kFailOnError
The merging process will stop and yield failure when encountering invalid objects.
Definition TFileMerger.h:34
@ kSkipOnError
The merging process will skip invalid objects and continue.
Definition TFileMerger.h:36
Int_t GetMaxOpenedFiles() const
Bool_t OpenExcessFiles()
Open up to (fMaxOpenedFiles-1) of the excess files.
void SetIOFeatures(ROOT::TIOFeatures &features)
TList fExcessFiles
! List of TObjString containing the name of the files not yet added to fFileList due to user or syste...
Definition TFileMerger.h:65
virtual const char * GetCWD()
TStopwatch fWatch
Stop watch to measure file copy speed.
Definition TFileMerger.h:46
TFile * fOutputFile
The outputfile for merging.
Definition TFileMerger.h:48
void SetMergeOptions(const std::string_view &options)
virtual Bool_t PartialMerge(Int_t type=kAll|kIncremental)
Merge the files.
Bool_t fLocal
Makes local copies of merging files if True (default is kTRUE).
Definition TFileMerger.h:61
TFileMerger & operator=(const TFileMerger &)=delete
virtual void Reset()
Reset merger file list.
Int_t fMaxOpenedFiles
Maximum number of files opened at the same time by the TFileMerger.
Definition TFileMerger.h:60
virtual Bool_t SetCWD(const char *)
virtual Bool_t AddAdoptFile(TFile *source, Bool_t cpProgress=kTRUE)
Add the TFile to this file merger and give ownership of the TFile to this object (unless kFALSE is re...
Bool_t fFastMethod
True if using Fast merging algorithm (default).
Definition TFileMerger.h:50
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
THashList implements a hybrid collection class consisting of a hash table and a list to store TObject...
Definition THashList.h:34
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
A doubly linked list.
Definition TList.h:38
Mother of all ROOT objects.
Definition TObject.h:42
void MayNotUse(const char *method) const
Use this method to signal that a method (defined in a base class) may not be called in a derived clas...
Definition TObject.cxx:1160
TObject()
TObject constructor.
Definition TObject.h:259
Stopwatch class.
Definition TStopwatch.h:28
Basic string class.
Definition TString.h:138