Skip to content

Commit

Permalink
Separate sub-TDirectories for plugin outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Mendenhall committed Mar 1, 2016
1 parent d49f589 commit 885023e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 70 deletions.
38 changes: 22 additions & 16 deletions ROOTUtils/OutputManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@

#include "OutputManager.hh"
#include "PathUtils.hh"
#include <TFile.h>
#include <TDirectory.h>
#include <TH1.h>
#include <TStyle.h>
#include <cassert>

bool OutputManager::squelchAllPrinting = false;

OutputManager::OutputManager(string nm, string bp):
rootOut(NULL), defaultCanvas(new TCanvas()),
defaultCanvas(new TCanvas()),
parent(NULL), name(nm) {
TH1::AddDirectory(kFALSE);
// set up output canvas
Expand All @@ -39,47 +42,50 @@ parent(NULL), name(nm) {
}

OutputManager::OutputManager(string nm, OutputManager* pnt):
rootOut(NULL), defaultCanvas(NULL), parent(pnt), name(nm) {
defaultCanvas(NULL), parent(pnt), name(nm) {
TH1::AddDirectory(kFALSE);
if(parent) {
defaultCanvas = parent->defaultCanvas;
plotPath = dataPath = basePath = rootPath = parent->basePath+"/"+name+"/";
}
}

void OutputManager::openOutfile() {
if(rootOut) { rootOut->Close(); }
makePath(rootPath);
rootOut = new TFile((rootPath+"/"+name+".root").c_str(),"RECREATE");
rootOut->cd();
TDirectory* OutputManager::writeItems(TDirectory* d) {
if(!d) d = gDirectory;
else d = d->mkdir(name.c_str());
assert(d);
return TObjCollector::writeItems(d);
}

void OutputManager::writeROOT() {
void OutputManager::writeROOT(TDirectory* parentDir) {
printf("\n--------- Building output .root file... ----------\n");
if(!rootOut) openOutfile();
rootOut->cd();
writeItems();
TFile* rootOut = NULL;
if(parentDir) writeItems(parentDir);
else {
makePath(rootPath);
string outfname = rootPath+"/"+name+".root";
printf("Writing to '%s'\n", outfname.c_str());
rootOut = new TFile(outfname.c_str(),"RECREATE");
rootOut->cd();
writeItems(NULL);
}
clearItems();
rootOut->Close();
rootOut = NULL;
delete rootOut;
printf("--------- Done. ----------\n");
}


TH1F* OutputManager::registeredTH1F(string hname, string htitle, unsigned int nbins, float x0, float x1) {
if(rootOut) rootOut->cd();
TH1F* h = new TH1F(hname.c_str(),htitle.c_str(),nbins,x0,x1);
return (TH1F*)addObject(h);
}

TH1D* OutputManager::registeredTH1D(string hname, string htitle, unsigned int nbins, float x0, float x1) {
if(rootOut) rootOut->cd();
TH1D* h = new TH1D(hname.c_str(),htitle.c_str(),nbins,x0,x1);
return (TH1D*)addObject(h);
}

TH2F* OutputManager::registeredTH2F(string hname, string htitle, unsigned int nbinsx, float x0, float x1, unsigned int nbinsy, float y0, float y1) {
if(rootOut) rootOut->cd();
return (TH2F*)addObject(new TH2F(hname.c_str(),htitle.c_str(),nbinsx,x0,x1,nbinsy,y0,y1));
}

Expand Down
26 changes: 12 additions & 14 deletions ROOTUtils/OutputManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public:
/// destructor
virtual ~OutputManager() {
clearItems();
if(rootOut) rootOut->Close();
if(!parent) delete defaultCanvas;
}

Expand All @@ -59,20 +58,19 @@ public:
TH2F* registeredTH2F(string hname, string htitle, unsigned int nbinsx, float x0, float x1, unsigned int nbinsy, float y0, float y1);
/// print current canvas
virtual void printCanvas(string fname, string suffix=".pdf") const;

/// open output ROOT file for writing
virtual void openOutfile();
/// write output ROOT file; WARNING: THIS DELETES ALL REGISTERED ITEMS; do last if you reference these!
void writeROOT();

TFile* rootOut; ///< ROOT file output
TCanvas* defaultCanvas; ///< canvas for drawing plots
OutputManager* parent; ///< parent output manager
string basePath; ///< general output path
string plotPath; ///< specific output path for plots
string dataPath; ///< specific output path for output data
string rootPath; ///< specific output path for ROOT files
string name; ///< name for this subsystem
/// write items to currently open directory, or specified
TDirectory* writeItems(TDirectory* d = NULL) override;
/// write output ROOT file, or to new directory within parent; WARNING: THIS DELETES ALL REGISTERED ITEMS; do last if you reference these!
void writeROOT(TDirectory* parentDir = NULL);

TCanvas* defaultCanvas; ///< canvas for drawing plots
OutputManager* parent = NULL; ///< parent output manager
string basePath; ///< general output path
string plotPath; ///< specific output path for plots
string dataPath; ///< specific output path for output data
string rootPath; ///< specific output path for ROOT files
string name; ///< name for this subsystem

static bool squelchAllPrinting; ///< whether to cancel all printCanvas output
};
Expand Down
7 changes: 4 additions & 3 deletions ROOTUtils/PluginSaver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,13 @@ void PluginSaver::calculateResults() {
kv.second->thePlugin->calculateResults();
}

void PluginSaver::writeItems() {
SegmentSaver::writeItems();
TDirectory* PluginSaver::writeItems(TDirectory* d) {
d = SegmentSaver::writeItems(d);
printf("Writing plugins '%s'\n", filePlugins->String().Data());
for(auto& kv: myBuilders)
if(kv.second->thePlugin)
kv.second->thePlugin->writeItems();
kv.second->thePlugin->writeItems(d);
return d;
}

void PluginSaver::clearItems() {
Expand Down
20 changes: 10 additions & 10 deletions ROOTUtils/PluginSaver.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ public:
SegmentSaver* getPlugin(const string& nm) const;

/// zero out all saved histograms
virtual void zeroSavedHists() override;
void zeroSavedHists() override;
/// scale all saved histograms by a factor
virtual void scaleData(double s) override;
void scaleData(double s) override;
/// perform normalization on all histograms (e.g. conversion to differential rates); should only be done once!
virtual void normalize() override;
void normalize() override;
/// add histograms from another SegmentSaver of the same type
virtual void addSegment(const SegmentSaver& S, double sc = 1.) override;
void addSegment(const SegmentSaver& S, double sc = 1.) override;
/// virtual routine for generating output plots
virtual void makePlots() override;
void makePlots() override;
/// virtual routine for generating calculated hists
virtual void calculateResults() override;
void calculateResults() override;
/// virtual routine for comparing to other analyzers (of this type or NULL; meaning implementation-dependent)
virtual void compare(const vector<SegmentSaver*>& v) override;
void compare(const vector<SegmentSaver*>& v) override;

/// write items to file
virtual void writeItems() override;
/// write items to current directory or subdirectory of provided
TDirectory* writeItems(TDirectory* d = NULL) override;
/// clear (delete) items
virtual void clearItems() override;
void clearItems() override;

protected:
/// build plugins appropriate for input file; call in subclass after setting up myBuilders
Expand Down
44 changes: 24 additions & 20 deletions ROOTUtils/SegmentSaver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,37 @@
#include "SMExcept.hh"
#include <TString.h>

SegmentSaver::SegmentSaver(OutputManager* pnt, const string& nm, const string& inflName):
OutputManager(nm,pnt), ignoreMissingHistos(false), inflname(inflName), isCalculated(false), inflAge(0) {
// open file to load existing data
fIn = (inflname.size())?(new TFile(inflname.c_str(),"READ")) : NULL;
smassert(!fIn || !fIn->IsZombie(),"unreadable_file");
if(fIn) {
dirIn = fIn->GetDirectory("");
inflAge = fileAge(inflname);
printf("Loading data from %s [%.1f hours old]...\n",inflname.c_str(),inflAge/3600.);
}
auto PSS = dynamic_cast<SegmentSaver*>(pnt);
if(PSS && PSS->dirIn) dirIn = PSS->dirIn->GetDirectory(nm.c_str());
}

SegmentSaver::~SegmentSaver() {
if(fIn) {
fIn->Close();
delete fIn;
}
}

void resetZaxis(TH1* o) {
TObject* a = o->GetListOfFunctions()->FindObject("palette");
if(a) o->GetListOfFunctions()->Remove(a);
}

TObject* SegmentSaver::tryLoad(const string& oname) {
if(!fIn) return NULL;
if(!fIn && !dirIn) return NULL;
TObject* o = NULL;
fIn->GetObject(oname.c_str(),o);
if(dirIn) dirIn->GetObject(oname.c_str(),o); // first try in my directory
if(!o && fIn) fIn->GetObject(oname.c_str(),o); // fall back to file base for backwards compatibility
if(!o) {
if(ignoreMissingHistos) {
printf("Warning: missing object '%s' in '%s'\n",oname.c_str(),inflname.c_str());
Expand Down Expand Up @@ -102,24 +124,6 @@ TCumulative* SegmentSaver::registerCumulative(const string& onm, const TCumulati
return c;
}

SegmentSaver::SegmentSaver(OutputManager* pnt, const string& nm, const string& inflName):
OutputManager(nm,pnt), ignoreMissingHistos(false), inflname(inflName), isCalculated(false), inflAge(0) {
// open file to load existing data
fIn = (inflname.size())?(new TFile(inflname.c_str(),"READ")):NULL;
smassert(!fIn || !fIn->IsZombie(),"unreadable_file");
if(fIn) {
inflAge = fileAge(inflname);
printf("Loading data from %s [%.1f hours old]...\n",inflname.c_str(),inflAge/3600.);
}
}

SegmentSaver::~SegmentSaver() {
if(fIn) {
fIn->Close();
delete fIn;
}
}

TH1* SegmentSaver::getSavedHist(const string& hname) {
auto it = saveHists.find(hname);
smassert(it != saveHists.end());
Expand Down
1 change: 1 addition & 0 deletions ROOTUtils/SegmentSaver.hh
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public:
virtual void compare(const vector<SegmentSaver*>&) { }

TFile* fIn; ///< input file to read in histograms from
TDirectory* dirIn = NULL; ///< particular sub-directory for reading histograms
string inflname; ///< where to look for input file
bool isCalculated; ///< flag for whether calculation step has been completed

Expand Down
9 changes: 5 additions & 4 deletions ROOTUtils/TObjCollector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
#include "TObjCollector.hh"
#include <cassert>

void TObjCollector::writeItems() {
//printf("Saving registered objects...");
//fflush(stdout);
TDirectory* TObjCollector::writeItems(TDirectory* d) {
if(!d) d = gDirectory;
assert(d);
d->cd();
for(auto i: namedItems) i->Write();
for(auto const& kv: anonItems) kv.second->Write(kv.first.c_str());
//printf(" Done.\n");
return d;
}

void TObjCollector::clearItems() {
Expand Down
6 changes: 3 additions & 3 deletions ROOTUtils/TObjCollector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#ifndef TOBJCOLLECTOR_HH
#define TOBJCOLLECTOR_HH

#include <TFile.h>
#include <TObject.h>
#include <TNamed.h>
#include <TDirectory.h>

#include <string>
using std::string;
Expand All @@ -25,8 +25,8 @@ public:
/// destructor
virtual ~TObjCollector() { clearItems(); }

/// write items to file
virtual void writeItems();
/// write items to currently open directory, or specified; return directory written to
virtual TDirectory* writeItems(TDirectory* d = NULL);
/// clear (delete) items
virtual void clearItems();
/// register a named ROOT object for output (and eventual deletion)
Expand Down

0 comments on commit 885023e

Please sign in to comment.