1 #include "otsdaq/RootUtilities/RootFileExplorer.h"
2 #include "otsdaq/Macros/MessageTools.h"
5 RootFileExplorer::RootFileExplorer(
6 std::string fSystemPath ,
7 std::string fRootPath ,
8 std::string fFoldersPath ,
9 std::string fHistName ,
10 std::string fRFoldersPath,
11 std::string fFileName ,
13 ) : rootTagName_(
"ROOT")
15 fSystemPath_ = fSystemPath ;
16 fRootPath_ = fRootPath ;
17 fFoldersPath_ = fFoldersPath ;
18 fHistName_ = fHistName ;
19 fRFoldersPath_ = fRFoldersPath ;
20 fFileName_ = fFileName ;
21 STDLINE(std::string(
"fSystemPath : ")+fSystemPath_ ,ACCyan);
22 STDLINE(std::string(
"fRootPath : ")+fRootPath_ ,ACCyan);
23 STDLINE(std::string(
"fFoldersPath : ")+fFoldersPath_ ,ACCyan);
24 STDLINE(std::string(
"fHistName : ")+fHistName_ ,ACCyan);
25 STDLINE(std::string(
"fRFoldersPath: ")+fRFoldersPath_,ACCyan);
26 STDLINE(std::string(
"fFileName : ")+fFileName_ ,ACCyan);
30 isALeaf_[
true] =
"true" ;
31 isALeaf_[
false] =
"false" ;
32 rootFile_ = rootFile ;
33 anchorNodeLast_ = NULL ;
34 hierarchyPaths_.clear() ;
39 xercesc::DOMDocument * RootFileExplorer::initialize(
bool liveDQMFlag)
42 liveDQMFlag_ = liveDQMFlag;
46 xercesc::XMLPlatformUtils::Initialize();
48 catch(xercesc::XMLException& e)
50 string msg = xercesc::XMLString::transcode(e.getMessage()) ;
53 theImplementation_ = xercesc::DOMImplementationRegistry::getDOMImplementation(xercesc::XMLString::transcode(
"Core"));
55 if(theImplementation_)
59 theDocument_ = theImplementation_->createDocument(
60 xercesc::XMLString::transcode(
"http://www.w3.org/2001/XMLSchema-instance"),
61 xercesc::XMLString::transcode(rootTagName_.c_str()),
65 catch(
const xercesc::OutOfMemoryException&)
67 XERCES_STD_QUALIFIER cerr <<
"OutOfMemoryException"
68 << XERCES_STD_QUALIFIER endl;
70 catch(
const xercesc::DOMException& e)
72 XERCES_STD_QUALIFIER cerr <<
"DOMException code is: "
75 << xercesc::XMLString::transcode(e.getMessage())
76 << XERCES_STD_QUALIFIER endl;
78 catch(
const xercesc::XMLException& e)
80 STDLINE(
string(
"Error Message: ")+xercesc::XMLString::transcode(e.getMessage()),ACRed) ;
84 XERCES_STD_QUALIFIER cerr <<
"An error occurred creating the theDocument_"
85 << XERCES_STD_QUALIFIER endl;
90 XERCES_STD_QUALIFIER cerr <<
"Requested theImplementation_ is not supported"
91 << XERCES_STD_QUALIFIER endl;
94 this->initializeXMLWriter() ;
98 rootElement_ = theDocument_->getDocumentElement();
100 ss_.str(
""); ss_ <<
"Main anchor (document): " << rootElement_ ;
101 STDLINE(ss_.str(),
string(ACCyan)+
string(ACReverse)) ;
102 STDLINE(rootFile_->GetName(),
string(ACCyan)+
string(ACReverse)) ;
108 string fName = fSystemPath_ +string(
"/") +
110 if( fFoldersPath_.length() > 0 ) fName += string(
"/") + fFoldersPath_;
111 if( fFileName_ .length() > 0 ) fName += string(
"/") + fFileName_ ;
112 STDLINE(
string(
"Opening fFileName_: ")+fFileName_, ACYellow) ;
113 STDLINE(
string(
"Opening fName : ")+fName , ACYellow) ;
114 rootFile_ =
new TFile(fName.c_str()) ;
116 if( debug_) rootFile_->ls() ;
118 rootElement_ = theDocument_->getDocumentElement();
123 return theDocument_ ;
131 ss_.str(
""); ss_ <<
"fRFoldersPath_: " << fRFoldersPath_ ; STDLINE(ss_.str(),ACGreen) ;
132 ss_.str(
""); ss_ <<
"previous : " << previousAncestor_; STDLINE(ss_.str(),ACGreen) ;
133 xercesc::DOMElement * nodes ;
134 if( theNodes_.find(previousAncestor_) == theNodes_.end() )
136 nodes = theDocument_->createElement( xercesc::XMLString::transcode(
"nodes" ));
137 ss_.str(
""); ss_ <<
"NEW nodes (" << nodes <<
") for name " << name ; STDLINE(ss_.str(),
"") ;
138 anchorNode->appendChild(nodes);
139 theNodes_ [previousAncestor_] = nodes ;
140 theNodeName_[previousAncestor_] = name ;
144 nodes = theNodes_.find(previousAncestor_)->second ;
145 ss_.str(
""); ss_ << name <<
" points to an OLD nodes (" << nodes <<
") parallel to " << theNodeName_[previousAncestor_]; STDLINE(ss_.str(),
"") ;
147 xercesc::DOMElement * node = theDocument_->createElement( xercesc::XMLString::transcode(
"node" ));
148 node->setAttribute(xercesc::XMLString::transcode(
"class" ),
149 xercesc::XMLString::transcode(
"x-tree-icon x-tree-icon-parent")) ;
150 nodes->appendChild(node);
152 ss_.str(
""); ss_ <<
"Attaching node " << node <<
" previousAncestor_ " << previousAncestor_ <<
" to nodes " << nodes; STDLINE(ss_.str(),
"") ;
154 xercesc::DOMElement * ancestor = theDocument_->createElement (xercesc::XMLString::transcode(
"ancestor"));
155 xercesc::DOMText * ancestorVal = theDocument_->createTextNode(xercesc::XMLString::transcode(previousAncestor_.c_str()));
156 ancestor->appendChild(ancestorVal);
157 node->appendChild(ancestor);
159 xercesc::DOMElement * fSystemPath = theDocument_->createElement( xercesc::XMLString::transcode(
"fSystemPath" ));
160 node->appendChild(fSystemPath);
162 xercesc::DOMText * fSystemPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fSystemPath_.c_str() ));
163 fSystemPath->appendChild(fSystemPathVal);
165 xercesc::DOMElement * fRootPath = theDocument_->createElement( xercesc::XMLString::transcode(
"fRootPath" ));
166 node->appendChild(fRootPath);
168 xercesc::DOMText * fRootPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fRootPath_.c_str() ));
169 fRootPath->appendChild(fRootPathVal);
171 xercesc::DOMElement * fFoldersPath = theDocument_->createElement( xercesc::XMLString::transcode(
"fFoldersPath" ));
172 node->appendChild(fFoldersPath);
174 xercesc::DOMText * fFoldersPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fFoldersPath_.c_str()));
175 fFoldersPath->appendChild(fFoldersPathVal);
177 xercesc::DOMElement * fDisplayName = NULL ;
178 xercesc::DOMElement * fFileName = NULL ;
179 xercesc::DOMElement * fRFoldersPath = NULL ;
180 xercesc::DOMElement * fHistName = NULL ;
182 xercesc::DOMText * fDisplayNameVal = NULL ;
183 xercesc::DOMText * fFileNameVal = NULL ;
184 xercesc::DOMText * fRFoldersPathVal = NULL ;
185 xercesc::DOMText * fHistNameVal = NULL ;
187 fDisplayName = theDocument_->createElement( xercesc::XMLString::transcode(
"fDisplayName" ));
188 fRFoldersPath = theDocument_->createElement( xercesc::XMLString::transcode(
"fRFoldersPath" ));
189 fHistName = theDocument_->createElement( xercesc::XMLString::transcode(
"fHistName" ));
190 fFileName = theDocument_->createElement( xercesc::XMLString::transcode(
"fFileName" ));
192 fFileNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fFileName_.c_str() ));
193 fHistNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fHistName_.c_str() ));
194 fRFoldersPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fRFoldersPath_.c_str()));
198 fDisplayNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fHistName_.c_str() ));
202 fDisplayNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(name.c_str() ));
205 node ->appendChild(fDisplayName );
206 node ->appendChild(fRFoldersPath );
207 node ->appendChild(fFileName );
208 node ->appendChild(fHistName );
210 fDisplayName ->appendChild(fDisplayNameVal );
211 fFileName ->appendChild(fFileNameVal );
212 fHistName ->appendChild(fHistNameVal );
213 fRFoldersPath->appendChild(fRFoldersPathVal);
215 xercesc::DOMElement * leaf = theDocument_->createElement( xercesc::XMLString::transcode(
"leaf" ));
216 node->appendChild(leaf);
218 xercesc::DOMText * leafVal = theDocument_->createTextNode(xercesc::XMLString::transcode(isALeaf_[isLeaf].c_str()));
219 leaf->appendChild(leafVal);
249 std::string subDirName,
250 xercesc::DOMElement * anchorNode )
253 if( !anchorNode) anchorNode = rootElement_ ;
254 currentDir = currentDir->GetDirectory(subDirName.c_str()) ;
259 TIter nextobj(currentDir->GetList());
262 while((obj = (TObject*)nextobj()))
264 string objName = obj->GetName() ;
265 ss_.str(
"") ; ss_ <<
"Exploring " << objName <<
" class: " << string(obj->ClassName()); STDLINE(ss_.str(),
"") ;
266 if(
string(obj->ClassName()) ==
"TTree" ) continue ;
267 if(
string(obj->ClassName()) ==
"TNtuple" ) continue ;
268 if(
string(obj->ClassName()) ==
"TGeoManager" ) continue ;
269 if(
string(obj->ClassName()) ==
"TGeoVolume" ) continue ;
270 if( obj->IsFolder() )
272 ss_.str(
"") ; ss_ <<
"Enter " << objName ;
273 STDLINE(ss_.str(),
"") ;
274 hierarchyPaths_.push_back(objName) ;
275 fRFoldersPath_ =
"" ;
277 for(
int i=0; i<(int)hierarchyPaths_.size(); ++i) {fRFoldersPath_ += hierarchyPaths_[i];}
278 computeRFoldersPath() ;
279 previousAncestor_ = currentDir->GetName() ;
282 fHistName_ = obj->GetName () ;
283 fHistTitle_ = obj->GetTitle() ;
284 if(theNodes_.find(previousAncestor_) != theNodes_.end())
286 this->shrinkHierarchyPaths(1) ;
289 computeRFoldersPath() ;
294 fHistName_ = obj->GetName() ;
295 previousAncestor_ = currentDir->GetName() ;
302 STDLINE(
"No currentDir!!!",
string(ACRed)+
string(ACReverse)) ;
308 xercesc::DOMElement * anchorNode )
310 if( !anchorNode ) anchorNode = rootElement_ ;
311 if( !anchorNodeLast_ ) anchorNodeLast_ = anchorNode ;
314 TIter hList(currentDirectory->GetListOfKeys());
316 while((keyH = (TKey*)hList()))
318 std::string objName = keyH->GetName() ;
319 ss_.str(
"") ; ss_ <<
"Exploring " << objName <<
" level: " << level <<
" COUNTER: " << ++counter_;
321 string what = keyH->GetClassName () ;
322 if( what ==
"TTree" ) continue ;
323 if( what ==
"TNtuple" ) continue ;
324 if( what ==
"TGeoManager" ) continue ;
325 if( what ==
"TGeoVolume" ) continue ;
326 if( what ==
"TDirectoryFile" )
328 ss_.str(
"") ; ss_ <<
"Enter " << objName <<
" level: " << level ;
330 previousAncestor_ = currentDirectory->GetName() ;
331 currentDirectory->cd(objName.c_str());
332 TDirectory * subDir = gDirectory ;
333 hierarchyPaths_.push_back(std::string(subDir->GetName())) ;
335 computeRFoldersPath() ;
340 this->shrinkHierarchyPaths(1) ;
341 computeRFoldersPath() ;
345 fHistName_ = objName ;
346 STDLINE(fHistName_,ACCyan) ;
347 previousAncestor_ = currentDirectory->GetName() ;
354 void RootFileExplorer::computeRFoldersPath(
void)
356 fRFoldersPath_ =
"" ;
357 for(
int i=0; i<(int)hierarchyPaths_.size(); ++i)
359 fRFoldersPath_ += hierarchyPaths_[i] +
"/";
363 void RootFileExplorer::dumpHierarchyPaths(
string what)
365 ss_.str(
"") ; ss_ << what <<
" - hierarchyPaths_.size(): " << hierarchyPaths_.size() ;
366 STDLINE(ss_.str(),
"") ;
367 for(
int i=0; i<(int)hierarchyPaths_.size(); i++)
369 ss_.str(
"") ; ss_ << i <<
"] " << hierarchyPaths_[i] ;
370 STDLINE(ss_.str(),
"") ;
374 std::string RootFileExplorer::computeHierarchyPaths(
void)
376 this->dumpHierarchyPaths(
"Computing...") ;
377 std::string name =
"" ;
378 for(
int i=0; i<(int)hierarchyPaths_.size(); i++)
380 name += hierarchyPaths_[i] ;
381 if( i != (
int)hierarchyPaths_.size() - 1 ) name +=
string(
"/") ;
386 void RootFileExplorer::shrinkHierarchyPaths(
int number)
388 for(
int i=0; i<number; ++i)
390 if( hierarchyPaths_.size() > 0 ) hierarchyPaths_.pop_back() ;
394 void RootFileExplorer::initializeXMLWriter(
void)
397 XMLString::transcode(
"LS", tempStr, 99);
398 DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
399 theSerializer_ = ((DOMImplementationLS*)impl)->createLSSerializer();
400 if (theSerializer_->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent,
true))
401 theSerializer_->getDomConfig()->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent,
true);
403 if (theSerializer_->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint,
true))
404 theSerializer_->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint,
true);
406 myFormTarget_ =
new StdOutFormatTarget();
407 theOutput_ = ((DOMImplementationLS*)impl)->createLSOutput();
408 theOutput_->setByteStream(myFormTarget_);
411 string RootFileExplorer::blanks(
int level)
414 for(
int i=0; i<level; ++i)
xercesc::DOMElement * populateBinaryTreeNode(xercesc::DOMElement *anchorNode, std::string name, bool isLeaf)
void makeLiveDQMBinaryTree(TDirectory *currentDirectory, int indent, std::string subDirName, xercesc::DOMElement *anchorNode)
void makeDirectoryBinaryTree(TDirectory *currentDirectory, int indent, xercesc::DOMElement *anchorNode)