Processor.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include "cafe/Processor.hpp"
00004 
00005 #include "cafe/Plugins.hpp"
00006 #include "cafe/Config.hpp"
00007 #include "cafe/Stat.hpp"
00008 
00009 #include <assert.h>
00010 #include <iostream>
00011 #include <string>
00012 
00013 // for gDirectory
00014 #include "TROOT.h"
00015 #include "TClass.h"
00016 #include "TMethodCall.h"
00017 #include "TInterpreter.h"
00018 
00019 extern cafe::Stat* STAT ; 
00020 
00021 namespace cafe {
00022 
00023     Processor::Processor(const char *name)
00024         : _eventCount(0),
00025           _debug(0),
00026           _name(name),
00027           _directory(0),
00028           _parent(0)
00029     {}
00030 
00031     Processor::Processor()
00032         : _eventCount(0),
00033           _debug(0),
00034           _name(""),
00035           _directory(0),
00036           _parent(0)
00037     {}
00038 
00039     Processor::~Processor()
00040     {}
00041 
00042 
00043     void Processor::begin() 
00044     {
00045     }
00046 
00047     void Processor::finish()
00048     {
00049     }
00050 
00051     void Processor::inputFileOpened(TFile *file)
00052     {
00053     }
00054 
00055     void Processor::inputFileClosing(TFile *file)
00056     {
00057     }
00058 
00059     bool Processor::processEvent(cafe::Event& event)
00060     {
00061         return true;
00062     }
00063 
00064     unsigned int Processor::debug() const
00065     {
00066         return _debug;
00067     }
00068     
00069     void Processor::setDebug(unsigned int level)
00070     {
00071         _debug = level;
00072     }
00073 
00074     void Processor::incEventCount()
00075     {
00076         _eventCount++;
00077         if (STAT) STAT->EventSelected("PROCESSOR \"" + name() + "\" (input)") ;
00078     }
00079 
00080     int  Processor::eventCount() const
00081     {
00082         return _eventCount;
00083     }
00084 
00085     std::string Processor::name() const 
00086     {
00087         return _name;
00088     }
00089 
00090     void Processor::setDirectory(TDirectory *dir)
00091     {
00092         _directory = dir;
00093     }
00094 
00095     TDirectory *Processor::getDirectory() const
00096     {
00097         return _directory  ? _directory : (_parent != 0 ? (_directory = _parent->getDirectory()) : _directory = gROOT);
00098     }
00099 
00100 
00101     void  Processor::setParent(Processor *parent)
00102     {
00103         _parent = parent;
00104     }
00105 
00106 
00107     Processor *Processor::getParent() const
00108     {
00109         return _parent;
00110     }
00111 
00112     std::string Processor::resolve(const std::string& name, bool recurse) const
00113     {
00114         const Processor *proc = this;
00115 
00116         std::string result = "";
00117 
00118         while(proc != 0) {
00119             Config conf(proc->name());
00120             result = conf.get(name, "");
00121             if((result != "") || !recurse) break;
00122             proc = proc->getParent();
00123         }
00124 
00125         return result;
00126     }
00127 
00128     std::string Processor::replace(const std::string& input, bool recurse) const
00129     {
00130         using namespace std;
00131 
00132         string result(input);
00133         string::size_type pos = 0;
00134 
00135         while(pos != string::npos) {
00136             pos = result.find("%{", pos);
00137             if(pos != string::npos) {
00138                 string::size_type end = result.find('}', pos);
00139                 if(end != string::npos) {
00140                     result.replace(pos, end - pos + 1, resolve(result.substr(pos + 2, end - pos - 2), recurse));
00141                 }
00142                 pos = end;
00143             }
00144         }
00145         return result;
00146     }
00147 
00148     std::string Processor::fullName() const
00149     {
00150         // Return a path starting with 'name()'
00151         //
00152         // Not efficient but correct
00153         //
00154         if(_parent != 0) {
00155             std::string result = _parent->fullName();
00156             result += "/";
00157             result += _name;
00158             return result;
00159         } else {
00160             return "/" + _name;
00161         }
00162     }
00163 
00164     std::ostream& Processor::out()
00165     {
00166         return std::cout;
00167     }
00168 
00169     std::ostream& Processor::info()
00170     {
00171         return std::cerr;
00172     }
00173 
00174     std::ostream& Processor::warn()
00175     {
00176         return std::cerr;
00177     }
00178 
00179 
00180     std::ostream& Processor::err()
00181     {
00182         return std::cerr;
00183     }
00184 
00185 
00186     // static 
00187     Processor* Processor::Create(const std::string& name, const std::string& instance)
00188     {
00189 
00190         // Controller always does this right, but protect against users
00191         std::string inst = instance;
00192         if(inst == "") {
00193             inst = name;
00194         }
00195 
00203         if(TClass *cl = gROOT->GetClass (name.c_str())) {
00204 
00205             std::string::size_type pos = name.rfind(':');
00206             std::string n = name;
00207             if(pos != std::string::npos) {
00208                 n = name.substr(pos + 1);
00209             }
00210             
00211             if(cl->GetMethodWithPrototype(n.c_str(),"const char *") != 0) {
00212                 TMethodCall call;
00213                 call.InitWithPrototype(cl, n.c_str(), "const char *");
00214                 call.ResetParam();
00215                 call.SetParam((Long_t )inst.c_str());
00216                 Long_t ret;
00217                 call.Execute(ret);
00218                 return reinterpret_cast<Processor*>(ret);
00219             }
00220         }
00221 
00225 
00226         Plugins *plugins = Plugins::instance();
00227         return plugins->load<Processor>("Processor", name, instance);
00228     }
00229 
00230 }
00231 
00232 ClassImp(cafe::Processor)

Generated on Thu Apr 3 04:14:23 2008 for CAF by doxygen 1.3.4