|
// // $Id: BrDbDetectorVolume.cxx,v 1.9 2002/04/23 15:37:34 videbaek Exp $ // $Author: videbaek $ // $Date: 2002/04/23 15:37:34 $ // $Copyright: 2001 Brahms Collaboration // /////////////////////////////////////////////////////////////////////// // // BrDbDectectorVolume is a class that maps onto the BRAHMS DB // detector volume tables. // The values in this table are meant to be relative to the // platform that the detectors are sitting on. // // /////////////////////////////////////////////////////////////////////// #include <BrDbDetectorVolume.h> #include <BrRunsDb.h> #include <fstream.h> #ifndef BRAT_BrException #include "BrException.h" #endif #ifndef WIN32 #include <cstdlib> #include <iostream> #else #include <stdlib.h> #include <iostream.h> #endif ClassImp(BrDbDetectorVolume); //____________________________________________________________________ const Char_t* BrDbDetectorVolume::kTableName = "DetectorVolumes"; //____________________________________________________________________ BrDbDetectorVolume::BrDbDetectorVolume(void) { // Empty default constructor. } //____________________________________________________________________ BrDbDetectorVolume::BrDbDetectorVolume(const Char_t *name,const Char_t *parentName, Int_t specAngle, Int_t validstart, Int_t validstop, Int_t fromstart, Int_t fromend, Int_t date,Double_t posX, Double_t posY, Double_t posZ, Double_t theta,Double_t phi, Double_t psi, Double_t sizeX, Double_t sizeY, Double_t sizeZ,ERunTypes runType) { // Constructor for detectors; (set front and back edge = 0) SetName(name); SetParentName(parentName); fSpectrometerAngle = specAngle; fValidStart = validstart; fValidStop = validstop; fFromStart = fromstart; fFromEnd = fromend; fDate = date; SetPosition(posX,posY,posZ); SetAngles(theta,phi,psi); SetSize(sizeX,sizeY,sizeZ); fRunType = runType; fRevisionId = 0; fPlatformType = -1; //This will need to be set before being put into DB //printf("Inside BrDbDetectorVolume constructor; specAng = %d, sizeX = %12.8f, fSizeX = %12.8f, sizeY = %6.4f, sizeZ = %6.4fn",specAngle, sizeX, fSizeX, sizeY, sizeZ); } BrDbDetectorVolume::BrDbDetectorVolume(const Char_t *file){ //Constructor for reading from an ascii file //First initialize SetName(""); SetParentName(""); fSpectrometerAngle = 0; fValidStart = 0; fValidStop = 0; fFromStart = 0; fFromEnd = 0; fDate = 0; SetPosition(0,0,0); SetAngles(0,0,0); SetSize(0,0,0); fRunType = kTypeData; //Default fRevisionId = 0; fPlatformType = -1; //This will need to be set before being put into DB ReadAsciiFile(file); } //____________________________________________________________________ BrDbQuery* BrDbDetectorVolume::Create(void) { // Returns a SQL query string suitable for creation of this table in // a database. return BrDbQuery::Create(BrDbDetectorVolume::kTableName, "detName VARCHAR(64) NOT NULL, parentVolume VARCHAR(64) NOT NULL, platformType INT NOT NULL, spectrometerAngle INT NOT NULL, validStart INT NOT NULL, validStop INT NOT NULL, fromStart INT NOT NULL, fromStop INT NOT NULL, date INT NOT NULL, runType INT NOT NULL, posX DOUBLE(16,8) NOT NULL, posY DOUBLE(16,8) NOT NULL, posZ DOUBLE(16,8) NOT NULL, theta DOUBLE(16,8) NOT NULL, phi DOUBLE(16,8) NOT NULL, psi DOUBLE(16,8) NOT NULL, sizeX DOUBLE(16,8) NOT NULL, sizeY DOUBLE(16,8) NOT NULL, sizeZ DOUBLE(16,8) NOT NULL, revisionId INT NOT NULL"); } //____________________________________________________________________ BrDbDetectorVolume* BrDbDetectorVolume::SingleInstance(TSQLRow* row) { // Returns an instance of a BrDbDetectorVolume. User needs to store this // object immediately. Double_t posX,posY,posZ,theta,phi,psi,sizeX,sizeY,sizeZ; Int_t platformType; ERunTypes runType; sscanf(row->GetField(3), "%d",&platformType); sscanf(row->GetField(10),"%d",&runType); sscanf(row->GetField(11),"%lf",&posX); sscanf(row->GetField(12),"%lf",&posY); sscanf(row->GetField(13),"%lf",&posZ); sscanf(row->GetField(14),"%lf",&theta); sscanf(row->GetField(15),"%lf",&phi); sscanf(row->GetField(16),"%lf",&psi); sscanf(row->GetField(17),"%lf",&sizeX); sscanf(row->GetField(18),"%lf",&sizeY); sscanf(row->GetField(19),"%lf",&sizeZ); BrDbDetectorVolume* vol = new BrDbDetectorVolume( row->GetField(1), //detector name row->GetField(2), //parent vol name strtol(row->GetField(4),NULL,0), //10x spec angle strtol(row->GetField(5),NULL,0), //startvalid strtol(row->GetField(6),NULL,0), //stopvalid strtol(row->GetField(7),NULL,0), //fromstart strtol(row->GetField(8),NULL,0), //fromstop strtol(row->GetField(9),NULL,0), //creation date posX,posY,posZ, theta,phi,psi, sizeX,sizeY,sizeZ, runType); //runType vol->SetRevisionId(strtol(row->GetField(20),NULL,0)); //RevisionId vol->SetPlatformType(platformType); //platformType return vol; } //____________________________________________________________________ TObjArray* BrDbDetectorVolume::MultipleInstance(TSQLResult* res) { // Returns an (1D) array of BrDbDetectorVolume's matching Query that made // the TSQLResult. User need to store this immediately. Int_t count = (res) ? res->GetRowCount() : 0; TObjArray* table = new TObjArray(count); for (Int_t i = 0; i < count; i++) table->Add(BrDbDetectorVolume::SingleInstance(res->Next())); return table; } //____________________________________________________________________ BrDbQuery* BrDbDetectorVolume::Insert(void) { return BrDbQuery::Insert(BrDbDetectorVolume::kTableName, Form("%d, '%s', '%s', %d, %d, %d, %d, %d, %d, %d, %d, %12.8lf, %12.8lf, %12.8lf, %12.8lf, %12.8lf, %12.8lf, %12.8lf, %12.8lf, %12.8lf, %d", GetDBID(), fName,fParentName,fPlatformType,fSpectrometerAngle, fValidStart,fValidStop,fFromStart, fFromEnd, fDate, fRunType, fPosX,fPosY,fPosZ,fTheta,fPhi,fPsi, fSizeX,fSizeY,fSizeZ,fRevisionId)); } //____________________________________________________________________ void BrDbDetectorVolume::SetName(const Char_t *name) { //Set the detector name Int_t len = strlen(name); if(len > 63) len = 63; strncpy(fName,name,len); fName[len] = 0; } //____________________________________________________________________ void BrDbDetectorVolume::SetParentName(const Char_t *parent) { //Set the platform name that this detector sits on if(!parent) { fParentName[0] = 0; return; } Int_t len = strlen(parent); if(len > 63) len = 63; strncpy(fParentName,parent,len); fParentName[len] = 0; } //____________________________________________________________________ void BrDbDetectorVolume::SetSize(Double_t x, Double_t y, Double_t z) { //Set the size parameters fSizeX = x; fSizeY = y; fSizeZ = z; } //____________________________________________________________________ void BrDbDetectorVolume::SetPosition(Double_t x, Double_t y, Double_t z) { //Set the position parameters (relative to parent volume) fPosX = x; fPosY = y; fPosZ = z; } //____________________________________________________________________ void BrDbDetectorVolume::SetAngles(Double_t theta, Double_t phi, Double_t psi){ //Set euler angles (relative to parent volume) fTheta = theta; fPhi = phi; fPsi = psi; } void BrDbDetectorVolume::ReadAsciiFile(const Char_t *file){ //Adopt convention to read from GBRAHMS type ASCII file //We will read: position, orientation and size //This list must be identical to the one in WriteAsciiFile const Char_t *fields[] = { "Name:", "Parent:", "PosX:", "PosY:", "PosZ:", "Theta:", "Phi:", "Psi:", "SizeX:", "SizeY:", "SizeZ:", "ValidStartRun:", "EndOfList" }; ifstream inFile; inFile.open(file); if(inFile.fail()) { Error("ReadAsciiFile","Requested ASCII file: %s does not exist",file); return; } Int_t icount = 0; TString line; while(1) { line.ReadLine(inFile); if(inFile.eof()) { printf("Breaking after reading %d entriesn",icount); break; } if(!strcmp(fields[icount],"EndOfList")) { printf("Too many entries in this filen"); break; } if(!line.BeginsWith("#")) { //Process only if not a comment if(line.BeginsWith(fields[icount],TString::kIgnoreCase)) { Ssiz_t indx = line.Index(":",0,TString::kIgnoreCase); TString field(line(0,indx+1)); TString val(line(indx+1,line.Length()-indx)); TString value(val.Strip(TString::kBoth)); //strip blanks off if(field.Contains("Name:",TString::kIgnoreCase)) { strcpy(fName,value.Data()); } else if(field.Contains("Parent:",TString::kIgnoreCase)) { strcpy(fParentName,value.Data()); } else if(field.Contains("PosX:",TString::kIgnoreCase)) { fPosX = atof(value.Data()); } else if(field.Contains("PosY:",TString::kIgnoreCase)) { fPosY = atof(value.Data()); } else if(field.Contains("PosZ:",TString::kIgnoreCase)) { fPosZ = atof(value.Data()); } else if(field.Contains("Theta:",TString::kIgnoreCase)) { fTheta = atof(value.Data()); } else if(field.Contains("Phi:",TString::kIgnoreCase)) { fPhi = atof(value.Data()); } else if(field.Contains("Psi:",TString::kIgnoreCase)) { fPsi = atof(value.Data()); } else if(field.Contains("SizeX:",TString::kIgnoreCase)) { fSizeX = atof(value.Data()); } else if(field.Contains("SizeY:",TString::kIgnoreCase)) { fSizeY = atof(value.Data()); } else if(field.Contains("SizeZ:",TString::kIgnoreCase)) { fSizeZ = atof(value.Data()); } else if(field.Contains("ValidStartRun:",TString::kIgnoreCase)) { Int_t validStartRun = atoi(value.Data()); BrRunsDb *runDb = BrRunsDb::Instance(); if(!runDb->Connect()) { Error("ReadAsciiFile","Can't reconnect to run db"); fValidStart = 0; } else { BrDbRun *run = runDb->GetRun(validStartRun); fValidStart = run->GetStartTime(); } } else { printf("Unknown field in file: %sn",field.Data()); } } else printf("Found line %s, it should be %sn",line.Data(),fields[icount]); icount++; } } //printf("Closing the filen"); inFile.close(); } void BrDbDetectorVolume::WriteAsciiFile(const Char_t *file){ //Adopt convention to write ASCII file //We will write: position, orientation and size ofstream outFile; outFile.open(file,ios::out); if(outFile.fail()) { Error("WriteAsciiFile","Requested ASCII file: %s cannot be opened for writing",file); return; } Int_t icount = 0; TString line; line = "Name: "; line.Append(fName); outFile<<line<<endl; line = "Parent: "; line.Append(fParentName); outFile<<line<<endl; TString lineFormat = "%12.8lf"; line = "PosX: "; line.Append(Form(lineFormat.Data(),fPosX)); outFile<<line<<endl; line = "PosY: "; line.Append(Form(lineFormat.Data(),fPosY)); outFile<<line<<endl; line = "PosZ: "; line.Append(Form(lineFormat.Data(),fPosZ)); outFile<<line<<endl; line = "Theta: "; line.Append(Form(lineFormat.Data(),fTheta)); outFile<<line<<endl; line = "Phi: "; line.Append(Form(lineFormat.Data(),fPhi)); outFile<<line<<endl; line = "Psi: "; line.Append(Form(lineFormat.Data(),fPsi)); outFile<<line<<endl; line = "SizeX: "; line.Append(Form(lineFormat.Data(),fSizeX)); outFile<<line<<endl; line = "SizeY: "; line.Append(Form(lineFormat.Data(),fSizeY)); outFile<<line<<endl; line = "SizeZ: "; line.Append(Form(lineFormat.Data(),fSizeZ)); outFile<<line<<endl; //Go through this rigamarole here so that user can input run number //in ascii file rather than a dumb unix time which if off by even //a second has the potential to foul everything up. line = "ValidStartRun: "; BrRunsDb *runDb = BrRunsDb::Instance(); if(!runDb->Connect()) { Error("ReadAsciiFile","Can't reconnect to run db"); line.Append("0"); } else { BrDbRun *run = runDb->GetRun(Form("StartTime = %d",fValidStart)); if(run) line.Append(Form("%d",run->GetRunNo())); else { Warning("WriteAsciiFile","No run found for start time %d",fValidStart); line.Append("0"); } } outFile<<line<<endl; outFile.close(); } // // $Log: BrDbDetectorVolume.cxx,v $ // Revision 1.9 2002/04/23 15:37:34 videbaek // Remove ios::in form ifstream // // Revision 1.8 2002/03/28 20:48:46 hagel // Fix kTypeRun typo // // Revision 1.7 2002/03/28 18:08:42 hagel // Implemented ASCII read and write for BrDbDetectorVolume // // Revision 1.6 2002/02/08 23:48:14 hagel // Remove extra print statement // // Revision 1.5 2002/02/08 22:55:48 hagel // Take advantage of new features of BrRotMatrix // // Revision 1.4 2001/08/14 14:45:36 hagel // Yet another iteration on GeometryDB // // 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.4 2001/06/05 18:40:31 cholm // Removed BrDbInc.h an all references to it // // Revision 1.3 2001/05/07 21:22:01 hagel // Next iteration of Geometry DB // // Revision 1.2 2001/04/20 16:13:45 hagel // Rework MySQL mode of BrMagnetVolume // // Revision 1.1 2001/03/07 16:54:02 hagel // Initial revision, probably needs more iterations // // |
||||||
This page automatically generated by script docBrat by Christian Holm |
Copyright ; 2002 BRAHMS Collaboration
<brahmlib@rcf.rhic.bnl.gov>
|