Skip to content
Snippets Groups Projects
Commit e6ee7034 authored by Gerhard Raven's avatar Gerhard Raven
Browse files

Modernize RootCnv

   - replace trivial constructors/destructors with = default
   - prefer std::unique_ptr over raw pointers and explicit delete
   - prefer nullptr over 0
   - clean up constructors
parent d530f13c
No related branches found
No related tags found
1 merge request!7C++11 modernization changes
......@@ -17,6 +17,7 @@
#include <stdexcept>
#include <string>
#include <vector>
#include <memory>
/*
* Gaudi namespace declaration
......@@ -76,11 +77,11 @@ namespace Gaudi {
std::vector<RootRef> refs;
/// Default constructor
RootObjectRefs() {}
RootObjectRefs() = default;
/// Copy constructor
RootObjectRefs(const RootObjectRefs& r) : links(r.links), refs(r.refs) {}
/// Default destructor
~RootObjectRefs() {}
~RootObjectRefs() = default;
/// Assignment operator
RootObjectRefs& operator=(const RootObjectRefs& r) {
links = r.links;
......@@ -106,9 +107,9 @@ namespace Gaudi {
/// Class ID of the described object
unsigned long clid;
/// Standard constructor
RootNTupleDescriptor() {}
RootNTupleDescriptor() = default;
/// Standard destructor
virtual ~RootNTupleDescriptor() {}
virtual ~RootNTupleDescriptor() = default;
};
typedef int ExtractStatus;
......@@ -119,20 +120,20 @@ namespace Gaudi {
struct RootEventExtractor {
protected:
TFile* m_in;
TTree* m_evt_in;
TTree* m_ref_in;
std::unique_ptr<TFile> m_in;
TTree* m_evt_in = nullptr;
TTree* m_ref_in = nullptr;
TFile* m_out;
TTree* m_evt_out;
TTree* m_ref_out;
TFile* m_out = nullptr;
TTree* m_evt_out = nullptr;
TTree* m_ref_out = nullptr;
int m_localDB_id;
int m_localDB_id = 0;
std::vector<int> m_eventList;
public:
/// Default constructor
RootEventExtractor();
RootEventExtractor() = default;
/// Initializing constructor directly opening the input file and the output file
RootEventExtractor(const char* input, const char* output, const char* output_option);
......@@ -170,15 +171,8 @@ namespace Gaudi {
using namespace Gaudi;
using namespace std;
/// Default constructor
RootEventExtractor::RootEventExtractor()
: m_in(0), m_evt_in(0), m_ref_in(0), m_out(0), m_evt_out(0), m_ref_out(0), m_localDB_id(0)
{
}
/// Initializing constructor directly opening the input file and the output file
RootEventExtractor::RootEventExtractor(const char* input, const char* output, const char* output_option)
: m_in(0), m_evt_in(0), m_ref_in(0), m_out(0), m_evt_out(0), m_ref_out(0), m_localDB_id(0)
{
if ( EXTRACT_SUCCESS != openInput(input) ) {
throw std::runtime_error("Failed to open input file:"+std::string(input));
......@@ -210,7 +204,7 @@ ExtractStatus RootEventExtractor::select(int evt_num) {
/// Close input file
ExtractStatus RootEventExtractor::openInput(const char* name) {
if ( m_in ) closeInput();
m_in = TFile::Open(name);
m_in.reset( TFile::Open(name) );
if ( m_in && !m_in->IsZombie() ) {
m_evt_in = (TTree*)m_in->Get("Event");
m_ref_in = (TTree*)m_in->Get("Refs");
......@@ -223,7 +217,7 @@ ExtractStatus RootEventExtractor::openInput(const char* name) {
ExtractStatus RootEventExtractor::openOutput(const char* name, const char* option) {
if ( m_out ) closeOutput();
m_out = TFile::Open(name,option);
m_evt_out = m_ref_out = 0;
m_evt_out = m_ref_out = nullptr;
return EXTRACT_SUCCESS;
}
......@@ -232,11 +226,10 @@ ExtractStatus RootEventExtractor::closeInput() {
if ( m_in ) {
::printf("+++ Closing input file:%s\n",m_in->GetName());
m_in->Close();
delete m_in;
}
m_in = 0;
m_evt_in = 0;
m_ref_in = 0;
m_in.reset();
m_evt_in = nullptr;
m_ref_in = nullptr;
return EXTRACT_SUCCESS;
}
......@@ -249,9 +242,9 @@ ExtractStatus RootEventExtractor::closeOutput() {
m_out->Close();
delete m_out;
}
m_out = 0;
m_evt_out = 0;
m_ref_out = 0;
m_out = nullptr;
m_evt_out = nullptr;
m_ref_out = nullptr;
return EXTRACT_SUCCESS;
}
......@@ -268,11 +261,11 @@ ExtractStatus RootEventExtractor::extract() {
}
else {
m_evt_out = (TTree*)m_out->Get("Event");
if ( 0 == m_evt_out ) {
if ( !m_evt_out ) {
m_evt_out = m_evt_in->CloneTree(0);
}
m_ref_out = (TTree*)m_out->Get("Refs");
if ( 0 == m_ref_out ) {
if ( !m_ref_out ) {
m_ref_out = m_ref_in->CloneTree(0);
new_output = true;
}
......@@ -343,7 +336,7 @@ ExtractStatus RootEventExtractor::extract() {
TString name = br_in->GetName();
TClass* br_class = gROOT->GetClass(br_in->GetClassName(),kTRUE);
br_out = m_evt_out->GetBranch(name);
if ( 0 == br_out ) {
if ( !br_out ) {
::printf("+++ ERROR: Input and output event trees are incompatible. Selection not possible.\n");
return EXTRACT_ERROR;
}
......
......@@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include <map>
#include <memory>
// Forward declarations
class TTree;
......@@ -38,8 +39,8 @@ namespace Gaudi {
typedef std::map<std::string,ContainerSections> DatabaseSections;
DatabaseSections m_sections;
TFile* m_output;
bool m_treeSections;
std::unique_ptr<TFile> m_output;
bool m_treeSections = false;
union uuid_data {
unsigned char buf[16];
......@@ -111,8 +112,7 @@ namespace {
}
/// Standard constructor
RootDatabaseMerger::RootDatabaseMerger() : m_output(0), m_treeSections(false) {
}
RootDatabaseMerger::RootDatabaseMerger() = default;
/// Default destructor
RootDatabaseMerger::~RootDatabaseMerger() {
......@@ -137,7 +137,7 @@ MergeStatus RootDatabaseMerger::attach(const string& file_id) {
::printf("+++ Cannot attach output file %s --- file does not exist.\n",fid);
return MERGE_ERROR;
}
m_output = TFile::Open(fid,"UPDATE");
m_output.reset( TFile::Open(fid,"UPDATE") );
if ( m_output && !m_output->IsZombie() ) {
//if ( s_dbg ) ::printf("+++ Update output file %s.\n",fid);
//if ( s_dbg ) ::printf("+++ Update output file.\n");
......@@ -157,7 +157,7 @@ MergeStatus RootDatabaseMerger::create(const string& fid) {
::printf("+++ Cannot create output file %s --- file already exists.\n",fid.c_str());
return MERGE_ERROR;
}
m_output = TFile::Open(fid.c_str(),"RECREATE");
m_output.reset( TFile::Open(fid.c_str(),"RECREATE") );
if ( m_output && !m_output->IsZombie() ) {
TTree* t1 = new TTree("Sections","Root Section data");
TTree* t2 = new TTree("Refs","Root Section data");
......@@ -210,8 +210,7 @@ MergeStatus RootDatabaseMerger::close() {
m_output->Purge();
if ( s_dbg ) m_output->ls();
m_output->Close();
delete m_output;
m_output = 0;
m_output.reset();
}
return MERGE_SUCCESS;
}
......@@ -282,15 +281,14 @@ void RootDatabaseMerger::dumpSections() {
/// Merge new input to existing output
MergeStatus RootDatabaseMerger::merge(const string& fid) {
if ( m_output ) {
TFile* source = TFile::Open(fid.c_str());
std::unique_ptr<TFile> source{ TFile::Open(fid.c_str()) };
if ( source && !source->IsZombie() ) {
size_t idx = fid.rfind('/');
::printf("+++ Start merging input file:%s\n",
idx != string::npos ? fid.substr(idx+1).c_str() : fid.c_str());
if ( copyAllTrees(source) == MERGE_SUCCESS ) {
if ( copyRefs(source,"Refs") == MERGE_SUCCESS ) {
if ( copyAllTrees(source.get()) == MERGE_SUCCESS ) {
if ( copyRefs(source.get(),"Refs") == MERGE_SUCCESS ) {
source->Close();
delete source;
return MERGE_SUCCESS;
}
}
......@@ -381,7 +379,7 @@ MergeStatus RootDatabaseMerger::copyTree(TFile* source, const string& name) {
src_tree->SetEntries(1);
}
addSections(src_tree,out_tree);
if ( out_tree == 0 ) {
if ( !out_tree ) {
out_tree = src_tree->CloneTree(-1,"fast");
if ( s_dbg ) ::printf("+++ Created new Tree %s.\n",out_tree->GetName());
out_tree->Write();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment