BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
//////////////////////////////////////////////////////////////////
//
//  BrGeometryDbManager
//
// This manager class will be the owner of the DetectorVolume 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 Singletom mechanism. This enables us to use both
// the global mechanism as well as the instantons.
//
//  Main application will have
//  ---------------------------
//  BrGeometryDbManager *gGeomDb = BrGeometryDbManager::Instance();
//
//    gGeomDb->SetDbFileName("mrs.geo");
//    gGeomDb->SetDbFileName("mrs.mag");
//    gGeomDb->SetDbFileName("mysim.geo");
//
// The files should be search in order given. This means that if a
// given detector is not found in the first the search
// continues. Note as of now Magnetvolume are derived from
// BrDetectorVolume  
//
// The user module should have in the Init()method. Could possible
// be in a Begin() in case geometry changes by run.
//
//    ------------------------------------
//    BrGeometryDbManager *geomdb = BrGeometryDbManager::Instance();
//
//    BrDetectorVolume* t1_vol = (BrDetectorVolume*) 
//             geomdb->GetDetectorVolume("BrDetectorVolume",name);
//    BrMagnetVolume* d1_vol = (BrMagnetVolume*) 
//             geomdb->GetDetectorVolume("BrMagnetVolume",name);
//
// where name may have been obtained from either the name of the module
// example BrDigitizeTPC  name == GetName()
//  
//
//  $Id: BrGeometryDbManager.cxx,v 1.11 2002/04/16 14:44:12 hagel Exp $
//  $Author: hagel $
//  $Date: 2002/04/16 14:44:12 $
//
#include <cstdio>
#include <BrIostream.h>
//
// ROOT classes
#include "TROOT.h"
#include <CallFunc.h>
#include <TClass.h>
#include <TMethodCall.h>
#include <TObjString.h>
//
// Brat classes
#include "BrGeometryDbManager.h"
#include "BrDetectorVolume.h"
#include "BrMagnetVolume.h"

//classes for access to MySQL DB
#include "BrRunInfoManager.h"
#ifndef BRAT_BrGeometriesDb
#include "BrGeometriesDb.h"
#endif

#include <vector>

//____________________________________________________
ClassImp(BrGeometryDbManager);

//____________________________________________________
//
// Static instance of  
// 
BrGeometryDbManager* BrGeometryDbManager::fInstance=0;

//____________________________________________________
//
 BrGeometryDbManager::BrGeometryDbManager(){
  fObjectlist = new TObjArray();
  fAsciiVolumeList = new TObjArray;
  fDebugLevel=0;
  fDbMode = kModeASCII;     //default is to stay with the old way
}

//____________________________________________________ 
 BrGeometryDbManager* BrGeometryDbManager::Instance(){
  if( fInstance == 0)
      fInstance = new BrGeometryDbManager;
  return fInstance;
}
//__________________________________________________
  
 BrGeometryDbManager::~BrGeometryDbManager(){
  if(!fObjectlist)
    fObjectlist->Delete();
  delete fObjectlist;
  fFileList.Delete();
}

//_________________________________________________________

 void BrGeometryDbManager::SetDbFileName(Char_t* filename){
  //Set ASCII Db filename.  Error given if we are in MySQL mode

  if(fDbMode == kModeMySQL) {
     cerr<<"BrGeometryDbManager::SetDbFileName not permitted in MySQL mode";
     cerr<<endl;
     return;
     }

  //We are not in MySQL mode, we can do the operation
  TObjString* obj = new TObjString(filename);
  fFileList.Add(obj);
}


