BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
//
//
//

// $Id: BrRdbmGeometriesDb.cxx,v 1.3 2002/06/06 23:04:22 hagel Exp $
// $Author: hagel $
// $Date: 2002/06/06 23:04:22 $
// $Copyright: 2000 Brahms Collaboration

#include <BrRdbmGeometriesDb.h>
#ifndef BRAT_BrException
#include "BrException.h"
#endif
#ifndef WIN32 
#include <cstdlib>
#include <iostream>
#else 
#include <cstdlib>
#include <iostream.h>
#endif

//____________________________________________________________________
ClassImp(BrRdbmGeometriesDb);

//____________________________________________________________________
 BrRdbmGeometriesDb::BrRdbmGeometriesDb(const Char_t* name, 
				       const Char_t* title) 
{
  // CTOR
  fImplementation = new BrRdbmDb(name, title);
}

//____________________________________________________________________
 BrGeometriesDb* BrRdbmGeometriesDb::Instance(void) 
{
  if (!fgInstance) 
    fgInstance = new BrRdbmGeometriesDb("", "");
  return fgInstance;
}

//____________________________________________________________________
 TObjArray* BrRdbmGeometriesDb::GetXDetectorVolume(const Char_t* condition,Bool_t selectLatestRev) {
  // Find Runs that matches condition <condition>
  //If latestRev = kTRUE, return only the latest revision of unique 
  //name && specAng &&startValid
  TObjArray *volArr;
  volArr = BrDbDetectorVolume::MultipleInstance(GetMultiple(BrDbDetectorVolume::kTableName,
                                               condition));
  if(!selectLatestRev) return volArr;

  //Coming here means user selected to only return latest revision.
  //Revision incremented if startValid and specAngle are the same.
  TIter nextVol(volArr);
  TObjArray *latestRevVol = new TObjArray();
  TIter nextRevVol(latestRevVol);
  BrDbDetectorVolume *vol,*latestRev;
  while((vol=(BrDbDetectorVolume*)nextVol())) {
     Int_t numFound = 0;
     while((latestRev=(BrDbDetectorVolume*)nextRevVol())) {
        if(vol->GetValidStart() == latestRev->GetValidStart() &&
           vol->GetSpectrometerAngle() == latestRev->GetSpectrometerAngle() &&
           !strcasecmp(vol->GetName(),latestRev->GetName())) {
           //Found one with identical start time and spec angle
           //Now check to see if highest revision already in
           numFound++;
           if(vol->GetRevisionId() > latestRev->GetRevisionId()) {
              //Remove current one and replace with later revision
              latestRevVol->Remove(latestRev);
              latestRevVol->Add(vol);
              }
           }
        }
     if(!numFound) latestRevVol->Add(vol);
     nextRevVol.Reset();
     }
  delete volArr;
  return latestRevVol;
}

//____________________________________________________________________
BrDbDetectorVolume* 
 BrRdbmGeometriesDb::GetDetectorVolume(const Char_t* condition) 
{
  // Find a detector volume with largest revisionId that matches 
  // condition <condition>

  BrDbDetectorVolume *vol = 0;

  TObjArray *arr = GetXDetectorVolume(condition);
  TIter next(arr);
  BrDbDetectorVolume *volTmp;
  while((volTmp = (BrDbDetectorVolume*)next()))  {
     if(!vol) 
       vol = volTmp;
     if(volTmp->GetRevisionId() > vol->GetRevisionId()) 
       vol = volTmp;
  }

  //Need to create new one copied from old one since old one will be deleted
  //when we delete arr.
  if(vol) 
    vol = new BrDbDetectorVolume(*vol);
  delete arr;

  return vol;
}

//____________________________________________________________________
BrDbDetectorVolume* 
 BrRdbmGeometriesDb::GetDetectorVolumeByName(const Char_t *name)
{
  // Find a parameter with name <name> belonging to detector
  // <detectorId> with largest revision #

  return GetDetectorVolume(Form("DetName = '%s'",name));
}

