Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

AlgFactory.cxx

Go to the documentation of this file.
00001 
00002 // $Id: AlgFactory.cxx,v 1.30 2005/04/13 22:04:10 gmieg Exp $
00003 //
00004 // AlgFactory.cxx
00005 //
00006 // AlgFactory creates Algorithm objects derived from AlgBase.
00007 //
00008 // Author:  G. Irwin 2/2000
00010 
00011 #include <cassert>
00012 
00013 #include "TROOT.h"
00014 #include "TString.h"
00015 #include "TSystem.h"
00016 
00017 #include "Algorithm/AlgConfig.h"
00018 #include "Algorithm/AlgBase.h"
00019 #include "Algorithm/AlgFactory.h"
00020 #include "Algorithm/ConfigRecord.h"
00021 #include "DatabaseInterface/DbiConfigSet.h"
00022 #include "DatabaseInterface/DbiConfigStream.h"
00023 #include "DynamicFactory/DynAlgReg.h"
00024 #include "DynamicFactory/DynamicPluggableFactory.h"
00025 #include "JobControl/JobController.h"
00026 #include "MessageService/MsgService.h"
00027 
00028 ClassImp(AlgFactory)
00029 
00030 //......................................................................
00031 CVSID("$Id: AlgFactory.cxx,v 1.30 2005/04/13 22:04:10 gmieg Exp $");
00032 
00033 // Define static variables.  They get initialized to zero by default.
00034    AlgFactory *AlgFactory::fsTheInstance;
00035 
00036 //______________________________________________________________________
00037 AlgFactory::AlgFactory() :
00038   fConfigRecordIsModified(kFALSE)
00039 {
00040    MSG("Alg", Msg::kDebug) << "AlgFactory::Constructor\n";
00041    fsTheInstance = this;
00042 }
00043 
00044 //______________________________________________________________________
00045 AlgFactory::~AlgFactory()
00046 {
00047    MSG("Alg", Msg::kDebug) << "AlgFactory::Destructor\n";
00048    fAlgHandleList.Delete();
00049    fsTheInstance = 0;
00050 }
00051 
00052 //______________________________________________________________________
00053 AlgFactory &AlgFactory::GetInstance()
00054 {
00055 
00056 // Cleaner destructor calls AlgFactory dtor
00057    static Cleaner cleaner;
00058 
00059    if (!fsTheInstance) {
00060      MSG("Alg", Msg::kDebug) << "Instantiating AlgFactory " << endl;
00061      cleaner.UseMe();           // dummy call to quiet compiler warnings
00062      fsTheInstance = new AlgFactory();
00063    }
00064    return *fsTheInstance;
00065 }
00066 
00067 //______________________________________________________________________
00068 AlgHandle AlgFactory::GetAlgHandle(const char *aalgnm,
00069                                    const char *ccnfnm)
00070 {
00071    const char *algnm = Strip(aalgnm);
00072    const char *cnfnm = Strip(ccnfnm);
00073 
00074    TString regstr(algnm);
00075    regstr = regstr + ":" + cnfnm;
00076    AlgHandle *ah = (AlgHandle *)
00077                                fAlgHandleList.FindObject(regstr.Data());
00078 
00079    if (ah) {
00080      delete [] algnm;  delete [] cnfnm;  // Strip() gives user ownership
00081      return *ah;
00082    }
00083 
00084    DynamicPluggableFactory &dpf =
00085                                  DynamicPluggableFactory::GetInstance();
00086 
00087    Bool_t ljreg = kFALSE;
00088    if ((ljreg = !dpf.IsRegistered(regstr.Data())))
00089                                                  Register(algnm, cnfnm);
00090    AlgBase &ab = dpf.LendAlg(algnm);
00091    AlgConfig &ac = dpf.LendAlgConfig(regstr.Data());
00092    TRef acr = &ac;
00093    ah = new AlgHandle(&ab, acr);
00094 
00095    ah->SetName(regstr.Data());
00096    fAlgHandleList.Add(ah);
00097 
00098    fConfigRecordIsModified = kTRUE;
00099 
00100    if (ljreg) {
00101      TString macroname("DBtxt_");
00102      macroname = macroname + algnm + "_" + cnfnm + ".C";
00103      char *mac = gSystem->Which(gROOT->GetMacroPath(), macroname,
00104                                                        kReadPermission);
00105      if (mac) {
00106        MSG("Alg", Msg::kInfo)
00107                     << "AlgFactory:  (AlgorithmClass:AlgConfigName) = ("
00108            << regstr.Data() << ") parameters set from ROOT macro file: "
00109                                                          << mac << endl;
00110        gROOT->Macro(macroname.Data());
00111      }
00112 
00113      else {            // Get the AlgConfig parameters from the database
00114 
00115 // Here one tries to fill AlgConfig from database.
00116        DbiConfigStream cfstream(algnm,cnfnm);
00117        const DbiConfigSet *cfset = cfstream.GetConfigSet();
00118        if (cfset) {
00119          cfstream >> &ac;            // Get AlgConfig parameters from DB
00120          MSG("Alg", Msg::kInfo)
00121                     << "AlgFactory:  (AlgorithmClass:AlgConfigName) = ("
00122             << regstr.Data() << ") parameters set from DB table entry: "
00123                                        << algnm << "," << cnfnm << endl;
00124        }
00125        else {                               // AlgConfig table not in DB
00126 
00127 // The messages below are printed if the attempt fails.
00128          MSG("Alg", Msg::kError)
00129                     << "AlgFactory:  (AlgorithmClass:AlgConfigName) = ("
00130               << regstr.Data() << ") - failed to find ROOT macro file: "
00131                 << macroname << " in " << gROOT->GetMacroPath() << endl;
00132          MSG("Alg", Msg::kError)
00133                     << "AlgFactory:  (AlgorithmClass:AlgConfigName) = ("
00134            << regstr.Data() << ") parameters not available in database."
00135                              << "  This is likely to be fatal." << endl;
00136        }
00137      }
00138      delete [] mac;      // TSystem::Which() gives ownership to the user
00139    }
00140 
00141    delete [] algnm;  delete [] cnfnm; // Strip() gives ownership to user
00142    return *ah;
00143 }
00144 
00145 //______________________________________________________________________
00146 ConfigRecord *AlgFactory::GetOwnedConfigRecord(const VldContext *vld)
00147 {
00148 
00149 // Only returns a ConfigRecord when the AlgConfig list has been modified
00150    if (fConfigRecordIsModified == kFALSE) return 0;
00151 
00152 // Create new ConfigRecord for owned return to caller.
00153    const RecHeader rh(*vld);
00154    ConfigRecord *confrec = new ConfigRecord(rh);
00155    confrec->HasBeenModified();
00156    confrec->SetTransient(false);// Record not to be cleared by mom.clear
00157    confrec->SetName("AlgConfigRecord");
00158    confrec->SetTitle("Created by AlgFactory");
00159 
00160 // Add AlgConfigs to new ConfigRecord
00161    TIter algiter(&fAlgHandleList);
00162    AlgHandle *ah = 0;
00163    while ((ah = (AlgHandle *) algiter())) {
00164      confrec->SecureConfig(ah->GetAlgConfig());
00165    }
00166 
00167    fConfigRecordIsModified = kFALSE;
00168    return confrec;
00169 }
00170 
00171 //______________________________________________________________________
00172 void AlgFactory::Register(const char *aalgnm, const char *ccnfnm,
00173                           const char *libnm,  const char *cclnm)
00174 {
00175    const char *algnm = Strip(aalgnm);
00176    const char *cnfnm = Strip(ccnfnm);
00177 
00178    TString cclstr(cclnm ? cclnm : "AlgConfig");
00179    TString regstr(algnm);
00180    regstr = regstr + ":" + cnfnm;
00181    DynamicPluggableFactory &dpf =
00182                                  DynamicPluggableFactory::GetInstance();
00183    if (dpf.IsRegistered(regstr.Data())) {
00184      MSG("Alg", Msg::kWarning)
00185        << "AlgFactory:  (AlgorithmClass:AlgConfigName) = ("
00186        << regstr.Data() << ") already registered.  2nd attempt Ignored."
00187        << endl;
00188      delete [] algnm;  delete [] cnfnm;  // Strip() gives user ownership
00189      return;
00190    }
00191 
00192    DynAlgReg *dar = new DynAlgReg(regstr.Data(), algnm, libnm,
00193                                   cclstr.Data());
00194    dpf.Register(dar);
00195 
00196    MSG("Alg", Msg::kDebug)
00197     << "AlgFactory:  Registration of (AlgorithmClass:AlgConfigName) = ("
00198     << regstr.Data() << ") completed." << endl;
00199 
00200    delete [] algnm;  delete [] cnfnm;    // Strip() gives user ownership
00201    return;
00202 }

Generated on Mon Feb 15 11:06:17 2010 for loon by  doxygen 1.3.9.1