//_________________________________________________________
//
 TObject* BrGeometryDbManager::GetDetectorVolume(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
  //

  BrDbObject* obj;

  if(fDebugLevel > 1){
    cout <<"GetDetectorVolume Requestn ";
    cout << classname << " : " <<  detector <<endl;
  }
  
if((obj = GetDbObject(classname, detector)))
    {
    if(fDebugLevel > 0){
      cout << "Found " << detector << "n";
      cout << obj << endl;
    }
      return (obj->fDetectorGeometry);
    }
  else
    {
      if(fDbMode == kModeMySQL) {
        //This is all we do if we are in MySQL mode.  
        //Get the object, add to object list and get out
	TObject *volumep = GetDetectorVolumeFromMySQL(classname,detector);
        obj   = new BrDbObject(classname, detector,  volumep);
        fObjectlist->Add(obj);
	return volumep;
      }

      //come here if still in ASCII mode
      //
      // Create a TClass for classname, e.g. BrDetectorVolume, 
      //
      //TClass *cl = new TClass(classname,0); //This causes problems in root_v3
      TClass *cl = gROOT->GetClass(classname);
      G__CallFunc func;
      void *address;
      long  offset;
      //    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 *volumep = (TObject*)func.ExecInt(address);
      
      if(!volumep){
	//    Warn user that object was not found.  Then return before getting
	//    an access violation
	Warning("BrGeometryDbManager",
		"Cannot execute default constructor for %s",classname);
	//delete cl;
	return 0;
      }
      
      //
      // Now it has been created we want to fill it with the information from the
      // database (file). This will be done by calling the Method GetFromFile
      // using the method calls.
      //
      TMethodCall* call = new TMethodCall(cl, "ReadASCIIFile",""file.geo","Detector" ");

      Bool_t result = kFALSE;
      for(int k=0; k<fFileList.GetEntries();k++){
	
	TObjString*  obj = (TObjString*) fFileList.At(k);
	TString string(obj->GetString());
	Long_t ret;
	call->Execute(volumep, Form(""%s","%s"",obj->GetName(),detector), ret);
	result = (Bool_t) ret;
	if(result) break;
      }

      if(!result)
	{
	  Warning("GetDetectorVolume","For %s Failed",detector);
	  delete volumep;
	  //delete cl;
	  delete call;
	  return 0;
	}
      
      if(fDebugLevel > 0)
	cout << " Adding " << classname << " for " << detector << endl;
      obj   = new BrDbObject(classname, detector,  volumep);
      fObjectlist->Add(obj);

      //delete cl;
      delete call;
      return volumep;
    }
}


//______________________________________________________________________

 BrDbObject* BrGeometryDbManager::GetDbObject(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);
  BrDbObject* dbobj;
  while ((dbobj = (BrDbObject*) next())){
    if( !strcmp(detector, dbobj->fDetectorName) &&
          !strcmp(classname, dbobj->fClassName)) return dbobj; 
  }
  return 0;
}

//______________________________________________________________________

 void BrGeometryDbManager::List(){
  TIter next(fObjectlist);
  BrDbObject* dbobj;
  cout << "BrDbManager Object List" << endl;
  while ((dbobj = (BrDbObject*) next()))
    cout << dbobj ;
}

//Net methods providing access to BRAHMS MySQL DB
 void BrGeometryDbManager::SetDbModeMySQL() {
  //Set the db access mode to mysql

  fDbMode = kModeMySQL;
}

//________________________________________________________________
 void BrGeometryDbManager::Update() {
  //Update the geometry objects
  //Do only if in MySQL mode.  If not in MySQL mode, politely decline
  //to do anything

  if(fDbMode != kModeMySQL) return;

  //Now, we are in business.
  TIter nextVol(fObjectlist);
  BrDbObject *dbObj;
  while((dbObj = (BrDbObject*)nextVol())) {
     BrDetectorVolume *vol = (BrDetectorVolume*)dbObj->fDetectorGeometry;
     vol->Update();
     }
}
//________________________________________________________________
 TObject* BrGeometryDbManager::GetDetectorVolumeFromMySQL(const Char_t* classname, const Char_t* detector) {
  //Build the volume objects by getting appropriate entries from BRAHMS Db

  BrDbObject* obj;
  if((obj = GetDbObject(classname, detector))) {
     if(fDebugLevel > 0){
        cout << "Found " << detector << "n";
        cout << obj << endl;
        }
        return (obj->fDetectorGeometry);
     }
  else {
     TString classString(classname);
     if(classString.Contains("BrDetectorVolume")) {
        return BuildDetectorVolume(detector);
        }
     if(classString.Contains("BrMagnetVolume")) {
        return BuildMagnetVolume(detector);
        }
     cerr<<"Error, GetDetectorVolumeFromMySQL; unknown volume class: ";
     cerr<<classname;
     cerr<<endl;
     return 0;
     }

}

//______________________________________________________________________
 TObject* BrGeometryDbManager::BuildDetectorVolume(const Char_t* name) {
  //Build the detector volume from entries in MySQL database
  //Volume is created here.  It builds itself either from the 
  //MySQL database or from an ASCII file if it finds it's name in the
  //fAsciiVolumeList.

  //Create the volume
  BrDetectorVolume *newVolume = new BrDetectorVolume(name,fAsciiVolumeList);

  //Now, fill it using update
  newVolume->Update();

  return newVolume;
}

//______________________________________________________________________
 TObject* BrGeometryDbManager::BuildMagnetVolume(const Char_t* name) {
  //Build the magnet volume from entries in MySQL database
  //Volume is created here.  It builds itself either from the 
  //MySQL database or from an ASCII file if it finds it's name in the
  //fAsciiVolumeList.

  //Create the volume
  BrMagnetVolume *newVolume = new BrMagnetVolume(name,fAsciiVolumeList);

  //Now fill it using update
  newVolume->Update();

  return newVolume;


}

