BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
/////////////////////////////////////////////////////////////////
//
//  BrCalibrationManager
//
//  This manager class will be the owner of the DetectorParameter
//  Objects. Its intended use if for a user module to access database
//  only via this mechanism, and not by opening any files by
//  themselves. It also ensures that  in the case that several user
//  modules uses a given geometry it is ensured that 
//  a) The refer to the same object
//  b) The application can use the setter methods to change values for a
//     geometry constant and will thus be the same for all modules.
//  c) The objects are owned by the manager and will be deleted only
//     when the program exists (The instance is a static object). 
//
//  Sample code:
//
//  The application can load the Manager and keep a global pointer
//  accesible via a Brat common application file. It is in fact 
//  implemented via a Singleton mechanism. This enables us to use both
//  the global mechanism as well as the instantons.
//
//  Main application will have
//  ---------------------------
//  BrCalibrationManager *gParamDb =
//    BrCalibrationManager::Instance();
//
//
//  The user module should have (either in the Named Constructor 
//  or in the Init() method.
//  ------------------------------------
//  BrCalibrationManager *gParamDb =
//    BrCalibrationManager::Instance();
//
//_______________________________________________________________________
//
// BrDbElement
// 
// This class is used only by the manager class.
// The objects are used to maintain what databases objects has been
// loaded via this mechanism.
//

//
// $Id: BrCalibrationManager.cxx,v 1.3 2002/03/21 23:41:37 cholm Exp $
// $Author: cholm $
// $Date: 2002/03/21 23:41:37 $
// $Copyright: (c) 2001 BRAHMS Collaboration <brahmlib@rcf.rhic.bnl.gov>
//

#include <cstdio>
#include <iostream.h>
#include <iomanip.h>
#include <CallFunc.h>
#include <TClass.h>
#include "BrCalibrationManager.h"
#include <BrCalibration.h>
#include <BrException.h>

//____________________________________________________________________
ClassImp(BrCalibrationManager);

//____________________________________________________________________
//
// Static instance of  
// 
BrCalibrationManager* BrCalibrationManager::fInstance=0;

//____________________________________________________________________
BrCalibrationManager::BrCalibrationManager()
{
  fObjectlist = new TObjArray();
  fState = INIT;
  
}

//____________________________________________________________________
BrCalibrationManager* BrCalibrationManager::Instance()
{
  if( fInstance == 0)
    fInstance = new BrCalibrationManager;
  return fInstance;
}

//____________________________________________________________________
BrCalibrationManager::~BrCalibrationManager()
{
  if(!fObjectlist)
    fObjectlist->Delete();
  delete fObjectlist;
}

//____________________________________________________________________
BrCalibration* BrCalibrationManager::Register(const Char_t* classname, 
					      const Char_t* detector)
{
  // Return a pointer to the concrete class.
  // This is the only place where specific references to concrete class
  // are given. But in order to avoid cyclic dependencies, references
  // are made indirectly through the G__CallFunc interface.  That also
  // makes this routine much more general.  It should not have to be
  // modified whenever a new parameter class is added.  The only thing
  // one has to take care of is that the parameter class inherits from
  // TObject, or better yet (I think) TNamed.  They must also have a
  // constructor with 3 arguments that are Char_t *'s, eg  
  //   BrDetectorParamsTof::BrDetectorParamsTof(Char_t *name,
  //                                            Char_t *title,
  //                                            Char_t *filename)
  
  BrDbElement* obj;
  
  if(DebugLevel() > 1) {
    cout <<"BrCalibration Requestn ";
    cout << classname << " : " <<  detector <<endl;
  }
  if((obj = GetDbElement(classname, detector))) {
    if(DebugLevel() > 0){
      cout << "Found " << detector << "n";
      cout << obj << endl;
    }
    return (BrCalibration*) (obj->fParameterElement);
  }
  
  // Create a TClass for classname, e.g. BrDetectorParamsTof
  TClass *cl = new TClass(classname,0);
  G__CallFunc func;
  void *address;
  long  offset;
  
  //    Set the call function to the constructor of the class we want.
  //    In this case we hope the user is asking for a parameter class,
  //    but for the purposes of this routine, it will not make any
  //    difference as long as there is a method of classname with
  //    arguments of two Char_t *'s 
  func.SetFunc(cl->GetClassInfo()->GetMethod(classname,
					     "Char_t *,Char_t *", 
					     &offset).InterfaceMethod());
  
  //    Set arguments to what we want for the arguments of the constructor
  func.SetArg((long)detector);           //Name
  func.SetArg((long)detector);           //Title
  
  
  //    Change the type of offset to be used as an argument to ExecInt
  address = (void*)offset;
  
  // This actually creates an instance of the class.
  TObject *par = (TObject*)func.ExecInt(address);
  
  if(!par){
    //    Warn user that object was not found.  Then return before getting
    //    an access violation
    Warning("BrCalibrationManager",
	    "Cannot execute default constructor for %s",classname);
    return 0;
  }
  if(DebugLevel() > 0)
    cout << " Adding " << classname << " for " << detector << endl;

  obj    = new BrDbElement(classname, detector, par);
  fObjectlist->Add(obj);


  delete cl;
  // Return the parameter we have been after all along
  return (BrCalibration*) par;

}


