RootController.cxx
Go to the documentation of this file.
1 
12 #include "RootController.h"
13 
14 #include "RootNTuple.h"
15 
17 
18 #ifdef _MSC_VER
19 #include "w32pragma.h"
20 #endif
21 #include "TFile.h"
22 #include "TKey.h"
23 #include "TTree.h"
24 
25 #include "TH2.h"
26 
27 #include <fstream>
28 #include <stdexcept>
29 
30 #include <cassert>
31 
32 using std::string;
33 using std::vector;
34 
35 using namespace hippodraw;
36 
39 typedef std::map < std::string, TFile * > ::iterator FileMapIterator_t;
40 
42 
46 {
47  if ( s_instance == 0 ) {
48  s_instance = new RootController ();
49  }
50 #ifdef _MSC_VER
51  TTree * tree = new TTree (); // force loading of dll
52  delete tree;
53 #endif
54  TH2::Class(); // This is needed to avoid ROOT crashing upon reading
55  // a second file that contains a TH2 object.
56  return s_instance;
57 }
58 
61 {
62 }
63 
64 const std::string &
66 version () const
67 {
68  m_version = ROOT_RELEASE;
69  return m_version;
70 }
71 
72 TFile *
74 openFile ( const std::string & name )
75 {
76  TFile * file = 0;
77  FileMapIterator_t first = m_file_map.find ( name );
78 
79  if ( first == m_file_map.end() ) {
80  ifstream test ( name.c_str (), std::ios::in );
81  if ( test.is_open () == false ) {
82  string what ( "RootController: File `" );
83  what += name;
84  what += "' was not found.";
85 
86  throw std::runtime_error ( what );
87  }
88  file = new TFile ( name.c_str() );
89  m_file_map [ name ] = file;
90  }
91  else {
92  file = first -> second;
93  }
94 
95  return file;
96 }
97 
98 void
100 closeFile ( const std::string & name )
101 {
102  FileMapIterator_t where = m_file_map.find ( name );
103  if ( where != m_file_map.end () ) {
104  TFile * file = where -> second; // do before following for VC++ debugger.
105  m_file_map.erase ( where );
106  delete file;
107  }
108 }
109 
113 const vector < string > &
115 getNTupleNames ( const std::string & file_name )
116 {
117  m_ntuple_names.clear();
118 
119  TFile * file = openFile ( file_name );
120 
121  if ( file != 0 ) {
122  TList * keys = file -> GetListOfKeys ();
123  Int_t size = keys -> GetSize ();
124 
125  for ( Int_t i = 0; i < size; i++ ) {
126  TObject * obj = keys -> At ( i );
127  TKey * key = dynamic_cast < TKey * > ( obj );
128  const string class_name = key -> GetClassName ();
129  if ( class_name == "TTree" ||
130  class_name == "TNtuple" ||
131  class_name == "TNtupleD" ) {
132  const string name = key -> GetName ();
133  m_ntuple_names.push_back ( name );
134  }
135  }
136  }
137  closeFile ( file_name );
138 
139  return m_ntuple_names;
140 }
141 
142 TTree *
144 getTree ( const std::string & filename,
145  const std::string & treename )
146 {
147  TFile * file = openFile ( filename );
148  if ( file == NULL ) return NULL;
149 
150  TObject * object = file -> Get ( treename.c_str() );
151  TTree * tree = dynamic_cast < TTree * > ( object );
152 
153  return tree;
154 }
155 
156 DataSource *
158 createNTuple ( const std::string & filename, const std::string & treename )
159 {
160  TTree * tree = getTree ( filename, treename );
161  if ( tree == NULL ) return NULL;
162 
163  DataSource * ntuple = new RootNTuple ( tree );
164 
165  return initNTuple ( ntuple, filename, treename );
166 }
167 
168 DataSource *
170 initNTuple ( DataSource * ntuple, const std::string & filename,
171  const std::string & treename )
172 {
173  string ds_name = filename;
174  ds_name += ": ";
175  ds_name += treename;
176 
177  ntuple -> setTitle ( treename );
178  ntuple -> setName ( ds_name );
180  controller -> registerNTuple ( ds_name, ntuple );
181  controller -> registerDataSourceFile ( ntuple );
182 
183  m_tuple_map [ ntuple ] = filename;
184  ntuple -> addObserver ( this );
185 
186  return ntuple;
187 }
188 
189 DataSource *
191 createNTuple ( const std::string & name )
192 {
193  DataSource * rtuple = 0;
194 
195  string::size_type pos = name.find_last_of ( ':' );
196  if ( pos == string::npos ) {
197  const vector < string > & tree_names = getNTupleNames ( name );
198  rtuple = createNTuple ( name, tree_names[0] );
199  }
200  else {
201  const string filename = name.substr ( 0, pos );
202  string tree_name = name.substr ( pos + 1 );
203  pos = tree_name.find_first_not_of ( ' ' );
204  tree_name.erase ( 0, pos );
205  rtuple = createNTuple ( filename, tree_name );
206  }
207 
208  return rtuple;
209 }
210 
211 void
213 fillDimSize ( std::vector < int > & dims,
214  const DataSource * source,
215  const std::string & column )
216 {
217  dims.clear();
218 
219  const RootNTuple * rtuple = dynamic_cast < const RootNTuple * > ( source );
220  if ( rtuple != 0 ) {
221  int index = rtuple -> indexOf ( column );
222  vector < int > shape; // complete shape include events
223  rtuple -> fillShape ( shape, index );
224  for ( unsigned int i = 1; i < shape.size(); i++ ) {
225  dims.push_back ( shape [ i] ); // just shape of variable
226  }
227  }
228 }
229 
230 bool
232 smartExpandRootNTuple ( DataSource * source, std::string & column )
233 {
234  bool yes = false;
235  RootNTuple * rtuple = dynamic_cast < RootNTuple * > ( source );
236  if ( rtuple != 0 ) {
237  rtuple -> smartExpandRootNTuple ( column );
238  yes = true;
239  }
240 
241  return yes;
242 }
243 
244 void
246 update ( const Observable * )
247 {
248  // nothing to be done.
249 }
250 
254 void
256 willDelete ( const Observable * obs )
257 {
258  const RootNTuple * tuple = dynamic_cast < const RootNTuple * > ( obs );
259 // assert ( tuple != 0 ); // only observing this type
260  if ( tuple != 0 ) {
261  TupleToFileMap_t::iterator first = m_tuple_map.find ( tuple );
262  assert ( first != m_tuple_map.end () ); // should be there.
263 
264  const string & filename = first -> second;
265  // Are there any others
266  int count = 0;
267  first = m_tuple_map.begin();
268  while ( first != m_tuple_map.end () ) {
269  const string & name = first -> second;
270  if ( name == filename ) {
271  count ++;
272  }
273  ++first;
274  }
275 
276  if ( count == 1 ) {
277  closeFile ( filename );
278  }
279  }
280 }
Part of an implementation of the Observable-Observer pattern based on the example in the GOF Patterns...
Definition: Observable.h:39
unsigned int i
The ROOT TTree class.
hippodraw::RootController class interface.
TFile * openFile(const std::string &name)
Attempts to open the file name.
const std::string & version() const
Returns the version of ROOT being used.
hippodraw::RootNTuple class interface.
A DataSource class implemented with a ROOT TBranch objects from a ROOT TTree to store the column data...
Definition: RootNTuple.h:37
bool smartExpandRootNTuple(DataSource *source, std::string &column)
Expands the array column.
virtual void willDelete(const Observable *obs)
Closes the file that was used to create the observed RootNTuple.
column
The column indices for 2 dimension data point tuple.
std::map< std::string, TFile * >::iterator FileMapIterator_t
Short cut to iterator type.
std::vector< intptr_t > shape(numeric::array arr)
Definition: num_util.cpp:317
DataSource * initNTuple(DataSource *source, const std::string &filename, const std::string &treename)
Initialized and registers new DataSource.
return yes
Definition: CanvasView.cxx:883
DataSource * createNTuple(const std::string &name)
void fillDimSize(std::vector< int > &dims, const DataSource *source, const std::string &column)
Fills the vector dims.
static RootController * s_instance
The singleton instance of the RootController.
intp size(numeric::array arr)
Definition: num_util.cpp:296
A singleton class that is the interface between GUI and the DataSource objects.
TupleToFileMap_t m_tuple_map
A map to find which ROOT file was used to create RootNTuple.
std::map< std::string, TFile * > m_file_map
The list of opened ROOT files.
static RootController * instance()
Returns the singleton instance of the RootController.
std::string m_version
The version of ROOT being used.
return index
Definition: PickTable.cxx:182
const std::vector< std::string > & getNTupleNames(const std::string &)
Returns the names of the DataSource objects contained in the file name.
virtual void update(const Observable *)
Does nothing, but satisfies the pure virtual function in base.
DataSourceController class interface.
A Controller class for ROOT files.
TTree * getTree(const std::string &file, const std::string &tree)
Returns the named tree from file in the ROOT file.
virtual ~RootController()
The destructor.
void closeFile(const std::string &name)
Closed the named file, if found in list of opened files.
static DataSourceController * instance()
Returns the pointer to the singleton instance.
std::vector< std::string > m_ntuple_names
Temporary list of DataSource names in the file.
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen