emcNodeHelper.h

Go to the documentation of this file.
00001 #ifndef __emcNodeHelper_h__
00002 #define __emcNodeHelper_h__
00003 
00004 #include "PHCompositeNode.h"
00005 #include "PHIODataNode.h"
00006 #include "PHTable.hh"
00007 #include "PHNodeIterator.h"
00008 #include "PHObject.h"
00009 
00010 #include <string>
00011 #include <iostream>
00012 #include <cassert> 
00013 
00018 class emcNodeHelper
00019 {
00020 public:
00021   emcNodeHelper();
00022 
00023   template <class T>
00024   static
00025   T* addTable(PHCompositeNode* node,
00026               const char* tablename,
00027               unsigned int tablesize,
00028               bool transient = false,
00029               const char* nodename="");
00030  
00031   template <class T>
00032   static
00033   T* addObject(PHCompositeNode* node,
00034                const char* objectName,
00035                bool transient = false,
00036                const char* nodename="");
00037 
00038   template <class T>
00039   static
00040   void insertObject(PHCompositeNode* node,
00041                     T* object,
00042                     const char* objectName,  
00043                     bool transient = false,
00044                     const char* nodename="");
00045 
00046   static PHCompositeNode* findCompositeNode(PHCompositeNode* topNode, 
00047                                             const char* path);
00048 
00049   template <class T>
00050   static
00051   T* getTable(const char* tableName, PHCompositeNode* topNode);
00052 
00053   template <class T>
00054   static
00055   T* getObject(const char* objectName, PHCompositeNode* topNode);
00056 
00057   static bool makeDSTnodes(PHCompositeNode* topNode);
00058 
00059   static bool makeCompositeNode(PHCompositeNode* topNode,
00060                                 const char* path, const char* opt);
00061 };
00062 
00063 //_____________________________________________________________________________
00064 template <class T>
00065 T* 
00066 emcNodeHelper::addObject(PHCompositeNode* node,
00067                          const char* objectName,
00068                          bool transient,
00069                          const char* _nodename)
00070 {
00071   std::string nodename = _nodename;
00072 
00073   if ( nodename.empty() )
00074     {
00075       nodename = objectName;
00076     }
00077 
00078   // First check if object is already there
00079 
00080   T* object = getObject<T>(nodename.c_str(),node);
00081 
00082   if ( !object )
00083     {
00084       object = new T;
00085       PHObject* ph = dynamic_cast<PHObject*>(object);
00086       if (!ph)
00087         {
00088           std::cerr << __FILE__ << ":" << __LINE__ 
00089                << " object is not a PHObject !"
00090                << std::endl;
00091           delete object;
00092           return 0;
00093         }
00094 
00095       PHIODataNode<T>* onode = 
00096         new PHIODataNode<T>(object,nodename.c_str(),"PHObject");
00097 
00098       if ( transient ) 
00099         {
00100           onode->makeTransient();
00101         }
00102 
00103       node->addNode(onode);
00104     }
00105   else
00106     {      
00107       // object alreay exists, we do nothing.
00108       std::cout << __FILE__ << ":" << __LINE__ << " emcNodeHelper::addObject : "
00109            << "Object " << objectName << " is already in node "
00110            << nodename << ". Doing nothing." 
00111            << std::endl;
00112     }
00113 
00114   return object;
00115 }
00116 
00117 //_____________________________________________________________________________
00118 template <class T>
00119 void
00120 emcNodeHelper::insertObject(PHCompositeNode* node,
00121                             T* object,
00122                             const char* objectName,
00123                             bool transient,
00124                             const char* _nodename)
00125 {
00126   std::string nodename = _nodename;
00127   
00128   if ( nodename.empty() )
00129     {
00130       nodename = objectName;
00131     }
00132 
00133   // Check it's really a PHObject
00134   PHObject* test = dynamic_cast<PHObject*>(object);
00135   if ( !test && object ) 
00136     {
00137       std::cerr << __FILE__ << ":" << __LINE__ 
00138            << " object is not a PHObject!"
00139            << std::endl;
00140       return;
00141     }
00142 
00143   // First check if object is already there
00144   
00145   T* cobject = getObject<T>(nodename.c_str(),node);
00146 
00147   if ( !cobject )
00148     {
00149       PHIODataNode<T>* onode = 
00150         new PHIODataNode<T>(object,nodename.c_str(),"PHObject");
00151       
00152       if ( transient ) 
00153         {
00154           onode->makeTransient();
00155         }
00156 
00157       node->addNode(onode);
00158     }
00159   else
00160     {      
00161       // object alreay exists, we do nothing.
00162       std::cout << __FILE__ << ":" << __LINE__ << " emcNodeHelper::insertObject : "
00163            << "Object " << objectName << " is already in node "
00164            << nodename << ". Doing nothing." 
00165            << std::endl;
00166     }
00167 }
00168 
00169 
00170 //_____________________________________________________________________________
00171 template <class T>
00172 T* 
00173 emcNodeHelper::addTable(PHCompositeNode* node,
00174                         const char* tablename,
00175                         unsigned int tablesize, 
00176                         bool transient,
00177                         const char* _nodename)
00178 {
00179   std::string nodename = _nodename;
00180 
00181   if ( nodename.empty() )
00182     {
00183       nodename = tablename;
00184     }
00185 
00186   // First check if table is already there
00187 
00188   T* table = getTable<T>(nodename.c_str(),node);
00189 
00190   if ( !table )
00191     {
00192       table = new T(tablename,tablesize);
00193       PHTable* test = dynamic_cast<PHTable*>(table);
00194       if (!test)
00195         {
00196           std::cerr << __FILE__ << ":" << __LINE__ 
00197                << " table is not a PHTable !"
00198                << std::endl;
00199           delete table;
00200           return 0;
00201         }
00202 
00203       PHIODataNode<PHTable>* tnode = 
00204         new PHIODataNode<PHTable>(table,nodename.c_str(),"PHTable");
00205 
00206       if ( transient ) 
00207         {
00208           tnode->makeTransient();
00209         }
00210 
00211       node->addNode(tnode);
00212     }
00213   else
00214     {      
00215       // table alreay exists, we do nothing.
00216       std::cout << __FILE__ << ":" << __LINE__ << " emcNodeHelper::addTable : "
00217            << "Table " << tablename << " is already in node "
00218            << nodename << ". Doing nothing." 
00219            << std::endl;
00220     }
00221 
00222   return table;
00223 }
00224 
00225 //_____________________________________________________________________________
00226 template<class T>
00227 T*
00228 emcNodeHelper::getObject(const char* objectName, PHCompositeNode* topNode)
00229 {
00230   assert(topNode!=0);
00231   PHNodeIterator iter(topNode);
00232 
00233   PHIODataNode<T>* node = 
00234     static_cast<PHIODataNode<T>*>(iter.findFirst("PHIODataNode",
00235                                                  objectName));
00236 
00237   T* rv=0;
00238 
00239   if ( node ) 
00240     {
00241       rv = static_cast<T*>(node->getData());
00242     }
00243   return rv;
00244 }
00245 
00246 //_____________________________________________________________________________
00247 template<class T>
00248 T*
00249 emcNodeHelper::getTable(const char* tableName, PHCompositeNode* topNode)
00250 {
00251   assert(topNode!=0);
00252   PHNodeIterator iter(topNode);
00253 
00254   PHIODataNode<PHTable>* node = 
00255     static_cast<PHIODataNode<PHTable>*>(iter.findFirst("PHIODataNode",
00256                                                        tableName));
00257 
00258   T* rv=0;
00259 
00260   if ( node ) 
00261     {
00262       rv = static_cast<T*>(node->getData());
00263     }
00264   return rv;
00265 }
00266 
00267 #endif