Diana Software
LRootGlobalReader.cc
Go to the documentation of this file.
1 #include "LRootGlobalReader.hh"
2 #include "QObject.hh"
3 #include "TH1I.h"
4 #include "TH1D.h"
5 #include "TH1F.h"
6 #include "TObjString.h"
7 #include "QDianaR.hh"
8 #include "TROOT.h"
9 
11 
12 using namespace Diana;
13 
14 
16 {
17  fQObjectCache = new std::map<std::string, QObject*>;
18  fFile = NULL;
19 }
20 
22 {
23  delete fQObjectCache;
24 }
25 
26 QError LRootGlobalReader::Open(const std::string& filename, const std::string& opt)
27 {
29  if (!fFile) fFile = new TFile(filename.c_str(),"READ");
30  if(fFile->IsZombie()) {
32  err.SetDescription("Cannot open " + filename);
33  delete fFile;
34  fFile = NULL;
35  }
36  else if(fFile->TestBit(TFile::kRecovered)) {
38  err.SetDescription("File " + filename + " not closed");
39  delete fFile;
40  fFile = NULL;
41  }
42  return err;
43 }
44 
45 
47 {
49  if(fFile){
50  // MV FIXME this check if for cint. When we do ".q" TFiles are automatically deleted
51  // by root so that this delete results in a double free.
52  if(gROOT->GetListOfFiles()->GetSize() > 0) delete fFile;
53  }
54  fFile = NULL;
55  std::map<std::string, QObject*>::iterator iter;
56  for(iter = fQObjectCache->begin(); iter != fQObjectCache->end(); iter++)
57  delete iter->second;
58 
59  return err;
60 }
61 
63 {
64  if(fFile != NULL) return true;
65  return false;
66 }
67 
68 std::string LRootGlobalReader::GetFullPath(const std::string& name) const
69 {
70  std::string fullpath = std::string(ROOT_GLOBAL_DIR) + "/" + name;
71  return fullpath;
72 }
73 
74 const QObject* LRootGlobalReader::GetQObject(const std::string& name, const std::string& className, QError& err) const
75 {
76  // check for already existing object
77  err = QERR_SUCCESS;
78  std::map<std::string, QObject*>::const_iterator object = fQObjectCache->find(name);
79  if(object != fQObjectCache->end()) return object->second;
80  // get object from root file
81  TObject* tobj = NULL;
82  if(ReadingIsSafe()) fFile->GetObject(GetFullPath(name).c_str(),tobj);
83  QObject* obj = dynamic_cast<QObject*>(tobj);
84  if(obj) (*fQObjectCache)[name] = obj;
85  if( obj == NULL) {
87  err.SetDescription(GetName(),__LINE__,std::string("Could not find object \"")+name+"\"");
88  }
89  return obj;
90 }
91 
92 
err
Definition: CheckOF.C:114
#define ROOT_GLOBAL_DIR
Definition: QDianaR.hh:4
@ QERR_UNKNOWN_ERR
Definition: QError.hh:108
@ QERR_CANNOT_OPEN_FILE
Definition: QError.hh:33
@ QERR_SUCCESS
Definition: QError.hh:27
#define REGISTER_GLOBAL_READER(clazz, ext)
global reader for root files
std::string GetFullPath(const std::string &name) const
QError Close()
Close file, called by QGlobalReaderDispatcher.
const Diana::QObject * GetQObject(const std::string &name, const std::string &className, QError &err) const
QError Open(const std::string &filename, const std::string &opt="")
Open file, called by QGlobalReaderDispatcher.
bool ReadingIsSafe() const
std::map< std::string, Diana::QObject * > * fQObjectCache
error class with error type and description
Definition: QError.hh:115
Abstract class for global readers.
const std::string & GetName() const
Definition: QNamed.hh:19
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...