//____________________________________________________________________
 void BrRdbmGeometriesDb::AddDetectorVolume(BrDbDetectorVolume *vol) 
{
  // Add a detector volume to the database. Fails if names are not
  // properly set (throws a BrExcpetion)
  if (strlen(vol->GetName()) < 1) 
    throw new BrWarning("BrRdbmGeometriesDb::AddDetectorVolume",
			"Name for detector MUST be specified");

  if (strlen(vol->GetParentName()) < 1) {
     if(strcasecmp("CAVE",vol->GetName())) {
        throw new BrWarning("BrRdbmGeometriesDb::AddDetectorVolume",
		       "Parent name MUST be specified if vol is not CAVE");
        }
     }

  //Get the platform type so it can be set.
  BrDbVolumePlatformMap *map = 
    GetVolumePlatformMapByName(vol->GetName());
  if(!map) {
    throw new BrWarning("BrGeometrriesDb::AddDetectorVolume",
                        Form("Volume map for %s is not in DB so "
			     "platform type cannot be set",
			     vol->GetName()));
  }
  cout<< "Setting platform type of " << vol->GetName() 
      << " to " << map->GetPlatformType() << endl;
  vol->SetPlatformType(map->GetPlatformType());

  Int_t largestRev = 0;

  /*
  Not clear if this is necessary or not.  We are working with start time
  instead of run number, so revision much harder.
  */

  //Need to see if we have something in DB with following attributes.
  Int_t validStart = vol->GetValidStart();
  Int_t ang = vol->GetSpectrometerAngle();

  BrDbQuery* query = 
    BrDbQuery::Select(BrDbDetectorVolume::kTableName,0,
 	 Form("DetName='%s' AND validStart = %d "
	      "AND spectrometerAngle = %d",
	      vol->GetName(),validStart,ang));

  TSQLResult* res = Query(query);
  delete query;

  if (res->GetRowCount() > 0) {
     TObjArray* ar = BrDbDetectorVolume::MultipleInstance(res);
     TIter next(ar);
     BrDbDetectorVolume* v = 0;
     while((v = (BrDbDetectorVolume*)next())) {
       printf("Found volume with revision = %dn",v->GetRevisionId());
       if(v->GetRevisionId() > largestRev) 
	 largestRev = v->GetRevisionId();
        }
     delete v;
  }
  delete res;

  printf("Setting detector volume revision for %s to %dn",
	 vol->GetName(),largestRev+1);
  vol->SetRevisionId(largestRev + 1);

  //Now, lock the tables and update the DB, then unlock them
  LockTables(BrDbDetectorVolume::kTableName);
  vol->SetDBID(Increment());
  Query(vol->Insert());
  UnLockTables();
}

//____________________________________________________________________
TObjArray* 
 BrRdbmGeometriesDb::GetXMagnetVolume(const Char_t* condition) 
{
  // Find Runs that matches condition <condition>
  return 
    BrDbMagnetVolume::MultipleInstance(GetMultiple(BrDbMagnetVolume::kTableName,
                                               condition));
}

//____________________________________________________________________
BrDbMagnetVolume* 
 BrRdbmGeometriesDb::GetMagnetVolume(const Char_t* condition) 
{
  // Find a detector volume with largest revisionId that matches 
  // condition <condition>

  BrDbMagnetVolume *vol = 0;

  TObjArray *arr = GetXMagnetVolume(condition);
  TIter next(arr);
  BrDbMagnetVolume *volTmp;
  while((volTmp = (BrDbMagnetVolume*)next())) {
     if(!vol) vol = volTmp;
     if(volTmp->GetRevisionId() > vol->GetRevisionId()) vol = volTmp;
     }

  //Need to create new one copied from old one since old one will be deleted
  //when we delete arr.
  if(vol) vol = new BrDbMagnetVolume(*vol);
  delete arr;

  return vol;
}

//____________________________________________________________________
BrDbMagnetVolume* 
 BrRdbmGeometriesDb::GetMagnetVolumeByName(const Char_t *name)
{
  // Find a parameter with name <name> belonging to detector
  // <detectorId> with largest revision #

  return GetMagnetVolume(Form("MagnetName = '%s'",name));
}

