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 fFileName ,
12 ) : rootTagName_(
"ROOT")
14 fSystemPath_ = fSystemPath ;
15 fRootPath_ = fRootPath ;
16 fFoldersPath_ = fFoldersPath;
17 fHistName_ = fHistName ;
18 fFileName_ = fFileName ;
22 isALeaf_[
true] =
"true" ;
23 isALeaf_[
false] =
"false" ;
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) ;
34 rootFile_ =
new TFile((fSystemPath_ + std::string(
"/") +
35 fRootPath_ + std::string(
"/") +
36 fFoldersPath_+ std::string(
"/") +
37 fFileName).c_str() ) ;
39 if( debug_) rootFile_->ls() ;
44 xercesc::DOMDocument * RootFileExplorer::initialize(
void)
48 xercesc::XMLPlatformUtils::Initialize();
50 catch(xercesc::XMLException& e)
52 std::string msg = xercesc::XMLString::transcode(e.getMessage()) ;
56 theImplementation_ = xercesc::DOMImplementationRegistry::getDOMImplementation(xercesc::XMLString::transcode(
"Core"));
58 if(theImplementation_)
62 theDocument_ = theImplementation_->createDocument(
63 xercesc::XMLString::transcode(
"http://www.w3.org/2001/XMLSchema-instance"),
64 xercesc::XMLString::transcode(rootTagName_.c_str()),
68 catch(
const xercesc::OutOfMemoryException&)
70 XERCES_STD_QUALIFIER cerr <<
"OutOfMemoryException"
71 << XERCES_STD_QUALIFIER endl;
73 catch(
const xercesc::DOMException& e)
75 XERCES_STD_QUALIFIER cerr <<
"DOMException code is: "
78 << xercesc::XMLString::transcode(e.getMessage())
79 << XERCES_STD_QUALIFIER endl;
81 catch(
const xercesc::XMLException& e)
87 XERCES_STD_QUALIFIER cerr <<
"An error occurred creating the theDocument_"
88 << XERCES_STD_QUALIFIER endl;
93 XERCES_STD_QUALIFIER cerr <<
"Requested theImplementation_ is not supported"
94 << XERCES_STD_QUALIFIER endl;
97 rootElement_ = theDocument_->getDocumentElement();
99 this->makeDirectoryBinaryTree(rootFile_,0,NULL) ;
100 return theDocument_ ;
103 xercesc::DOMElement * RootFileExplorer::populateBinaryTreeNode(xercesc::DOMElement * anchorNode,
108 xercesc::DOMElement * nodes ;
109 if( theNodes_.find(level) == theNodes_.end() )
111 nodes = theDocument_->createElement( xercesc::XMLString::transcode(
"nodes" ));
112 anchorNode->appendChild(nodes);
113 theNodes_[level] = nodes ;
117 nodes = theNodes_.find(level)->second ;
120 xercesc::DOMElement * node = theDocument_->createElement( xercesc::XMLString::transcode(
"node" ));
121 nodes->appendChild(node);
123 xercesc::DOMElement * nChilds = theDocument_->createElement( xercesc::XMLString::transcode(
"nChilds" ));
124 node->appendChild(nChilds);
126 xercesc::DOMText * nChildsVal = theDocument_->createTextNode(xercesc::XMLString::transcode(
"x" ));
127 nChilds->appendChild(nChildsVal);
129 xercesc::DOMElement * fSystemPath = theDocument_->createElement( xercesc::XMLString::transcode(
"fSystemPath" ));
130 node->appendChild(fSystemPath);
132 xercesc::DOMText * fSystemPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fSystemPath_.c_str() ));
133 fSystemPath->appendChild(fSystemPathVal);
135 xercesc::DOMElement * fRootPath = theDocument_->createElement( xercesc::XMLString::transcode(
"fRootPath" ));
136 node->appendChild(fRootPath);
138 xercesc::DOMText * fRootPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fRootPath_.c_str() ));
139 fRootPath->appendChild(fRootPathVal);
141 xercesc::DOMElement * fFoldersPath = theDocument_->createElement( xercesc::XMLString::transcode(
"fFoldersPath" ));
142 node->appendChild(fFoldersPath);
144 xercesc::DOMText * fFoldersPathVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fFoldersPath_.c_str()));
145 fFoldersPath->appendChild(fFoldersPathVal);
147 xercesc::DOMElement * fDisplayName = NULL ;
148 xercesc::DOMElement * fFileName = NULL ;
149 xercesc::DOMElement * fHistName = NULL ;
151 xercesc::DOMText * fDisplayNameVal = NULL ;
152 xercesc::DOMText * fFileNameVal = NULL ;
153 xercesc::DOMText * fHistNameVal = NULL ;
155 fDisplayName = theDocument_->createElement( xercesc::XMLString::transcode(
"fDisplayName" ));
156 fHistName = theDocument_->createElement( xercesc::XMLString::transcode(
"fHistName" ));
157 fFileName = theDocument_->createElement( xercesc::XMLString::transcode(
"fFileName" ));
159 fFileNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fFileName_.c_str()));
160 fHistNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fHistName_.c_str()));
163 fDisplayNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fHistName_.c_str())); ;
167 fDisplayNameVal = theDocument_->createTextNode(xercesc::XMLString::transcode(fFileName_.c_str()));
170 node ->appendChild(fDisplayName );
171 node ->appendChild(fFileName );
172 node ->appendChild(fHistName );
174 fDisplayName->appendChild(fDisplayNameVal);
175 fFileName ->appendChild(fFileNameVal );
176 fHistName ->appendChild(fHistNameVal );
178 STDLINE(std::string(
"fFileNameVal: ")+fFileName_, std::string(ACBlue) + std::string(ACReverse)) ;
179 STDLINE(std::string(
"fHistNameVal: ")+fHistName_, std::string(ACYellow)+ std::string(ACReverse)) ;
181 xercesc::DOMElement * leaf = theDocument_->createElement( xercesc::XMLString::transcode(
"leaf" ));
182 node->appendChild(leaf);
183 STDLINE(nodes, std::string(ACCyan)+ std::string(ACReverse)) ;
185 xercesc::DOMText * leafVal = theDocument_->createTextNode(xercesc::XMLString::transcode(isALeaf_[isLeaf].c_str()));
186 leaf->appendChild(leafVal);
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);
200 void RootFileExplorer::makeDirectoryBinaryTree(TDirectory * currentDirectory,
202 xercesc::DOMElement * anchorNode )
204 if( !anchorNode) anchorNode = rootElement_ ;
205 ss_.str(
"") ; ss_ <<
"Exploring root folder " << currentDirectory->GetName() ;
206 if( debug_ ) STDLINE(ss_.str(), ACRed) ;
208 TIter hList(currentDirectory->GetListOfKeys());
209 while((keyH = (TKey*)hList()))
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() )
221 fThisFolderPath_ = hName ;
222 currentDirectory->cd(hName.c_str());
223 TDirectory * subDir = gDirectory ;
224 if( theHierarchy_.find(level) == theHierarchy_.end() )
226 theHierarchy_[level] = subDir->GetName() ;
227 ss_.str(
"") ; ss_ <<
"theHierarchy_[" << level <<
"] = " << theHierarchy_[level] ;
228 STDLINE(ss_.str(),ACWhite) ;
230 if( debug_ ) STDLINE(fFoldersPath_,ACBlue) ;
231 if( debug_ ) STDLINE(subDir->GetName(),ACCyan) ;
234 ss_.str(
""); ss_ <<
"theHierarchy_.size(): " << theHierarchy_.size() ;
235 STDLINE(ss_.str(),ACCyan) ;
236 for(
int i=0; i<(int)theHierarchy_.size(); i++)
238 fFoldersPath_ += theHierarchy_.find(i)->second ;
239 STDLINE(std::string(
"fFoldersPath_: ")+fFoldersPath_,ACWhite) ;
241 xercesc::DOMElement * node = this->populateBinaryTreeNode(anchorNode, hName, level,
false) ;
242 this->makeDirectoryBinaryTree(subDir,level+1,node) ;
248 if( debug_ ) STDLINE(hName,
"") ;
249 for(
int i=0; i<level; i++)
251 fFoldersPath_ += theHierarchy_[i] + std::string(
"/") ;
252 ss_.str(
"") ; ss_ <<
"fFoldersPath_: " << fFoldersPath_ ;
253 STDLINE(ss_.str(),ACWhite) ;
257 STDLINE(std::string(
"fFoldersPath_: ")+fFoldersPath_,
"") ;
258 this->populateBinaryTreeNode(anchorNode, hName, level,
true ) ;