otsdaq  v2_05_02_indev
RootFileExplorer.cpp
1 #include "otsdaq/RootUtilities/RootFileExplorer.h"
2 #include "otsdaq/Macros/MessageTools.h"
3 // clang-format off
4 //================================================================================================
5 RootFileExplorer::RootFileExplorer(
6  std::string fSystemPath ,
7  std::string fRootPath ,
8  std::string fFoldersPath,
9  std::string fHistName ,
10  std::string fFileName ,
11  HttpXmlDocument & xmlOut
12  ) : rootTagName_("ROOT")
13 {
14  fSystemPath_ = fSystemPath ;
15  fRootPath_ = fRootPath ;
16  fFoldersPath_ = fFoldersPath;
17  fHistName_ = fHistName ;
18  fFileName_ = fFileName ;
19  xmlOut_ = xmlOut ;
20  level_ = 0 ;
21  debug_ = true ;
22  isALeaf_[true] = "true" ;
23  isALeaf_[false] = "false" ;
24 
25  if( debug_ )
26  {
27  STDLINE(std::string("fSystemPath_ : ")+fSystemPath_ ,ACWhite) ;
28  STDLINE(std::string("fRootPath_ : ")+fRootPath_ ,ACWhite) ;
29  STDLINE(std::string("fFoldersPath_: ")+fFoldersPath_,ACWhite) ;
30  STDLINE(std::string("fHistName_ : ")+fHistName_ ,ACWhite) ;
31  STDLINE(std::string("fFileName_ : ")+fFileName_ ,ACWhite) ;
32  }
33 
34  rootFile_ = new TFile((fSystemPath_ + std::string("/") +
35  fRootPath_ + std::string("/") +
36  fFoldersPath_+ std::string("/") +
37  fFileName).c_str() ) ;
38 
39  if( debug_) rootFile_->ls() ;
40 
41 }
42 
43 //================================================================================================
44 xercesc::DOMDocument * RootFileExplorer::initialize(void)
45 {
46  try
47  {
48  xercesc::XMLPlatformUtils::Initialize(); // Initialize Xerces infrastructure
49  }
50  catch(xercesc::XMLException& e)
51  {
52  std::string msg = xercesc::XMLString::transcode(e.getMessage()) ;
53  //STDLINE(std::string("XML toolkit initialization error: ")+msg,ACRed) ;
54  }
55 
56  theImplementation_ = xercesc::DOMImplementationRegistry::getDOMImplementation(xercesc::XMLString::transcode("Core"));
57 
58  if(theImplementation_)
59  {
60  try
61  {
62  theDocument_ = theImplementation_->createDocument(
63  xercesc::XMLString::transcode("http://www.w3.org/2001/XMLSchema-instance"),
64  xercesc::XMLString::transcode(rootTagName_.c_str()),
65  0
66  );
67  }
68  catch(const xercesc::OutOfMemoryException&)
69  {
70  XERCES_STD_QUALIFIER cerr << "OutOfMemoryException"
71  << XERCES_STD_QUALIFIER endl;
72  }
73  catch(const xercesc::DOMException& e)
74  {
75  XERCES_STD_QUALIFIER cerr << "DOMException code is: "
76  << e.code
77  << " "
78  << xercesc::XMLString::transcode(e.getMessage())
79  << XERCES_STD_QUALIFIER endl;
80  }
81  catch(const xercesc::XMLException& e)
82  {
83  //STDLINE(std::string("Error Message: ")+xercesc::XMLString::transcode(e.getMessage()),ACRed) ;
84  }
85  catch(...)
86  {
87  XERCES_STD_QUALIFIER cerr << "An error occurred creating the theDocument_"
88  << XERCES_STD_QUALIFIER endl;
89  }
90  }
91  else
92  {
93  XERCES_STD_QUALIFIER cerr << "Requested theImplementation_ is not supported"
94  << XERCES_STD_QUALIFIER endl;
95  }
96 
97  rootElement_ = theDocument_->getDocumentElement();
98 
99  this->makeDirectoryBinaryTree(rootFile_,0,NULL) ;
100  return theDocument_ ;
101 }
102 //==========================================================================================
103 xercesc::DOMElement * RootFileExplorer::populateBinaryTreeNode(xercesc::DOMElement * anchorNode,
104  std::string name ,
105  int level ,
106  bool isLeaf )
107 {
108  xercesc::DOMElement * nodes ;
109  if( theNodes_.find(level) == theNodes_.end() ) // a new node
110  {
111  nodes = theDocument_->createElement( xercesc::XMLString::transcode("nodes" ));
112  anchorNode->appendChild(nodes);
113  theNodes_[level] = nodes ;
114  }
115  else // Is already there
116  {
117  nodes = theNodes_.find(level)->second ;
118  }
119 
120  xercesc::DOMElement * node = theDocument_->createElement( xercesc::XMLString::transcode("node" ));
121  nodes->appendChild(node);
122 
123  xercesc::DOMElement * nChilds = theDocument_->createElement( xercesc::XMLString::transcode("nChilds" ));
124  node->appendChild(nChilds);
125 
126  xercesc::DOMText * nChildsVal = theDocument_->createTextNode(xercesc::XMLString::transcode("x" ));
127  nChilds->appendChild(nChildsVal);
128 
129  xercesc::DOMElement * fSystemPath = theDocument_->createElement( xercesc::XMLString::transcode("fSystemPath" ));
130  node->appendChild(fSystemPath);
131 
132  xercesc::DOMText * fSystemPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fSystemPath_.c_str() ));
133  fSystemPath->appendChild(fSystemPathVal);
134 
135  xercesc::DOMElement * fRootPath = theDocument_->createElement( xercesc::XMLString::transcode("fRootPath" ));
136  node->appendChild(fRootPath);
137 
138  xercesc::DOMText * fRootPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fRootPath_.c_str() ));
139  fRootPath->appendChild(fRootPathVal);
140 
141  xercesc::DOMElement * fFoldersPath = theDocument_->createElement( xercesc::XMLString::transcode("fFoldersPath" ));
142  node->appendChild(fFoldersPath);
143 
144  xercesc::DOMText * fFoldersPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fFoldersPath_.c_str()));
145  fFoldersPath->appendChild(fFoldersPathVal);
146 
147  xercesc::DOMElement * fDisplayName = NULL ;
148  xercesc::DOMElement * fFileName = NULL ;
149  xercesc::DOMElement * fHistName = NULL ;
150 
151  xercesc::DOMText * fDisplayNameVal = NULL ;
152  xercesc::DOMText * fFileNameVal = NULL ;
153  xercesc::DOMText * fHistNameVal = NULL ;
154 
155  fDisplayName = theDocument_->createElement( xercesc::XMLString::transcode("fDisplayName" ));
156  fHistName = theDocument_->createElement( xercesc::XMLString::transcode("fHistName" ));
157  fFileName = theDocument_->createElement( xercesc::XMLString::transcode("fFileName" ));
158 
159  fFileNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fFileName_.c_str()));
160  fHistNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fHistName_.c_str()));
161  if(isLeaf)
162  {
163  fDisplayNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fHistName_.c_str())); ;
164  }
165  else
166  {
167  fDisplayNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fFileName_.c_str()));
168  }
169 
170  node ->appendChild(fDisplayName );
171  node ->appendChild(fFileName );
172  node ->appendChild(fHistName );
173 
174  fDisplayName->appendChild(fDisplayNameVal);
175  fFileName ->appendChild(fFileNameVal );
176  fHistName ->appendChild(fHistNameVal );
177 
178  STDLINE(std::string("fFileNameVal: ")+fFileName_, std::string(ACBlue) + std::string(ACReverse)) ;
179  STDLINE(std::string("fHistNameVal: ")+fHistName_, std::string(ACYellow)+ std::string(ACReverse)) ;
180 
181  xercesc::DOMElement * leaf = theDocument_->createElement( xercesc::XMLString::transcode("leaf" ));
182  node->appendChild(leaf);
183  STDLINE(nodes, std::string(ACCyan)+ std::string(ACReverse)) ;
184 
185  xercesc::DOMText * leafVal = theDocument_->createTextNode(xercesc::XMLString::transcode(isALeaf_[isLeaf].c_str()));
186  leaf->appendChild(leafVal);
187 
188  STDLINE(std::string("fSystemPath_ : ")+fSystemPath_ ,ACRed);
189  STDLINE(std::string("fRootPath_ : ")+fRootPath_ ,ACRed);
190  STDLINE(std::string("fFoldersPath_ : ")+fFoldersPath_,ACRed);
191  STDLINE(std::string("fFileName_ : ")+name ,ACRed);
192  STDLINE(std::string("fRFoldersPath_: ") , std::string(ACRed)+ std::string(ACReverse));
193  STDLINE(std::string("fHistName_ : ")+name ,ACRed);
194 
195  fFoldersPath_ = "" ;
196 
197  return node;
198 }
199 //================================================================================================
200 void RootFileExplorer::makeDirectoryBinaryTree(TDirectory * currentDirectory,
201  int level ,
202  xercesc::DOMElement * anchorNode )
203 {
204  if( !anchorNode) anchorNode = rootElement_ ;
205  ss_.str("") ; ss_ << "Exploring root folder " << currentDirectory->GetName() ;
206  if( debug_ ) STDLINE(ss_.str(), ACRed) ;
207  TKey * keyH = NULL ;
208  TIter hList(currentDirectory->GetListOfKeys());
209  while((keyH = (TKey*)hList()))
210  {
211  std::string hName = keyH->GetName() ;
212  std::string what = keyH->GetClassName() ;
213  if( what == "TTree" ) continue ;
214  if( what == "TNtuple" ) continue ;
215  if( what == "TGeoManager" ) continue ;
216  if( what == "TGeoVolume" ) continue ;
217  if( debug_ ) STDLINE(std::string("currentDirectory: ")+ std::string(currentDirectory->GetName()),ACCyan) ;
218  if( debug_ ) STDLINE(std::string("Object type : ")+what,ACRed) ;
219  if( keyH->IsFolder() )
220  {
221  fThisFolderPath_ = hName ;
222  currentDirectory->cd(hName.c_str());
223  TDirectory * subDir = gDirectory ;
224  if( theHierarchy_.find(level) == theHierarchy_.end() )
225  {
226  theHierarchy_[level] = subDir->GetName() ;
227  ss_.str("") ; ss_ << "theHierarchy_[" << level << "] = " << theHierarchy_[level] ;
228  STDLINE(ss_.str(),ACWhite) ;
229  }
230  if( debug_ ) STDLINE(fFoldersPath_,ACBlue) ;
231  if( debug_ ) STDLINE(subDir->GetName(),ACCyan) ;
232  fFileName_ = hName ;
233  fFoldersPath_ = "" ;
234  ss_.str(""); ss_ << "theHierarchy_.size(): " << theHierarchy_.size() ;
235  STDLINE(ss_.str(),ACCyan) ;
236  for(int i=0; i<(int)theHierarchy_.size(); i++)
237  {
238  fFoldersPath_ += theHierarchy_.find(i)->second ;
239  STDLINE(std::string("fFoldersPath_: ")+fFoldersPath_,ACWhite) ;
240  }
241  xercesc::DOMElement * node = this->populateBinaryTreeNode(anchorNode, hName, level, false) ;
242  this->makeDirectoryBinaryTree(subDir,level+1,node) ;
243 // theHierarchy_.erase(level) ;
244  }
245  else
246  {
247  fFoldersPath_ = "" ;
248  if( debug_ ) STDLINE(hName,"") ;
249  for(int i=0; i<level; i++)
250  {
251  fFoldersPath_ += theHierarchy_[i] + std::string("/") ;
252  ss_.str("") ; ss_ << "fFoldersPath_: " << fFoldersPath_ ;
253  STDLINE(ss_.str(),ACWhite) ;
254  }
255  //fFoldersPath_ += currentDirectory->GetName() ;
256  fHistName_ = hName ;
257  STDLINE(std::string("fFoldersPath_: ")+fFoldersPath_,"") ;
258  /*xercesc::DOMElement * node =*/ this->populateBinaryTreeNode(anchorNode, hName, level, true ) ;
259  }
260  }
261 }
262 // clang-format on