emcNodeHelper.C

Go to the documentation of this file.
00001 #include "emcNodeHelper.h"
00002 
00003 #include "PHCompositeNode.h"
00004 #include "PHNodeIterator.h"
00005 #include <cassert>
00006 #include <string>
00007 #include <vector>
00008 
00009 //_____________________________________________________________________________
00010 void splitPath(const char* path,
00011                std::vector<std::string>& paths)
00012 {
00013   // Given a path e.g. /A/B/C, will return 
00014   // a vector of string with A,B and C.
00015 
00016   paths.clear();
00017 
00018   std::string str = path;
00019 
00020   if ( str.empty() )
00021     {
00022       return;
00023     }
00024 
00025   std::vector<size_t> slashes_pos;
00026 
00027   if ( str[0] != '/' ) 
00028     { 
00029       str.insert(str.begin(),'/');
00030     }
00031 
00032   if ( str[str.size()-1] != '/' ) 
00033     {
00034       str.push_back('/');
00035     }
00036 
00037   for (size_t i = 0 ; i < str.size() ; i++) 
00038     {
00039       if ( str[i] == '/' ) 
00040         { 
00041           slashes_pos.push_back(i);
00042         }
00043     }
00044   
00045   if ( slashes_pos.size() > 0 ) 
00046     {
00047       for (size_t i = 0 ; i < slashes_pos.size()-1 ; i++) 
00048         {
00049           paths.push_back(str.substr(slashes_pos[i]+1,
00050                                      slashes_pos[i+1]-slashes_pos[i]-1));
00051         }
00052     }  
00053 }
00054 
00055 //_____________________________________________________________________________
00056 emcNodeHelper::emcNodeHelper()
00057 {
00058 }
00059 
00060 //_____________________________________________________________________________
00061 PHCompositeNode*
00062 emcNodeHelper::findCompositeNode(PHCompositeNode* topNode, 
00063                                  const char* path)
00064 {
00065   if ( !path || !topNode ) return 0;
00066 
00067   std::vector<std::string> pathList;
00068 
00069   splitPath(path,pathList);
00070 
00071   PHNodeIterator it(topNode);
00072 
00073   PHCompositeNode* rv = 0;
00074 
00075   if ( pathList.size() == 1 ) 
00076     {
00077       rv = static_cast<PHCompositeNode*>
00078         (it.findFirst("PHCompositeNode",pathList[0].c_str()));
00079     }
00080   else
00081     {
00082       for ( size_t i = 0; i < pathList.size()-1 ; ++i ) 
00083         {
00084           std::string name = pathList[i];
00085           bool ok = it.cd(name.c_str());
00086           if (!ok) return 0;
00087         }
00088       rv= static_cast<PHCompositeNode*>
00089         (it.findFirst("PHCompositeNode",pathList[pathList.size()-1].c_str()));
00090     }
00091   return rv;
00092 }
00093 
00094 //_____________________________________________________________________________
00095 bool
00096 emcNodeHelper::makeDSTnodes(PHCompositeNode* topNode)
00097 {
00098   PHCompositeNode* dstNode = findCompositeNode(topNode,"DST");
00099   PHCompositeNode* parNode = findCompositeNode(topNode,"PAR");
00100   PHCompositeNode* dcmNode = findCompositeNode(topNode,"DCM");
00101   PHCompositeNode* evaNode = findCompositeNode(topNode,"EVA");
00102   PHCompositeNode* geaNode = findCompositeNode(topNode,"GEA");
00103 
00104   if ( !dstNode || !parNode || !dcmNode || !evaNode || !geaNode ) 
00105     {
00106       return false;
00107     }
00108 
00109   topNode->addNode(new PHCompositeNode("EMC"));
00110   topNode->addNode(new PHCompositeNode("EMC2"));
00111 
00112   return true;
00113 }
00114 
00115 //_____________________________________________________________________________
00116 bool
00117 emcNodeHelper::makeCompositeNode(PHCompositeNode* topNode,
00118                                  const char* path, const char* opt)
00119 {
00120   if (!path || !topNode) return false;
00121 
00122   std::vector<std::string> pathList;
00123 
00124   splitPath(path,pathList);
00125 
00126   PHNodeIterator it(topNode);
00127 
00128   std::string sopt = opt;
00129 
00130   bool create = (sopt=="-p");
00131 
00132   for ( size_t i = 0; i < pathList.size(); ++i ) 
00133     {
00134       std::string nodename = pathList[i];
00135       bool ok = it.cd(nodename.c_str());
00136       if ( !ok ) 
00137         {
00138           if ( create )
00139             {
00140               it.addNode(new PHCompositeNode(nodename.c_str()));
00141               bool ok = it.cd(nodename.c_str());              
00142               assert(ok==true);
00143             }
00144           else
00145             {
00146               return false;
00147             }
00148         }
00149     }
00150 
00151   return true;
00152 }