travex
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RootFile.cxx
Go to the documentation of this file.
1 #include <boost/filesystem.hpp>
2 
3 #include "travex/RootFile.h"
4 
5 #include "TSystem.h"
6 #include "TROOT.h"
7 
8 #include "travex/config.h"
9 #include "travex/HistContainer.h"
10 #include "travex/ProgramOptions.h"
11 
12 
13 using namespace tvx;
14 
15 
16 RootFile::RootFile(ProgramOptions& prgOpts, Option_t *option, const char *ftitle, Int_t compress) :
17  TFile(prgOpts.GetOutFileName().c_str(), option, ftitle, compress),
18  fDirs(),
19  fPrgOptions(prgOpts)
20 {
21  Info("RootFile", "Created ROOT file: %s", GetName());
22 
23  std::string macroPath = std::string(gROOT->GetMacroPath()) + ":" + gTravexMacrosPath;
24  gROOT->SetMacroPath(macroPath.c_str());
25  gROOT->Macro("style_hists.C");
26 }
27 
28 
34 {
35  for (const std::pair<std::string, HistContainer*>& subDir : fDirs)
36  {
37  std::string dirName = subDir.first;
38  HistContainer *container = subDir.second;
39 
40  if (!container) {
41  Error("FillDerivedHists", "No container/directory found for key %s. Skipping...", dirName.c_str());
42  continue;
43  }
44 
45  container->cd();
46  container->FillDerivedHists();
47  }
48 }
49 
50 
51 void RootFile::Close(Option_t *option)
52 {
53  if (fPrgOptions.SaveGraphics()) {
55  }
56 
57  TFile::Close(option);
58 }
59 
60 
61 void RootFile::SaveAllAs(std::string prefix)
62 {
63  gROOT->Macro("style_hists.C");
64 
65  namespace fs = boost::filesystem;
66 
67  if (fs::create_directories(prefix))
68  Info("SaveAllAs", "Created directory: %s", prefix.c_str());
69  else
70  Warning("SaveAllAs", "Perhaps directory already exists: %s", prefix.c_str());
71 
72  for (const std::pair<std::string, HistContainer*>& subDir : fDirs)
73  {
74  std::string dirName = subDir.first;
75  HistContainer *container = subDir.second;
76 
77  if (!container) {
78  Error("SaveAllAs", "No container/directory found for key %s. Skipping...", dirName.c_str());
79  continue;
80  }
81 
82  std::string path = prefix + "/" + dirName;
83 
84  if (gSystem->mkdir(path.c_str()) < 0)
85  Warning("SaveAllAs", "Perhaps directory already exists: %s", path.c_str());
86  else
87  Info("SaveAllAs", "Created directory: %s", path.c_str());
88 
89  container->SaveAllAs(path);
90  }
91 }
virtual void FillDerivedHists()
A user implementation of this method can be called when additional histograms need to be built from t...
Definition: HistContainer.h:83
RootFile(ProgramOptions &prgOpts, Option_t *option="", const char *ftitle="", Int_t compress=1)
Definition: RootFile.cxx:16
bool SaveGraphics() const
Definition: Event.h:12
Processes and controls user options provided in the command line.
std::string GetOutPrefix() const
A light container to hold and manipulate user histograms.
Definition: HistContainer.h:23
void SaveAllAs(std::string prefix="./")
Definition: RootFile.cxx:61
HistContainers fDirs
A string-to-HistContainer map for convenient access to enclosed directories.
Definition: RootFile.h:40
void FillDerivedHists()
For each histogram container calls the method of the same name in order to produce new histograms fro...
Definition: RootFile.cxx:33
ProgramOptions & fPrgOptions
Command line arguments and options requested by the user.
Definition: RootFile.h:43
void SaveAllAs(std::string prefix="./", std::string img_format="png")
Saves all histograms from the container as png images in the prefix directory.
virtual void Close(Option_t *option="")
Definition: RootFile.cxx:51