//____________________________________________________________________
BrDbElement* BrCalibrationManager::GetDbElement(const Char_t* classname, 
						const Char_t* detector)
{
  //
  //  Find a geometry database member with the requested detector and
  //  in the  given class.
  //
  TIter next(fObjectlist);
  BrDbElement* dbobj;
  while ((dbobj = (BrDbElement*) next())){
    if( !strcmp(detector, dbobj->fDetectorName) &&
          !strcmp(classname, dbobj->fClassName)) return dbobj; 
  }
  return 0;
}

//____________________________________________________________________
void BrCalibrationManager::Print(Option_t* option) const
{
  // Print information on the manager to std out.  
  // Options 
  //    S           Status 
  //    L           List of BrCalibrations manged by this manager
  cout << "BrCalibrationManager" << endl;
  TString opt(option);
  opt.ToLower(); 
  if (opt.Contains("s")) {
    cout << "Status:    " << flush;
    switch (fState) {
    case INIT:                   cout << "init" << endl;    break;
    case OPEN:                   cout << "open" << endl;    break;   
    case DEFAULT_PARAMETER_SET:  cout << "default" << endl; break;
    default:                     cout << "unknown" << endl; break; 
    }
  }

  if (opt.Contains("l")) {
    cout << "+--------------------------------+----------------+" << endl
	 << "| ParameterElement Class         | Detector       |" << endl
	 << "+--------------------------------+----------------+" << endl;
    TIter next(fObjectlist);
    BrDbElement* dbobj;
    while ((dbobj = (BrDbElement*) next()))
      dbobj->Print();
    cout << "+--------------------------------+----------------+" << endl;
  }
}

//____________________________________________________________________
void BrCalibrationManager::Init()
{
  //
  // Call the Init method for all of the ParameterObjects that has been
  // registered.
  //
  TIter next(fObjectlist);
  BrDbElement* dbobj;
  if(DebugLevel())
    cout << "BrCalibrationManager::Init()" << endl;
  while ((dbobj = (BrDbElement*) next())) {
    BrCalibration* parameter = 
      (BrCalibration*) dbobj->fParameterElement;
    parameter->Init();
  }
}

//____________________________________________________________________
void BrCalibrationManager::Update(Int_t start, Int_t stop)
{
  //
  // Call the Update method for all of the ParameterObjects that has
  // been registered.
  //
  if(DebugLevel())
    cout << "BrCalibrationManager::Update( " << start << ", " << stop
	 << " )" << endl;


  TIter next(fObjectlist);
  BrDbElement* dbobj;

  while ((dbobj = (BrDbElement*) next())) {
    BrCalibration* element = 
      (BrCalibration*) dbobj->fParameterElement;
    try{
      if(DebugLevel())
	cout << "..Updating " << dbobj->fClassName << endl;
      // Update each element in turn 
      element->Update(start, stop);
    }
    // Catch anything bad? 
    catch(BrException* e) {
      cerr << "*** BrCalibrationManager (Update) :n "
	   << dbobj->fClassName << " " << dbobj->fClassName << "n"
	   << *e << endl;
      e->Execute();
    }
  }
  // Tell all calibrations to close the connection 
  next.Reset();
  while ((dbobj = (BrDbElement*) next())) {
    BrCalibration* element = 
      (BrCalibration*) dbobj->fParameterElement;
    element->Close();
  }
}

//____________________________________________________________________
void BrCalibrationManager::Commit(Int_t start, Int_t stop){
  //
  // Call the Commit method for all of the ParameterObjects that has
  // been registered.
  //
  if(DebugLevel())
    cout << "BrManager::Commit( "<<start<< ", "<<stop<<" )" << endl;


  TIter next(fObjectlist);
  BrDbElement* dbobj;


  while ((dbobj = (BrDbElement*) next())) {
    BrCalibration* element = 
      (BrCalibration*) dbobj->fParameterElement;
    try{
      // Commmit each element in turn 
      element->Commit(start, stop);
    }
    catch(BrException* e) {
      cerr << "*** BrCalibrationManager (Commit) :n "
	   << dbobj->fClassName << dbobj->fClassName << "n"
	   << *e << endl;
      e->Execute();
    }
  }  
  // Tell all calibrations to close the connection 
  next.Reset();
  while ((dbobj = (BrDbElement*) next())) {
    BrCalibration* element = 
      (BrCalibration*) dbobj->fParameterElement;
    element->Close();
  }
}

