HighLAND
InputManager.cxx
1 #include "InputManager.hxx"
2 #include "CoreUtils.hxx"
3 #include <fstream>
4 
5 //********************************************************************
7 //********************************************************************
8 
9  _CorrectedSpill = NULL;
10  _Spill = NULL;
11  _prevSEvent = NULL;
12  _selected = NULL;
13  _IsMC = true;
14  _uniqueID = 0;
15 }
16 
17 //********************************************************************
19 //********************************************************************
20 
21  _CorrectedSpill = NULL;
22  _Spill = NULL;
23  _prevSEvent = NULL;
24  _IsMC = true;
25  _uniqueID = 0;
26 
27  for (std::vector<InputConverter*>::const_iterator it = _converters.begin(); it != _converters.end(); it++) {
28  (*it)->Reset();
29  }
30 
31 }
32 
33 //*****************************************************************************
35 //*****************************************************************************
36 
37  if (_CorrectedSpill)
38  delete _CorrectedSpill;
39 
40  if (_Spill)
41  delete _Spill;
42 
43  _CorrectedSpill=NULL;
44  _Spill=NULL;
45 
46 }
47 
48 //********************************************************************
49 bool InputManager::AddDataFileToConverter(const std::string& infile_name, const std::string& conv, const bool isCosmic, bool reset){
50 //********************************************************************
51 
52  // Check wehther this is a root file
53  bool isROOTFile = IsROOTFile(infile_name);
54 
55  // Find out the input format (FlatTre, oaAnalysisTree, etc)
56  std::string conv2 = conv;
57  if (conv2 == "") {
58  std::cout << "InputManager::Initialize(). Input type (oaAnalysisTree, FlatTree, ...) not specified. Find it automatically..." << std::endl;
59  std::string firstFile = FindFirstFile(infile_name, isROOTFile);
60  for (std::vector<InputConverter*>::const_iterator it = _converters.begin(); it != _converters.end(); it++) {
61  if ((*it)->IsCorrectType(firstFile)) {
62  conv2 = (*it)->Name();
63  break;
64  }
65  }
66  }
67 
68  if (conv2 == "") {
69  std::cerr << "ERROR: Unable to find an input converter for your input files - check that your input files are valid!" << std::endl;
70  exit(EXIT_FAILURE);
71  }
72 
73  if (!HasConverter(conv2)) return false;
74 
75  std::cout << "InputManager::Initialize(). Input type is: " << conv2 << std::endl;
76 
77  // Select the specified converter
78  SelectConverter(conv2);
79 
80  // Reset the trees in the TChain and the number of entries
81  if (reset) GetConverter().Reset();
82 
83  // Initialize the converter. Define tree branches, etc
84  if (!GetConverter().Initialize()) return false;
85  GetConverter().SetCosmicMode(isCosmic);
86 
87  // Read the input file
88  if (!ReadFile(infile_name, isROOTFile)) return false;
89 
90  // Get the number of entries in the tree
91  Long64_t nentries = GetEntries();
92  std::cout << "InputManager::input tree has " << nentries << " entries" << std::endl;
93 
94  return true;
95 }
96 
97 //********************************************************************
98 bool InputManager::Initialize(const std::string& infile_name, const std::string& conv, const bool isCosmic){
99 //********************************************************************
100 
101  if (!AddDataFileToConverter(infile_name, conv, isCosmic, true)) return false;
102 
103  // Check whether is data or MC
104  if(GetIsMC())
105  std::cout << "InputManager:: MC mode" << std::endl;
106  else
107  std::cout << "InputManager:: Data mode" << std::endl;
108 
109  // SoftwareVersion
110  std::cout << "InputManager:: SoftwareVersion for original file is " << GetSoftwareVersion() << std::endl;
111 
112  return true;
113 }
114 
115 //*****************************************************************************
116 bool InputManager::IsROOTFile(const std::string& infile_name){
117 //*****************************************************************************
118 
119  std::cout << "InputManager::IsROOTFile(). If this is not a ROOT file an error will appear in the next two lines. Ignore it !!!! " << std::endl;
120 
121  TString infileName(infile_name);
122  //std::ifstream inputFile(infileName.Data(), std::ios::in);
123 
124  //if(!inputFile){
125  //std::cerr << "Cannot open input file '" << infileName.Data() << "'. Exit!" << std::endl;
126  //return false;
127  //}
128 
129  TFile* isROOTFile = TFile::Open(infile_name.c_str());
130  bool isroo=true;
131  if (!isROOTFile){
132  isroo=false;
133  std::cout << "InputManager::IsROOTFile(). This is not a ROOT file. Try as a list of ROOT files. " << std::endl;
134  return isroo;
135  }
136  isROOTFile->Close();
137  std::cout << "InputManager::IsROOTFile(). This is a ROOT file. " << std::endl;
138  return isroo;//isROOTFile;
139 }
140 
141 //*****************************************************************************
142 bool InputManager::ReadFile(const std::string& infile_name, bool isROOTFile){
143 //*****************************************************************************
144 
145  // Load data files
146  TString infileName(infile_name);
147  std::ifstream inputFile(infileName.Data(), std::ios::in);
148 
149  //if(!inputFile){
150  //std::cerr << "Cannot open input file '" << infileName.Data() << "'. Exit!" << std::endl;
151  //return false;
152  //}
153 
154  //Loop over the input files
155  std::string inputString=infile_name;
156 
157  if (!isROOTFile){
158  while (inputFile >> inputString) {
159  if (!GetConverter().AddFileToTChain(inputString)) return false;
160  }
161  }
162  else{
163  if (!GetConverter().AddFileToTChain(inputString)) return false;
164  }
165 
166  return true;
167 }
168 
169 //*****************************************************************************
170 std::string InputManager::FindFirstFile(const std::string& infile_name, bool isROOTFile){
171 //*****************************************************************************
172 
173  TString infileName(infile_name);
174  std::ifstream inputFile(infileName.Data(), std::ios::in);
175 
176  //if(!inputFile){
177  //std::cerr << "Cannot open input file '" << infileName.Data() << "'. Exit!" << std::endl;
178  //std::string fal = "false";
179  //return fal;
180  //}
181 
182  std::string inputString=infile_name;
183 
184  if (!isROOTFile)
185  inputFile >> inputString;
186 
187  return inputString;
188 }
189 
190 //*****************************************************************************
191 bool InputManager::LoadSpill(Long64_t& entry){
192 //*****************************************************************************
193 
194  Long64_t ientry = LoadTree(entry);
195  if (ientry < 0) return false;
196 
197  // The spill to be read
198  AnaSpillC* SpillToRead = NULL;
199 
200  // Fill the spill structure from the input tree. The SpillToRead instance is created internally by the converter
201  Int_t nb = GetConverter().GetSpill(entry,SpillToRead);
202  if (nb <=0 || !SpillToRead){
203  return false;
204  }
205 
206  // If the spill is succesfully read the previous successfully read spill must be deleted
207  DeleteSpill();
208 
209  // We can now associate the spill just read with the _CorrectedSpill
210  _CorrectedSpill = SpillToRead;
211 
212  return true;
213 }
214 
215 //*****************************************************************************
216 bool InputManager::LoadEvent(Long64_t& entry, bool delPrevious){
217 //*****************************************************************************
218 
219  Long64_t ientry = LoadTree(entry);
220  if (ientry < 0) return false;
221 
222  // Empty pointer filled inside GetEvent method
223  AnaEventC* RawEvent = NULL;
224 
225  // Fill the event structure from the input tree
226  Int_t nb = GetConverter().GetEvent(entry,RawEvent);
227  if (nb <=0 ) return false;
228 
229  // Set the event unique ID
230  RawEvent->UniqueID = _uniqueID++;
231 
232  // Create a AnaSuperEvent
233  _sevent = new AnaSuperEventB(RawEvent);
234 
235  // Delete the previous event
236  if (_prevSEvent && delPrevious){
237  delete _prevSEvent;
238  }
239 
241 
242  return true;
243 }
244 
245 //*****************************************************************************
246 Long64_t InputManager::LoadTree(Long64_t entry){
247 //*****************************************************************************
248 
249  return GetConverter().LoadTree(entry);
250 }
251 
252 //*****************************************************************************
254 //*****************************************************************************
255 
256  // return the number of entries
257  return GetConverter().GetEntries();
258 }
259 
260 //********************************************************************
261 void InputManager::SelectConverter(const std::string& name) {
262 //********************************************************************
263 
264  for (std::vector<InputConverter*>::iterator it = _converters.begin(); it != _converters.end(); it++) {
265  if ((*it)->Name() == name){
266  _selected = *it;
267  break;
268  }
269  }
270 }
271 
272 //********************************************************************
273 bool InputManager::HasConverter(const std::string& conv, bool err_meaasage) {
274 //********************************************************************
275 
276  for (std::vector<InputConverter*>::iterator it = _converters.begin(); it != _converters.end(); it++) {
277  if ((*it)->Name() == conv){
278  return true;
279  }
280  }
281  if (err_meaasage)
282  std::cout << "ERROR: Input Converter '" << conv << "' does not exist" << std::endl;
283 
284  return false;
285 }
286 
287 //********************************************************************
288 void InputManager::AddConverter(const std::string& name, InputConverter* conv) {
289 //********************************************************************
290 
291  if (HasConverter(name, false)){
292  std::cout << "ERROR: InputConverter " << name << " already exists. If you want to replace it please use ReplaceConverter method" << std::endl;
293  return;
294  }
295 
296  // Add a new converter
297  conv->SetName(name);
298  _converters.push_back(conv);
299 }
300 
301 //********************************************************************
302 void InputManager::ReplaceConverter(const std::string& name, InputConverter* conv) {
303 //********************************************************************
304 
305  for (std::vector<InputConverter*>::iterator it = _converters.begin(); it != _converters.end(); it++) {
306  if ((*it)->Name() == name){
307  std::cout << "Overriding " << name << " input converter" << std::endl;
308 
309  // Replace an existing converter with the same name
310  delete *it;
311  conv->SetName(name);
312  *it = conv;
313  return;
314  }
315  }
316 
317  std::cout << "InputConverter " << name << " does not exist. Cannot replace it !!!!!" << std::endl;
318 
319 }
320 
321 //********************************************************************
323 //********************************************************************
324  for (std::vector<InputConverter*>::iterator it = _converters.begin(); it != _converters.end(); it++) {
325  if (*it) delete *it;
326  }
327  _converters.clear();
328 }
329 
330 //********************************************************************
332 //********************************************************************
333 
335 }
336 
337 //*****************************************************************************
339 //*****************************************************************************
340 
342 }
343 
344 //*****************************************************************************
346 //*****************************************************************************
347  if (_Spill) {
348  delete _Spill;
350  }
351 }
352 
353 //*****************************************************************************
354 //AnaEventC* InputManager::MakeEventFromFinalSpill() {
355 //*****************************************************************************
356 
357 // return new AnaEventC(GetSpill(),GetBunch());
358 //}
359 
360 //*****************************************************************************
361 //AnaEventC* InputManager::MakeEvent() {
362 //*****************************************************************************
363 
364 // return new AnaEventC(GetCorrectedSpill(),GetCorrectedBunch());
365 //}
366 
367 //*****************************************************************************
368 //AnaSuperEventB* InputManager::MakeSuperEvent() {
369 //*****************************************************************************
370 /*
371  AnaSuperEventB* sevent = new AnaSuperEventB();
372 
373  sevent->RawEvent = MakeEvent();
374  sevent->Event = sevent->RawEvent->Clone();
375 
376  return sevent;
377 }
378 
379 
380 */
virtual void Reset()
Reset the converters and the UniqueID.
Long64_t LoadTree(Long64_t entry)
Call InputConverter::LoadTree() for the selected converter.
virtual Int_t GetEvent(Long64_t &entry, AnaEventC *&event)=0
std::vector< InputConverter * > _converters
Vector of converters.
AnaSpillC * _Spill
The current spill with corrections and systematics.
virtual bool LoadSpill(Long64_t &entry)
AnaSuperEventB * _sevent
The super event.
bool LoadEvent(Long64_t &entry, bool delPrevious=true)
void SelectConverter(InputConverter *sel)
Set this converter as the selected one.
virtual Long64_t LoadTree(Long64_t entry)
Handle loading the correct entry from fChain.
virtual void Reset()
Reset the trees in the TChain and the number of entries. Also the header (POT, etc) ...
void RemoveConverters()
Delete all the converters that have been added.
virtual void SetCosmicMode(const bool cosmic)
virtual AnaSpillC * Clone()=0
Clone this object.
void IncrementPOTBySpill()
bool HasConverter(const std::string &conv, bool err_message=true)
Whether the vector of converters contains one registered with this name.
Int_t UniqueID
The event unique ID.
void ReplaceConverter(const std::string &name, InputConverter *conv)
Replace an existing converter.
InputManager()
Constructor.
Definition: InputManager.cxx:6
virtual void IncrementPOTBySpill()=0
std::string FindFirstFile(const std::string &infile_name, bool isROOTFile)
Int_t _uniqueID
The current Event UniqueID.
InputConverter & GetConverter()
Get the current selected converter.
virtual Long64_t GetEntries()
Return the total number of entries in the chain.
bool IsROOTFile(const std::string &infile_name)
Is this a ROOT file ?
Long64_t GetEntries()
Return the number of entries in the input file tree(s).
AnaSuperEventB * _prevSEvent
The previous Super Event for deleting if necessary.
bool AddDataFileToConverter(const std::string &infile_name, const std::string &conv, const bool isCosmic=false, bool reset=false)
bool _IsMC
Data/MC status.
void SetName(const std::string &name)
Set the name of this converter, which is used internally by the InputManager.
virtual void DeleteSpill()
clean up the remaining pointers to the spills.
bool Initialize(const std::string &infile_name, const std::string &conv, const bool isCosmic=false)
void AddConverter(const std::string &name, InputConverter *conv)
Add this converter to the vector of recognised converters.
InputConverter * _selected
Selected converter.
bool ReadFile(const std::string &infile_name, bool isROOTFile)
void MakeFinalSpill()
Creates a clone of the corrected Spill. This must be done after applying corrections.
virtual Int_t GetSpill(Long64_t &entry, AnaSpillC *&spill)=0
const std::string & GetSoftwareVersion()
Returns the software version from the header.
AnaSpillC * _CorrectedSpill
The current corrected spill.
void ResetSpillToCorrected()
virtual bool AddFileToTChain(const std::string &inputString)=0
Add the file specified to fChain, and any friend chains that were set up.