//______________________________________________________________________
 void BrGeometryDbManager::SetAsciiRead(const Char_t *vol,const Char_t *file) {
//Set a volume name to be read from a particular file

fAsciiVolumeList->Add(new BrAsciiVolumeMap(vol,file));
}

ClassImp(BrDbObject)

//______________________________________________________________________
BrDbObject::~BrDbObject(){
  if(!fDetectorGeometry)
     delete fDetectorGeometry;
}
//______________________________________________________________________

BrDbObject::BrDbObject(const Char_t* classname, const Char_t* detectorname, TObject* obj){
strcpy(fClassName,classname);
strcpy(fDetectorName,detectorname);
fDetectorGeometry= obj;
}

//______________________________________________________________________
ostream& operator << (ostream& os, BrDbObject* dbobj){
  os   << "  " <<   dbobj->fClassName << "n"
       << "  " << dbobj->fDetectorName << "n"
       << endl;
  return os;
}

//  $Log: BrGeometryDbManager.cxx,v $
//  Revision 1.11  2002/04/16 14:44:12  hagel
//  Implement Hall Probe reading from Db in ySQL mode.  Major surgery on BrGeometryDbManager concerning building BrDetectorVolume and BrMagnetVolume
//
//  Revision 1.10  2002/03/28 18:08:44  hagel
//  Implemented ASCII read and write for BrDbDetectorVolume
//
//  Revision 1.9  2002/02/08 22:55:48  hagel
//  Take advantage of new features of BrRotMatrix
//
//  Revision 1.8  2002/01/31 15:24:06  ouerdane
//  added verbosity level and associated setter
//
//  Revision 1.7  2001/11/05 23:41:42  hagel
//  Changes to MySQL mode for Geometry DB manager
//
//  Revision 1.6  2001/10/26 17:09:03  hagel
//  Eliminate multiple reads from DB in MySQL mode
//
//  Revision 1.5  2001/09/21 19:34:34  videbaek
//  Remove a non-qualified printout.
//
//  Revision 1.4  2001/08/03 09:19:09  hagel
//  Small iteration on Geometry DB stuff
//
//  Revision 1.3  2001/08/02 03:13:46  hagel
//  Yet another iteration on GeometryDB; this should be close to last
//
//  Revision 1.2  2001/06/22 02:56:48  hagel
//  Improvements in GeometryDB
//
//  Revision 1.1.1.1  2001/06/21 14:55:18  hagel
//  Initial revision of brat2
//
//  Revision 1.17  2001/06/05 18:41:40  cholm
//  Removed BrDbInc.h an all references to it
//
//  Revision 1.16  2001/05/07 21:22:04  hagel
//  Next iteration of Geometry DB
//
//  Revision 1.15  2001/04/20 16:13:50  hagel
//  Rework MySQL mode of BrMagnetVolume
//
//  Revision 1.14  2001/03/07 17:45:20  hagel
//  Implement MySQL DB access support
//
//  Revision 1.13  2000/11/23 01:31:58  brahmlib
//  Changed the returned object from operator+.
//
//  Revision 1.12  2000/11/22 21:15:36  videbaek
//  Geometry manager updated to deal with modified Magnet and DetectorVolume
//  as well as with multiple file definitions.
//
//  Revision 1.11  2000/11/17 22:23:13  videbaek
//  Clean up of cvsinfo and log
//
//  Revision 1.10  2000/05/10 15:56:13  nbi
//  Added the classes BrRunsDb, BrGeometriesDb, changed some code, bug
//  corrections , and so on. It's almost there ;-)
//
//  Revision 1.9  2000/03/21 21:21:55  cholm
//  Several changes: A few hacks where needed to compile on Digital Unix, noticably in my - Christian Holm - classes for the DB and Exceptions. Proberly still need to do something to some of Konstantins stuff. Also, I removed a lot of warnings fromt the compiler, by teddying up the code. Several places, old C-style indecies in for loops were assumed. Corrected. Unused variables have been commented out.
//
//  Revision 1.8  1999/04/21 14:42:07  videbaek
//  Removed diasnostic Instance adress print outs
//
//  Revision 1.7  1999/02/24 15:21:23  hagel
//  Enforce const in arguments
//
//  Revision 1.6  1999/01/15 15:34:53  videbaek
//  Added makeNT (non cygnus makefile for win95)
//  Improved geometryDbManager
//
//  Revision 1.5  1998/12/21 20:20:44  videbaek
//  added BrMagnetVolume as being managed.
//
//  Revision 1.1  1998/12/09 17:01:49  videbaek
//  Add new db manager class. It has been checked out for
//  the simple BrDetectorVolume class.
//

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