Berkeley Nuclear Data Software
PostProcBase.hpp
Go to the documentation of this file.
1 //project
2 #include "PostProcBase.h"
3 //root
4 #include "TFile.h"
5 
6 //c++
7 #include <algorithm>
8 #include <iostream>
9 #include <sstream>
10 #include <string>
11 #include <deque>
12 #include <utility>
13 using std::cout;
14 using std::endl;
15 using std::stringstream;
16 using std::to_string;
17 using std::deque;
18 template <class T>
20 ://allocator list
21 m_trees(nullptr),
22 m_treeName(""),
23 m_branchName(""),
24 m_currentEvent(nullptr),
25 m_hasFile(false)
26 {
27 //nothing to do
28 }
29 template <class T>
30 int PostProcBase<T>::addFile(string a_filename)
31 {
32  //check if an attempt to load the file has already happened
33  auto testIt = std::find(m_loadedFiles.begin(),m_loadedFiles.end(),a_filename);
34  //if it hasnt been loaded we should load it
35  if(testIt == m_loadedFiles.end())
36  {
37  //if a file has already been loaded the work is easy
38  if(m_hasFile)
39  {
40  int success = m_trees->AddFile(a_filename.c_str(),-1);
41  if(!success)
42  {
43  cout << "Adding an additional file to the TChain failed";
44  cout << " returning without doing anyting " << endl;
45  return 1;
46  }
47  else
48  {
49  //return success
50  m_loadedFiles.push_back(a_filename);
51  return 0;
52  }
53  }
54  //if no file has been loaded memory needs to be allocated and
55  //branches need to be assigned
56  else
57  {
58  setTreeName();
59  setBranchName();
60  cout << "creating event memory";
61  m_currentEvent = new T();
62  cout << " and tchain";
63  m_trees = new TChain(m_treeName.c_str());
64  cout << " with succes";
65  cout << endl;
66  int success = m_trees->Add(a_filename.c_str(),-1);
67  if(!success)
68  {
69  cout << "Adding the first file to the TChain failed";
70  cout << " returning without doing anyting " << endl;
71  return 1;
72  }
73  else
74  {
75  cout << "associating branch address";
76  m_trees->SetBranchAddress(m_branchName.c_str(),&m_currentEvent);
77  cout << " with sucess" << endl;
78  m_hasFile = true;
79  m_loadedFiles.push_back(a_filename);
80  return 0;
81  }
82  }
83  }
84  //if it has been loaded we should inform of the duplicate call
85  else
86  {
87  cout << "Attempt to load an already loaded file. Was this intentional";
88  cout << endl;
89  return 1;
90  }
91 //how did we get here
92  cout << "oddly reached end of add file without a hitting a return" <<endl;
93  return 1;
94 }
95 
96 template <class T>
98  string a_ext
99  )
100 {
101  std::vector<std::string> files;
102  files = EZFileSystem::getFileListWithExt(a_dir,a_ext);
103  std::cout << "Adding " << files.size() << " files";
104  std::cout << std::endl;
105  for(auto && file : files)
106  {
107  addFile(file);
108  }
109  return 0;
110 }
111 
112 
113 template <class T>
114 int PostProcBase<T>::loadEvent(int a_eventNumber)
115 {
116  if(fileNotLoaded())
117  {
118  return 1;
119  }
120  m_trees->GetEntry(a_eventNumber);
121  return 0;
122 }
123 template <class T>
125 {
126  if(!m_hasFile)
127  {
128  cout << "no file to test is loaded" <<endl;
129  return 1;
130  }
131  return 0;
132 }
133 template <class T>
134 std::ostream& PostProcBase<T>::
135  printState(std::ostream& a_stream)
136 {
137  a_stream << "PostProcBase ";
138  if(m_hasFile)
139  {
140  a_stream << " has " << m_loadedFiles.size() << " file present";
141  a_stream << std::endl;
142  a_stream << "Files: ";
143  for(auto & file : m_loadedFiles)
144  {
145  a_stream << file << ", ";
146  }
147  a_stream << std::endl;
148  }
149  else
150  {
151  a_stream << " doesn't have any files loaded and won't be particularly";
152  a_stream << " useful" << std::endl;
153  }
154  return a_stream;
155 }
156 
159 template <class T>
161 {
162  if(m_hasFile)
163  {
164  delete m_trees;
165  delete m_currentEvent;
166  m_loadedFiles.clear();
167  m_hasFile = false;
168  return 1;
169  }
170  else
171  {
172  std::cout << "I haven't loaded a file so clearing state is meaningless";
173  std::cout << std::endl;
174  return 0;
175  }
176  return 0;
177 }
178 
179 template <class T> PostProcBase<T>::~PostProcBase()
180 {
181  delete m_currentEvent;
182  delete m_trees;
183 }
184 
185 template <class T>
187 {
188  return m_trees;
189 }
190 
191 
static std::vector< std::string > getFileListWithExt(std::string a_directory, std::string a_ext)
Definition: EZFileSystem.cpp:38
virtual int addFilesFromDirectory(string a_dir, string a_ext)
Definition: PostProcBase.hpp:97
PostProcBase()
defualt constructor puts class into a working state
Definition: PostProcBase.hpp:19
virtual int loadEvent(int a_eventNumber)
loads a specified event into memory
Definition: PostProcBase.hpp:114
virtual int clearFileState()
Definition: PostProcBase.hpp:160
virtual std::ostream & printState(std::ostream &a_stream=std::cout)
Definition: PostProcBase.hpp:135
virtual int fileNotLoaded()
tests if a file is loaded and prinst a message if not
Definition: PostProcBase.hpp:124
TChain * getTrees()
danger danger, use this if you know what you are doing.
Definition: PostProcBase.hpp:186
virtual ~PostProcBase()
Definition: PostProcBase.hpp:179
virtual int addFile(string a_filename)
Definition: PostProcBase.hpp:30