//____________________________________________________________________
//
// BrDbElement  
// 
// Utility class used by BrCalibrationManager. 
// 

//____________________________________________________________________
ClassImp(BrDbElement);

//____________________________________________________________________
 BrDbElement::~BrDbElement()
{
  if(!fParameterElement)
    delete fParameterElement;
}

//____________________________________________________________________
 BrDbElement::BrDbElement(const Char_t* classname, 
			 const Char_t* detectorname, 
			 TObject* obj)
{
  strcpy(fClassName,   classname);
  strcpy(fDetectorName, detectorname);
  fParameterElement= obj;
}

//____________________________________________________________________
 void BrDbElement::Print(Option_t* option) const 
{
  // 
  cout << "|" << setw(32)<<  fClassName << "|"
       << setw(16)<< fDetectorName << "|" << endl;
}

//____________________________________________________________________
ostream& operator << (ostream& os, BrDbElement* dbobj)
{
  os   << "|" << setw(32)<<  dbobj->fClassName << "|"
       << setw(16)<< dbobj->fDetectorName << "|n";
  return os;
}

///////////////////////////////////////////////////////////////////////////////
//
//  $Log: BrCalibrationManager.cxx,v $
//  Revision 1.3  2002/03/21 23:41:37  cholm
//  Added code in Update and Commit, so that as soon as the connection to the
//  DB server isn't needed anymore from this Update/Commit step, we
//  disconnect our clients.  This is done, so we may keep as many free
//  slots on the server as possible.  MySQL, in it's current setup has a
//  limit of 1000 simultaneous connections, and so disconnecting from the
//  server may allow more jobs to run.  This `Disconnect ASAP' policy is
//  conditional on the preprocessor flag BRAT_USE_DISCONNECT, defined in
//  `db/abc/BrDb.h'.
//
//  Revision 1.2  2001/12/07 21:17:29  videbaek
//  Removed the methos UseForWrite and UseForRead.
//  Some cleanup in comments, and few other not needed statements.
//
//  Revision 1.1  2001/10/08 10:40:25  cholm
//  * Renamed BrParameterElement[Manager] to BrCalibration[Manager]
//  * Impacts some modules, and maybe user code too.
//  * Made the access class BrCalibrationsDb 'polymorphic'
//  * All database table representations have class version > 0 (persistent)
//
//  Revision 1.3  2001/09/23 01:49:59  videbaek
//  Modified the ParameterElement to deal in method suggested by Christian
//  Use(name, method, noelements).
//  This was implemented and checked on 9/22/01 and seems ok. The
//  previous methods UserForWrite and UseForLoad were kept.
//
//  Revision 1.2  2001/09/12 15:11:36  cholm
//  Cleaned up. Removed List in favour of Print.
//
//  Revision 1.1.1.1  2001/06/21 14:55:17  hagel
//  Initial revision of brat2
//
//  Revision 1.6  2001/06/18 10:32:08  cholm
//  Changed the connection scheme to the databases.  No more explicit passwords,
//  and better handling for diconnecting from the databases.  Password is stored
//  internally, so that one may connect and disconnect with out the need to read
//  password from file or user input over and over again.  Preferably, the
//  password should be stored in some kind of same memory or encrypted.  However,
//  for the time being I'm ok with this, since attaching a debugger is probably
//  only allowed for the owning user, so no-one can peek at the memory.
//
//  Revision 1.5  2001/06/05 18:42:12  cholm
//  Removed BrDbInc.h an all references to it
//
//  Revision 1.4  2000/12/18 17:07:26  videbaek
//  Added debug statement
//
//  Revision 1.3  2000/06/03 18:12:55  videbaek
//  Many update to the ParamemertElement and related classes for the calibration access.
//
//  Revision 1.2  2000/05/17 10:43:17  cholm
//  Test for reading Calibrations DB added
//
//  Revision 1.1  2000/05/16 16:16:33  videbaek
//  Added the database utility classes for dealing with calibrations.
//  These are ParameterElement and ParamterElementManager. Each of these two
//  classes has intrinsic utility classes.
//
//

This page automatically generated by script docBrat by Christian Holm

Copyright ; 2002 BRAHMS Collaboration <brahmlib@rcf.rhic.bnl.gov>
Last Update on by

Validate HTML
Validate CSS