Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New metadata class to merge TRestRun files #382

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions macros/REST_MergeFiles.C
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
#include "TFileMerger.h"
#include "TRestTask.h"

#ifndef RESTTask_MergeFiles
#define RESTTask_MergeFiles
#include "TRestRunMerger.h"

//*******************************************************************************************************
//***
//*** Your HELP is needed to verify, validate and document this macro
//*** This macro might need update/revision.
//*** Updated REST_MergeFiles macro with the new TRestRunMerger class
//*** See TRestRunMerger for more details about this implementation
//***
//*******************************************************************************************************
Int_t REST_MergeFiles(TString pathAndPattern, TString outputFilename) {
vector<string> files = TRestTools::GetFilesMatchingPattern((string)pathAndPattern);
TFileMerger* m = new TFileMerger(false);
m->OutputFile(outputFilename);
for (auto f : files) {
m->AddFile(f.c_str());
}
int a = m->Merge();
delete m;
return a;

// TRestRunMerger *runMerger = new TRestRunMerger( pathAndPattern );

// runMerger->MergeFiles( outputFilename );

// delete runMerger;
TRestRunMerger merger;

// return 0;
return merger.MergeFiles(files, std::string(outputFilename.Data()));
}
#endif
1 change: 1 addition & 0 deletions source/framework/core/inc/TRestRun.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class TRestRun : public TRestMetadata {
TFile* MergeToOutputFile(std::vector<std::string> filefullnames, std::string outputfilename = "");
TFile* FormOutputFile();
TFile* UpdateOutputFile();
TFile* OpenAndUpdateOutputFile();

void PassOutputFile() {
fOutputFile = fInputFile;
Expand Down
54 changes: 54 additions & 0 deletions source/framework/core/inc/TRestRunMerger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*************************************************************************
* This file is part of the REST software framework. *
* *
* Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
* For more information see https://gifna.unizar.es/trex *
* *
* REST is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* REST is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have a copy of the GNU General Public License along with *
* REST in $REST_PATH/LICENSE. *
* If not, see https://www.gnu.org/licenses/. *
* For the list of contributors see $REST_PATH/CREDITS. *
*************************************************************************/

#ifndef REST_TRestRunMerger
#define REST_TRestRunMerger

#include "TRestMetadata.h"

/// This class is meant to merge different TRestRun files
class TRestRunMerger : public TRestMetadata {
private:
// Input file patternt to match
TString fInputFilePattern = "";
// Name of the output file
TString fOutputFileName = "";
// Remove input files that are merged
Bool_t fRemoveInputFiles = false;

void Initialize() override;
void InitFromConfigFile() override;

public:
void PrintMetadata() override;

void MergeFiles();
Int_t MergeFiles(const std::vector<std::string>& files, std::string outputFileName,
bool removeInputFiles = false);

TRestRunMerger();
TRestRunMerger(const char* configFilename, std::string name = "");
~TRestRunMerger();

ClassDefOverride(TRestRunMerger, 1);
};
#endif
20 changes: 13 additions & 7 deletions source/framework/core/src/TRestRun.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -980,24 +980,24 @@ TString TRestRun::FormFormat(const TString& FilenameFormat) {
TFile* TRestRun::MergeToOutputFile(vector<string> filenames, string outputfilename) {
RESTDebug << "TRestRun::FormOutputFile. target : " << outputfilename << RESTendl;
string filename;
TFileMerger* m = new TFileMerger(false);
TFileMerger m(false);
if (outputfilename == "") {
filename = fOutputFileName;
RESTInfo << "Creating file : " << filename << RESTendl;
m->OutputFile(filename.c_str(), "RECREATE");
m.OutputFile(filename.c_str(), "RECREATE");
} else {
filename = outputfilename;
RESTInfo << "Creating file : " << filename << RESTendl;
m->OutputFile(filename.c_str(), "UPDATE");
m.OutputFile(filename.c_str(), "UPDATE");
}

RESTDebug << "TRestRun::FormOutputFile. Starting to add files" << RESTendl;

for (unsigned int i = 0; i < filenames.size(); i++) {
m->AddFile(filenames[i].c_str(), false);
m.AddFile(filenames[i].c_str(), false);
}

if (m->Merge()) {
if (m.Merge()) {
for (unsigned int i = 0; i < filenames.size(); i++) {
remove(filenames[i].c_str());
}
Expand All @@ -1007,8 +1007,6 @@ TFile* TRestRun::MergeToOutputFile(vector<string> filenames, string outputfilena
exit(1);
}

delete m;

// we rename the created output file
fOutputFileName = FormFormat(filename);
rename(filename.c_str(), fOutputFileName);
Expand Down Expand Up @@ -1075,6 +1073,14 @@ TFile* TRestRun::UpdateOutputFile() {
return nullptr;
}

TFile* TRestRun::OpenAndUpdateOutputFile() {
if (fOutputFile == nullptr) {
fOutputFile = TFile::Open(fOutputFileName, "UPDATE");
}

return UpdateOutputFile();
}

///////////////////////////////////////////////
/// \brief Write this object into TFile and add a new entry in database
///
Expand Down
Loading