HighLAND
InputConverter.hxx
1 #ifndef InputConverter_h
2 #define InputConverter_h
3 
4 #include <TROOT.h>
5 #include <TChain.h>
6 #include <TFile.h>
7 #include <sstream>
8 #include <fstream>
9 #include <stdio.h>
10 #include <iostream>
11 #include <math.h>
12 #include "CoreDataClasses.hxx"
13 #include "Header.hxx"
14 
15 /// This base class specifies the interface that all input converters should
16 /// implement.
17 ///
18 /// An input converter takes an input file and converts the information into
19 /// the AnaSpillB format used in the framework. For example, the
20 /// oaAnalysisTreeConverter class converts an oaAnalysis file to the AnaSpillB
21 /// format.
22 ///
23 /// The converters themselves are found in the highLevelAnalysisIO package.
25 
26  public:
27 
28  /// Constrctor provided a name
29  InputConverter(const std::string& name);
30 
31  /// Everyone should have a destructor...
32  virtual ~InputConverter() {};
33 
34  /// returns the name of the tree to convert
35  const std::string& GetTreeName(){return _treeName;}
36 
37  /// Return the name of this converter.
38  const std::string& Name() const{return _name;}
39 
40  /// Set the name of this converter, which is used internally by the InputManager
41  void SetName(const std::string& name){_name=name;}
42 
43  /// Reset the trees in the TChain and the number of entries. Also the header (POT, etc)
44  virtual void Reset(){
45  if (fChain) fChain->Reset();
46  _nentries=0;
47  header().Reset();
48  }
49  //--------------------- MANDATORY METHODS --------------------------
50 
51  /// Any initialization that must be done. Called before the input file is
52  /// read.
53  virtual bool Initialize() = 0;
54 
55  /// Read the specified entry from the file, and fill the spill.
56  /// The entry number must be incremented as part of this function, and the
57  /// new entry number returned.
58  /// 0 should be returned on error.
59  virtual Int_t GetSpill(Long64_t& entry, AnaSpillC*& spill) = 0;
60 
61  /// Read the specified entry from the file, and fill the event.
62  /// The entry number must be incremented inside this function
63  /// 0 should be returned on error.
64  virtual Int_t GetEvent(Long64_t& entry, AnaEventC*& event) = 0;
65 
66  /// Record the POT for the current spill, based on information in the AnaBeam
67  /// member of the current AnaSpillB.
68  virtual void IncrementPOTBySpill() = 0;
69 
70  /// Add the file specified to fChain, and any friend chains that were set up.
71  virtual bool AddFileToTChain(const std::string& inputString) = 0;
72 
73  /// Handle loading the correct entry from fChain.
74  virtual Long64_t LoadTree(Long64_t entry);
75 
76  /// Return the total number of entries in the chain.
77  virtual Long64_t GetEntries();
78 
79  /// Return the total number of events for a given number of entries in the tree
80  virtual Int_t GetNEvents(Int_t entries=-1);
81 
82  /// Whether the implementation can handle the specified file.
83  virtual bool IsCorrectType(const std::string& inputString);
84 
85  /// Specify whether we're running in "cosmic" mode or not. Some converters
86  /// don't care about this, but others do and can override this function.
87  virtual void SetCosmicMode(const bool cosmic) {(void)cosmic;};
88 
89  /// Whether the converter has added a chain with the given name to its map
90  /// of chains.
91  bool HasChain(const std::string& name) {
92  return (_chain_map.find(name) != _chain_map.end());
93  }
94 
95  /// Add a chain with the given name to the map of chains the converter uses.
96  /// When two names are given the first is the one we will use in the map,
97  /// while the second is the actual path in the root file.
98  /// If only one name is given, the path is used as name in the map
99  void AddChain(const std::string& name, const std::string& name_path="") {
100  if (!HasChain(name)) {
101  if (name_path=="")
102  _chain_map[name] = new TChain(name.c_str());
103  else
104  _chain_map[name] = new TChain(name_path.c_str());
105  } else {
106  std::cout << "TChain " << name << " already exists. Not added !!!" << std::endl;
107  }
108  }
109 
110  /// Get the chain with the given name, if it has been added to the map of
111  /// chains the converter uses. If no name is specified, the 'main' chain
112  /// fChain is returned. If no chain with the given name is in the map of
113  /// chains, then NULL is returned.
114  TChain* GetChain(const std::string& name = "") {
115  if (name == "") {
116  return fChain;
117  } else if (HasChain(name)) {
118  return _chain_map[name];
119  } else {
120  return NULL;
121  }
122  }
123 
124  /// Returns the Header manager
125  Header& header(){return _header;}
126 
127  protected:
128 
129  /// The name of the converter
130  std::string _name;
131 
132  /// The name of the tree to convert
133  std::string _treeName;
134 
135  /// The number of entries in the chain. To be set by the implementation.
137 
138  /// The main TChain used to read events from the input file.
139  TChain *fChain; //!
140 
141  /// current Tree number in a TChain
142  Int_t fCurrent; //!
143 
144  // Index of the current file being read
145  Int_t _currentFileIndex; //!
146 
147  // Name of the current file being read
148  std::string _currentFileName; //!
149 
150  /// Map of all the chains the converter uses. Access with AddChain(),
151  /// HasChain() and GetChain() functions.
152  std::map<std::string, TChain*> _chain_map;
153 
154  /// The header manager
156 };
157 
158 #endif
159 
InputConverter(const std::string &name)
Constrctor provided a name.
virtual ~InputConverter()
Everyone should have a destructor...
void Reset()
Reset all the information in the header.
Definition: Header.cxx:13
virtual Int_t GetEvent(Long64_t &entry, AnaEventC *&event)=0
std::string _name
The name of the converter.
int _nentries
The number of entries in the chain. To be set by the implementation.
bool HasChain(const std::string &name)
std::string _treeName
The name of the tree to convert.
virtual bool Initialize()=0
virtual Int_t GetNEvents(Int_t entries=-1)
Return the total number of events for a given number of entries in the tree.
std::map< std::string, TChain * > _chain_map
virtual Long64_t LoadTree(Long64_t entry)
Handle loading the correct entry from fChain.
This class handles POT info, SoftwareVersion and IsMC.
Definition: Header.hxx:10
const std::string & Name() const
Return the name of this converter.
void AddChain(const std::string &name, const std::string &name_path="")
virtual void Reset()
Reset the trees in the TChain and the number of entries. Also the header (POT, etc) ...
Int_t fCurrent
current Tree number in a TChain
virtual void SetCosmicMode(const bool cosmic)
virtual bool IsCorrectType(const std::string &inputString)
Whether the implementation can handle the specified file.
const std::string & GetTreeName()
returns the name of the tree to convert
TChain * GetChain(const std::string &name="")
virtual void IncrementPOTBySpill()=0
virtual Long64_t GetEntries()
Return the total number of entries in the chain.
void SetName(const std::string &name)
Set the name of this converter, which is used internally by the InputManager.
Header & header()
Returns the Header manager.
TChain * fChain
The main TChain used to read events from the input file.
virtual Int_t GetSpill(Long64_t &entry, AnaSpillC *&spill)=0
Header _header
The header manager.
virtual bool AddFileToTChain(const std::string &inputString)=0
Add the file specified to fChain, and any friend chains that were set up.