13#include <TInterpreter.h>
19#include <TTreeCloner.h>
60 unsigned char buf[16];
71 bool exists(
const std::string& fid )
const;
100using namespace Gaudi;
104 static bool s_dbg =
true;
115 Bool_t result = gSystem->AccessPathName( fid.c_str(), kFileExists );
117 return result == kFALSE;
122 const char* fid = file_id.c_str();
124 ::printf(
"+++ Another output file %s is already open. Request denied.\n",
m_output->GetName() );
126 }
else if ( !
exists( file_id ) ) {
127 ::printf(
"+++ Cannot attach output file %s --- file does not exist.\n", fid );
130 m_output.reset( TFile::Open( fid,
"UPDATE" ) );
136 ::printf(
"+++ Failed to open new output file %s.\n", fid );
143 ::printf(
"+++ Another output file %s is already open. Request denied.\n",
m_output->GetName() );
145 }
else if (
exists( fid ) ) {
146 ::printf(
"+++ Cannot create output file %s --- file already exists.\n", fid.c_str() );
149 m_output.reset( TFile::Open( fid.c_str(),
"RECREATE" ) );
151 TTree*
t1 =
new TTree(
"Sections",
"Root Section data" );
152 TTree*
t2 =
new TTree(
"Refs",
"Root Section data" );
154 t1->Branch(
"Sections", 0,
"Sections/C" );
155 t2->Branch(
"Links", 0,
"Links/C" );
156 t2->Branch(
"Params", 0,
"Params/C" );
157 t2->Branch(
"Databases", 0,
"Databases/C" );
158 t2->Branch(
"Containers", 0,
"Containers/C" );
159 if ( s_dbg ) ::printf(
"+++ Opened new output file %s.\n", fid.c_str() );
163 ::printf(
"+++ Failed to open new output file %s.\n", fid.c_str() );
169 static const char* fmt =
"FID=%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX";
171 TTree* t = (TTree*)
m_output->Get(
"Refs" );
176 TBranch* b = t->GetBranch(
"Params" );
178 uuid.GetUUID( d.
buf );
179 sprintf( text, fmt, d.
ibuf[0], d.
sbuf[2], d.
sbuf[3], d.
buf[8], d.
buf[9], d.
buf[10], d.
buf[11], d.
buf[12],
181 b->SetAddress( text );
184 if ( s_dbg ) ::printf(
"+++ Added new GUID %s to merge file.\n", text );
189 ::printf(
"+++ Failed to add new GUID to merge file.\n" );
197 if ( s_dbg ) ::printf(
"+++ Closing merge file.\n" );
210 int nb, total = 0, nbytes = 0;
212 TTree* t = (TTree*)
m_output->Get(
"Sections" );
214 TBranch* b = t->GetBranch(
"Sections" );
216 b->SetAddress( text );
218 string cont = i.first;
219 for (
const auto& j : i.second ) {
220 ::sprintf( text,
"[CNT=%s][START=%d][LEN=%d]", cont.c_str(), j.start, j.length );
226 ::printf(
"+++ Failed to update Sections tree with new entries. [WRITE_ERROR]\n" );
229 ::sprintf( text,
"[END-OF-SECTION]" );
235 ::printf(
"+++ Failed to update Sections branch with new entries. [WRITE_ERROR]\n" );
237 if ( s_dbg ) ::printf(
"+++ Added %d Sections entries with %d bytes in total.\n", total, nbytes );
240 ::printf(
"+++ Failed to update Sections tree with new entries. [NO_OUTPUT_BRANCH]\n" );
243 ::printf(
"+++ Failed to update Sections tree with new entries. [NO_OUTPUT_TREE]\n" );
246 ::printf(
"+++ Failed to update Sections tree with new entries. [NO_OUTPUT_FILE]\n" );
254 string prefix = i.first;
255 for (
const auto& j : i.second ) {
257 ::sprintf( text,
"['%s'][%d]", prefix.c_str(), cnt++ );
259 ::printf(
"+++ section %-55s Start:%8d ... %8d [%d entries]\n", text, j.start, j.start + j.length, j.length );
268 std::unique_ptr<TFile> source{ TFile::Open( fid.c_str() ) };
269 if ( source && !source->IsZombie() ) {
270 size_t idx = fid.rfind(
'/' );
271 ::printf(
"+++ Start merging input file:%s\n",
272 idx != string::npos ? fid.substr( idx + 1 ).c_str() : fid.c_str() );
280 ::printf(
"+++ Cannot open input file:%s\n", fid.c_str() );
284 ::printf(
"+++ No valid output file present. Merge request refused for fid:%s.\n", fid.c_str() );
292 s.start = (int)( out ? out->GetEntries() : 0 );
293 s.length = (int)in->GetEntries();
298 TObjArray* a_in = in->GetListOfBranches();
299 for (
int i = 0, n = a_in->GetLast(); i < n; ++i ) {
300 TBranch* b_in = (TBranch*)a_in->At( i );
301 TBranch* b_out = out ? out->GetBranch( b_in->GetName() ) : 0;
302 if ( !out || b_out ) {
304 s.start = (int)( b_out ? b_out->GetEntries() : 0 );
305 s.length = (int)b_in->GetEntries();
309 ::printf(
"+++ Cannot merge incompatible branches:%s.\n", b_in->GetName() );
318 TBranch* s = src_tree->GetBranch( name.c_str() );
319 TBranch* o = out_tree->GetBranch( name.c_str() );
321 s->SetAddress( text );
322 o->SetAddress( text );
323 for ( Long64_t i = 0, n = s->GetEntries(); i < n; ++i ) {
334 TIter nextkey( source->GetListOfKeys() );
336 for ( TKey* key = (TKey*)nextkey(); key; key = (TKey*)nextkey() ) {
337 const char* classname = key->GetClassName();
338 TClass* cl = gROOT->GetClass( classname );
340 if ( cl->InheritsFrom(
"TTree" ) ) {
341 string name = key->GetName();
342 if ( name ==
"Refs" )
continue;
344 printf(
"+++ Copy Tree:%s %d\n", name.c_str(),
int(
m_treeSections ) );
357 TTree* src_tree = (TTree*)source->Get( name.c_str() );
359 Long64_t src_entries = src_tree->GetEntries();
360 TTree* out_tree = (TTree*)
m_output->Get( name.c_str() );
362 if ( 0 == src_tree->GetEntries() ) { src_tree->SetEntries( 1 ); }
365 out_tree = src_tree->CloneTree( -1,
"fast" );
366 if ( s_dbg ) ::printf(
"+++ Created new Tree %s.\n", out_tree->GetName() );
371 m_output->GetObject( name.c_str(), out_tree );
372 TTreeCloner cloner( src_tree, out_tree,
"fast" );
373 if ( cloner.IsValid() ) {
374 Long64_t out_entries = out_tree->GetEntries();
375 out_tree->SetEntries( out_entries + src_entries );
376 Bool_t res = cloner.Exec();
377 if ( s_dbg ) ::printf(
"+++ Merged tree: %s res=%d\n", out_tree->GetName(), res );
384 ::printf(
"+++ Got a tree where fast cloning is not possible -- operation failed.\n" );
388 Long64_t nb = out_tree->CopyEntries(src_tree,-1,
"fast");
389 Long64_t out_entries = out_tree->GetEntries();
390 out_tree->SetEntries(out_entries+src_entries);
392 if ( s_dbg ) ::printf(
"+++ Merged tree: %s res=%lld\n",out_tree->GetName(),nb);
401 TTree* src_tree = (TTree*)source->Get( name.c_str() );
403 TTree* out_tree = (TTree*)
m_output->Get( name.c_str() );
408 copyBranch( src_tree, out_tree,
"Containers" );
409 copyBranch( src_tree, out_tree,
"Databases" );
417int merge(
const char* target,
const char* source,
bool fixup =
false,
bool dbg =
true ) {
421 static bool first =
true;
424 gSystem->Load(
"libCintex");
425 gInterpreter->ProcessLine(
"Cintex::Enable()");
433 MergeStatus ret = m.exists( target ) ? m.attach( target ) : m.create( target );
435 ret = m.merge( source );
438 if ( fixup ) m.createFID();
443 ::printf(
"+++ Cannot open output file:%s\n", target );
TemplatedAlg< int, std::vector< std::string > > t1
TemplatedAlg< double, bool > t2
MergeStatus attach(const std::string &fid)
Attach to existing output file for further merging.
MergeStatus addSections(TTree *in, TTree *out)
Add section information for the next merge step.
RootDatabaseMerger()
Standard constructor.
MergeStatus createFID()
Create and add new FID to the newly merged file.
MergeStatus close()
Close output file.
MergeStatus copyRefs(TFile *source, const std::string &name)
Copy one single tree from the input file to the output file.
DatabaseSections m_sections
MergeStatus create(const std::string &fid)
Create new output file.
std::vector< ContainerSection > ContainerSections
MergeStatus copyAllTrees(TFile *source)
Copy all data trees from the input file to the output file.
MergeStatus copyBranch(TTree *src_tree, TTree *out_tree, const std::string &name)
Copy single reference branch.
virtual ~RootDatabaseMerger()
Default destructor.
std::map< std::string, ContainerSections > DatabaseSections
std::unique_ptr< TFile > m_output
MergeStatus copyTree(TFile *source, const std::string &name)
Copy one single tree from the input file to the output file.
MergeStatus saveSections()
Save new sections to the output file.
void dumpSections()
Dump collected database sections.
MergeStatus merge(const std::string &fid)
Merge new input to existing output.
bool exists(const std::string &fid) const
Check if a database exists.
int merge(const char *target, const char *source, bool fixup=false, bool dbg=true)
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...