//____________________________________________________________________
 void BrRdbmGeometriesDb::AddMagnetVolume(BrDbMagnetVolume *vol) 
{
  // Add a detector volume to the database. Fails if names are not
  // properly set (throws a BrExcpetion)

  if (strlen(vol->GetName()) < 1) 
    throw new BrWarning("BrRdbmGeometriesDb::AddMagnetVolume",
			"Name for magnet MUST be specified");

  Int_t largestRev = 0;

  /*
  Not clear if this is necessary or not.  We are working with start time
  instead of run number, so revision much harder.
  Set them all effectively = 1 for the moment.

  BrDbQuery* query = 
    BrDbQuery::Select(BrDbMagnetVolume::kTableName,0,
		      Form("MagnetName='%s'",vol->GetName()));

  TSQLResult* res = Query(query);
  delete query;

  if (res->GetRowCount() > 0) {
     TObjArray* ar = BrDbMagnetVolume::MultipleInstance(res);
     TIter next(ar);
     BrDbMagnetVolume* v = 0;
     while((v = (BrDbMagnetVolume*)next())) {
        //printf("Found volume with revision = %dn",v->GetRevisionId());
	if(v->GetRevisionId() > largestRev) largestRev = v->GetRevisionId();
        }
     delete v;
     }
  delete res;
  */


  //Need to see if we have something in DB with following attributes.
  Int_t validStart = vol->GetValidStart();

  BrDbQuery* query = 
    BrDbQuery::Select(BrDbMagnetVolume::kTableName,0,
      Form("MagnetName='%s' AND validStart = %d ",vol->GetName(),validStart));

  TSQLResult* res = Query(query);
  delete query;

  if (res->GetRowCount() > 0) {
     TObjArray* ar = BrDbMagnetVolume::MultipleInstance(res);
     TIter next(ar);
     BrDbMagnetVolume* m = 0;
     while((m = (BrDbMagnetVolume*)next())) {
       printf("Found magnet volume with revision = %dn",m->GetRevisionId());
       if(m->GetRevisionId() > largestRev) 
	 largestRev = m->GetRevisionId();
        }
     delete m;
  }
  delete res;

  //printf("Setting detector volume revision for %s to %dn",
  // vol->GetName(),largestRev+1);




  //printf("Setting detector volume revision for %s to %dn",vol->GetName(),largestRev+1);
  vol->SetRevisionId(largestRev + 1);

  //Now, lock the tables and update the DB, then unlock them
  LockTables(BrDbMagnetVolume::kTableName);
  vol->SetDBID(Increment());
  Query(vol->Insert());
  UnLockTables();
}


//____________________________________________________________________
 TObjArray* BrRdbmGeometriesDb::GetXVolumePlatformMap(const Char_t* condition) 
{
  // Find Runs that matches condition <condition>
  // All revisions that satisfy condition <condition> are returned.
  return 
    BrDbVolumePlatformMap::MultipleInstance(GetMultiple(BrDbVolumePlatformMap::kTableName,
							condition));
}

//____________________________________________________________________
 BrDbVolumePlatformMap* BrRdbmGeometriesDb::GetVolumePlatformMap(const Char_t* condition) 
{
  // Find a platform map that matches condition <condition>
  return 
    BrDbVolumePlatformMap::SingleInstance(GetSingle(BrDbVolumePlatformMap::kTableName,condition));
}

//____________________________________________________________________
BrDbVolumePlatformMap* 
 BrRdbmGeometriesDb::GetVolumePlatformMapByName(const Char_t *name)
{
  // Find a parameter with volume name <name> belonging to detector
  // <detectorId> 
  return GetVolumePlatformMap(Form("VolumeName = '%s'",name));
}

//____________________________________________________________________
 void BrRdbmGeometriesDb::AddVolumePlatformMap(BrDbVolumePlatformMap *map) 
{
  // Add a platform position to the database. Fails if it allready exists in
  // the database (throws a BrExcpetion)
  if (strlen(map->GetName()) < 1) 
    throw new BrWarning("BrRdbmGeometriesDb::AddVolumePlatformMap",
			"Volume MUST have a name");

  BrDbQuery* query = 
    BrDbQuery::Select(BrDbVolumePlatformMap::kTableName,0,
	              Form("VolumeName='%s'",map->GetName()));

  TSQLResult* res = Query(query);
  delete query;

  Int_t largestRev = 0;
  if (res->GetRowCount() > 0) {
    throw new BrWarning("BrRdbmGeometriesDb::AddVolumePlatformMap",
			"Volume MUST be unique");
     }
  delete res;

  //Now, lock the tables and update the DB, then unlock them
  LockTables(BrDbVolumePlatformMap::kTableName);
  map->SetDBID(Increment());
  Query(map->Insert());
  UnLockTables();
}

//____________________________________________________________________
 TObjArray* BrRdbmGeometriesDb::GetXPlatformPosition(const Char_t* condition) 
{
  // Find Runs that matches condition <condition>
  // All revisions that satisfy condition <condition> are returned.
  return BrDbPlatformPosition::MultipleInstance(GetMultiple(BrDbPlatformPosition::kTableName,
                                               condition));
}

//____________________________________________________________________
BrDbPlatformPosition* 
 BrRdbmGeometriesDb::GetPlatformPosition(const Char_t* condition) 
{
  // Find a platform position with largest revision that matches condition 
  // <condition>

  BrDbPlatformPosition *pos = 0;

  TObjArray *arr = GetXPlatformPosition(condition);
  TIter next(arr);
  BrDbPlatformPosition *posTmp;
  while((posTmp = (BrDbPlatformPosition*)next())) {
    if(!pos) 
      pos = posTmp;
    if(posTmp->GetRevisionId() > pos->GetRevisionId()) 
      pos = posTmp;
  }

  //Need to create new one copied from old one since old one will be deleted
  //when we delete arr.
  if(pos) 
    pos = new BrDbPlatformPosition(*pos);
  delete arr;
  return pos;

  //return BrDbPlatformPosition::SingleInstance(GetSingle(BrDbPlatformPosition::kTableName,
  //					   condition));
}

//____________________________________________________________________
BrDbPlatformPosition* 
 BrRdbmGeometriesDb::GetPlatformPosition(const Char_t *name,Int_t irun)
{
  // Find a parameter with name <name> belonging to detector
  // <detectorId> 
  return GetPlatformPosition(Form("RunNo = %d AND "
				  "PlatformName = '%s'",
				  irun,name));
}

//____________________________________________________________________
 void BrRdbmGeometriesDb::AddPlatformPosition(BrDbPlatformPosition *pos) 
{
  // Add a platform position to the database. Fails if it allready exists in
  // the database (throws a BrExcpetion)
  if (strlen(pos->GetName()) < 1) 
    throw new BrWarning("BrRdbmGeometriesDb::AddPlatformPosition",
			"Platform MUST have a name");

  BrDbQuery* query = 
    BrDbQuery::Select(BrDbPlatformPosition::kTableName,0,
	              Form("PlatformName='%s' AND RunNo = %d",
                            pos->GetName(),pos->GetRunNo()));

  TSQLResult* res = Query(query);
  delete query;

  Int_t largestRev = 0;
  if (res->GetRowCount() > 0) {
     TObjArray* ar = BrDbPlatformPosition::MultipleInstance(res);
     TIter next(ar);
     BrDbPlatformPosition* p = 0;
     while((p = (BrDbPlatformPosition*)next())) {
        //printf("Found platform position with revision = %dn",p->GetRevisionId());
	if(p->GetRevisionId() > largestRev) 
	  largestRev = p->GetRevisionId();
     }
     delete p;
  }
  delete res;
  
  //printf("Setting platform revision for %s in run %d to %dn",pos->GetName(),pos->GetRunNo(),largestRev+1);
  pos->SetRevisionId(largestRev + 1);

  //Now, lock the tables and update the DB, then unlock them
  LockTables(BrDbPlatformPosition::kTableName);
  pos->SetDBID(Increment());
  Query(pos->Insert());
  UnLockTables();
}



// $Log: BrRdbmGeometriesDb.cxx,v $
// Revision 1.3  2002/06/06 23:04:22  hagel
// Fix small bugs with magnet revisions
//
// Revision 1.2  2001/11/05 23:41:42  hagel
// Changes to MySQL mode for Geometry DB manager
//
// EOF 

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