|
//////////////////////////////////////////////////////////// // // BrCalibration is a BRAHMS data class providing storage and // access function for Calibration constants. It is an essential // baseclass for concrete detectorclasses. This class calls the // BrDb.. MySql access methods, and uses structures from there. // // The class utilizes a utility class // BrParameterDbData that should not be used anywhere else. // // //////////////////////////////////////////////////////////// // // $Id: BrCalibration.cxx,v 1.10 2002/06/23 17:06:44 videbaek Exp $ // $Author: videbaek $ // $Date: 2002/06/23 17:06:44 $ // $Copyright: 2000 Brahms Collaboration // #include <iostream.h> #include <iomanip.h> #include <ctime> #include <BrCalibration.h> #include <TObjArray.h> #include <BrException.h> //_________________________________________________________________________ ClassImp(BrCalibration); //_________________________________________________________________________ BrCalibration::BrCalibration() { // Constructor. Set counter and list data members to zero. // Don't use this constructor unless you have to and know // what you are doing // Use BrCalibration(Char_t, Char_t ) instead fDebugLevel = 0; fVerboseLevel = 0; } //_________________________________________________________________________ BrCalibration::BrCalibration(Char_t *name, Char_t *title) : TNamed(name, title) { // Standard constructor. // the name should be that of the associated detector e.g. "H1" // This will be called from the ParameterManager - so the Database // etc is defined at this point. // The fCalibDb is set to the default in the constructor. It can be // overwritten by the SetDatabase Method. fDebugLevel = 0; fVerboseLevel = 0; try{ fMainDb = BrMainDb::Instance(); fCalibDb = BrCalibrationsDb::Instance(); fDetector = fMainDb->GetDetectorByName(GetName()); fDetectorId = fDetector->GetDBID(); fInitialized = kFALSE; } catch(BrException* e){ cout << "BrCalibration( "<< name << " ) " << *e << endl; e->Execute(); } } //_________________________________________________________________________ BrCalibration::~BrCalibration() { // Default destructor // fParameterDataList.Delete(); } //_____________________________________________________________________ void BrCalibration::AddParameterData(Char_t* parametername, BrCalibrationData* data) { // // Add the parameterdata structure to objectlist AND create the // BrDbParameter object. // fParameterDataList.Add(data); strcpy(data->fParameterName, parametername); } //__________________________________________________________________________ void BrCalibration::Commit(Int_t start, Int_t stop) { // // Check validity of data and update if need be by call to DB engine // if (DebugLevel() > 10) cout << "Commit ParameterElement : Detector :" << GetName() << endl; // If a connection isn't established, then try to do so. if (!fCalibDb->IsConnected()) fCalibDb->Connect(); if (!fCalibDb->IsConnected()) return; TIter next(&fParameterDataList); BrCalibrationData* data = 0; while ((data = (BrCalibrationData*)next())) { if(data->fAccessMode == 2) data->Commit(fCalibDb, start, stop); } } //__________________________________________________________________________ void BrCalibration::Close(Option_t* option) { // Close connection to the database, if it exists, so that we free // this slot on the server. Since we can have multiple DBs, one for // each BrCalibration object, we need to run through them all. This // is done by the BrCalibrationManager, so here we only need to make // sure if we're connected or not. #ifdef BR_USE_DISCONNECT if (!fCalibDb) return; if (fCalibDb->IsConnected()) fCalibDb->Close(); #endif } //__________________________________________________________________________ void BrCalibration::Init() { // // Init will be called be BrDbParameterManager Init() method. // Create the Parameter description Object for all objects. // Usualy called from the BrCalibrationManager::Init() method. // The Databse MUST have been set at this point, since the parameter // information is extracted from the DB here. // TIter next(&fParameterDataList); BrCalibrationData* data = 0; while ((data = (BrCalibrationData*)next())) data->fParameter = (fCalibDb->GetParameter(data->fParameterName, fDetectorId)); fInitialized = kTRUE; } //_____________________________________________________________________________ Int_t BrCalibration::GetAccessMode(const Char_t* param) const{ // // get the access mode in general term by looking up the // parameter element defined by name. // TString s(param); TIter next(&fParameterDataList); BrCalibrationData* data = 0; // Loop over parameters in this element while ((data = (BrCalibrationData*)next())) { // Check if we found the parameter if ( s != data->fParameterName) continue; return data->fAccessMode; } return 0; } //____________________________________________________________________ const Char_t *BrCalibration::FormatDate(const Int_t time) const { // Return formatted start date-time // Input is the fDatim from the TDatime // This is done in later versions of root using // TDatime::AsSQLString() but that is not yet in root that is being // used at the time of this writing. So we do it by hand for // compatibility. // Copied from BrRunInfo. static Char_t formattedDate[32]; time_t itime = time; struct tm *tp = localtime(&itime); strftime(formattedDate,32,"%d-%b-%Y %H:%M:%S",tp); return formattedDate; } //____________________________________________________________________ const Char_t* BrCalibration::GetRevisionValidStart(const Char_t* param) const { // // get the unix time for the Revion in use. // parameter element defined by name. // return (this ? FormatDate(this->GetRevisionValidStartUnix(param)) : "-1"); } //____________________________________________________________________ const Char_t* BrCalibration::GetRevisionValidEnd(const Char_t* param) const { // // get the Revison Time in human readable form // parameter element defined by name. // return (this ? FormatDate(this->GetRevisionValidEndUnix(param)) : "-1"); } //____________________________________________________________________ const Char_t* BrCalibration::GetRevisionDate(const Char_t* param) const { // // get the Insertion Date/Time in human readable form // parameter element defined by name. // return (this ? FormatDate(this->GetRevisionDateUnix(param)) : "-1"); } //____________________________________________________________________ Int_t BrCalibration::GetRevisionValidStartUnix(const Char_t* param) const { // // get the unix time for the Revion in use. // parameter element defined by name. // TString s(param); TIter next(&fParameterDataList); BrCalibrationData* data = 0; while ((data = (BrCalibrationData*)next())) { if ( s != data->fParameterName) continue; if(data->fRevision) return data->fRevision->GetValidStart(); else return 0; } return 0; } //____________________________________________________________________ Int_t BrCalibration::GetRevisionDateUnix(const Char_t* param) const { // // get the unix time for the Revion in use. // parameter element defined by name. // TString s(param); TIter next(&fParameterDataList); BrCalibrationData* data = 0; while ((data = (BrCalibrationData*)next())) { if ( s != data->fParameterName) continue; if(data->fRevision) return data->fRevision->GetDate(); else return 0; } return 0; } //____________________________________________________________________ Int_t BrCalibration::GetRevisionValidEndUnix(const Char_t* param) const { // // get the unix time for the Revion in use. // parameter element defined by name. // TString s(param); TIter next(&fParameterDataList); BrCalibrationData* data = 0; while ((data = (BrCalibrationData*)next())) { if ( s != data->fParameterName) continue; if(data->fRevision) return data->fRevision->GetValidEnd(); else return 0; } return 0; } //____________________________________________________________________ Bool_t BrCalibration::RevisionExists(const Char_t* param) const { // Check if there's a revision for parameter param. If param is // "*", then check all parameters that has been flaged for use. TString s(param); TIter next(&fParameterDataList); BrCalibrationData* data = 0; // Assume success Bool_t retVal = kTRUE; // Loop over parameters in this element while ((data = (BrCalibrationData*)next())) { // Check if we found the parameter if (s != "*" && s != data->fParameterName) continue; // Check if there's a revision and we can access it if (data->fAccessMode && !data->fRevision) { // No need to do furhter tests. retVal = kFALSE; break; } } return retVal; } //____________________________________________________________________ void BrCalibration::SetComment(const Char_t * parametername, const Char_t * text) { // // Set the revision comment if the table is set writeable // TIter next(&fParameterDataList); BrCalibrationData* data = 0; while ((data = (BrCalibrationData*)next())) { if(strcmp(parametername,data->fParameterName)) continue; if((data->fAccessMode == kWrite) || (data->fAccessMode == kTransitional)) data->fRevision->SetComment(text); else Warning("SetComment", "Table '%s' not writeable", parametername); return; } // Only get here in case of missing parameter name, or from so // stupid place in the code which shouldn't happen! Warning("SetComment", "Table '%s' not found", parametername); } //____________________________________________________________________ void BrCalibration::SetDatabase(BrCalibrationsDb* calib) { // // This MUST be done before Init() // if(fInitialized) throw new BrWarning("SetDatabase","Not allowed after Init()"); fCalibDb = calib; } //____________________________________________________________________ void BrCalibration::Update(Int_t start, Int_t stop) { // // Check validity of data and update if need be by call to DB engine // In case of access mode==3 (Load) reply an module to load these. if (Verbose() > 5) cout << "Update calibration for detector : " << GetName() << endl; // If a connection isn't established, then try to do so. if (!fCalibDb->IsConnected()) fCalibDb->Connect(); if (!fCalibDb->IsConnected()) return; TIter next(&fParameterDataList); BrCalibrationData* data = 0; while ((data = (BrCalibrationData*)next())) { if(data->fAccessMode == kRead) data->Update(fCalibDb, start, stop); if(data->fAccessMode == kWrite) data->Create(fCalibDb, start, stop); if(data->fAccessMode == kTransitional) data->Create(fCalibDb, start, stop); } } //____________________________________________________________________ void BrCalibration::Use(const TString& parName, BrCalibration::EAccessMode mode=kRead, UInt_t nElements=0){ // // Mark the access mode for the parameter. // By default a given class will not be updated unless // being marked by this method in on of the user modules. // Bool_t found = kFALSE; TIter next(&fParameterDataList); BrCalibrationData* data = 0; while ((data = (BrCalibrationData*)next())) { if(strcmp(parName, data->fParameterName)) continue; found = kTRUE; switch (mode) { case kRead: if(data->fAccessMode == kWrite) throw new BrError("BrCalibration::Use", "Cannot switch from write to read"); if(data->fAccessMode == kTransitional){ if(DebugLevel()) cout << "Use transitional Data for " << parName << endl; } else { data->fAccessMode = kRead; if (DebugLevel() > 10) { cout << " Use(" <<parName.Data() << ") set "; cout << data->fParameterName<<endl; } } break; case kWrite: if(data->fAccessMode == kRead) throw new BrError("BrCalibration::Use", "Cannot switch from read to write "); if(data->fAccessMode == kTransitional) throw new BrError("BrCalibration::Use", "Cannot switch from Load to write "); data->fAccessMode = kWrite; data->fDataSize = nElements; if (DebugLevel() > 10) cout << " Write(" <<parName.Data() << ") set " << data->fParameterName<<endl; break; case kTransitional: if(data->fAccessMode == 1) throw new BrError("BrCalibration::Use", "Cannot switch from read to Load "); if(data->fAccessMode == 2) throw new BrError("BrCalibration::Use", "Cannot switch from write to Load "); data->fAccessMode = kTransitional; data->fDataSize = nElements; if (DebugLevel() > 10) cout << " Transitional(" <<parName << ") set " << data->fParameterName<<endl; break; default: Warning("Use","Should not come here: %s mode %d", parName.Data(), mode); } if (DebugLevel() > 4) data->Print("BPR"); } if(!found) Warning("Use","Parameter %s not found",parName.Data()); } //____________________________________________________________________ void BrCalibration::Print(Option_t* option) const { // Options: // B Basic information // D Detector information // L Recursively print the contained BrCalibrationData // // Other options are passed onto contained BrCalibrationData // objects. See BrCalibrationData::Print for a description of // avaliable options. TString opt(option); opt.ToLower(); // Basic information if (opt.Contains("b")) cout << "BrCalibration: " << GetName() << " - " << GetTitle() << endl; // Detector information if (opt.Contains("d")) { if (fDetector) cout << " Detector: " << fDetector->GetName() << endl << " Detector DB ID: " << fDetector->GetDBID() << endl; else cout << " No detector information" << endl; } if(!opt.Contains("l")) return; TIter next(&fParameterDataList); BrCalibrationData* data = 0; // Loop over parameters in this element while ((data = (BrCalibrationData*)next())) data->Print(option); } //____________________________________________________________________ Bool_t BrCalibration::StillValid(Int_t start, Int_t stop, Int_t ValidStart, Int_t ValidEnd) { // This routine seems to be lacking some // reasonable content. // return kFALSE; } ////////////////////////////////////////////////////////////////////////// // // BrParameterDbData // // A local structure /class that keeps all the necessary information // to interact with the Db classes. // ///////////////////////////////////////////////////////////////////////// //_______________________________________________________________________ ClassImp(BrCalibrationData); //_______________________________________________________________________ BrCalibrationData::BrCalibrationData() { fParameter = 0; fRevision = 0; fRevisionType = 0; fAccessMode = 0; fDataSize = 0; strcpy(fParameterName,""); } //_______________________________________________________________________ BrCalibrationData::~BrCalibrationData() { if(fParameter) delete fParameter; if(fRevision) delete fRevision; if(fRevisionType) delete fRevisionType; } //_______________________________________________________________________ void BrCalibrationData::Update(BrCalibrationsDb* calibDb, Int_t start, Int_t stop, Int_t revisionID) { // // Update if need be be request to DB. // if(fAccessMode == 1){ if(fRevision) { Int_t ValidStart = fRevision->GetValidStart(); Int_t ValidEnd = fRevision->GetValidEnd(); if(BrCalibration::StillValid(start, stop, ValidStart, ValidEnd)) return; delete fRevision; fRevision = 0; } fRevision = calibDb->GetRevision(fParameter->GetDBID(), start, stop, 0, fParameter->GetPolicy()); } } //________________________________________________________________________ void BrCalibrationData::Create(BrCalibrationsDb* calibDb, Int_t start, Int_t stop, Int_t revisionID) { // Create an instance of the revision for this parameter. // Called from Update ensure created before filling. // Fill as much information as possible into a newly created // BrDbRevision // if(!fRevisionType) fRevisionType = calibDb->GetRevisionType("standard",""); if(fRevision){ throw new BrWarning("BrCalibrationData:::Create", "Revision already exists : %s", fParameter->GetName()); } fRevision = new BrDbRevision(fParameter->GetDBID(), 0, 0, 0, 0); fRevision->SetComment("No comment"); fRevision->SetEntries(fDataSize); void* DataArray = 0; if(!strcmp("Float_t", fParameter->GetTypeName())){ fRevision->SetBytes(sizeof(Float_t)*fDataSize); fRevision->SetDataType(BrDbRevision::kFloat); DataArray = new Float_t [fDataSize]; } if(!strcmp("Double_t", fParameter->GetTypeName())){ fRevision->SetBytes(sizeof(Double_t)*fDataSize); fRevision->SetDataType(BrDbRevision::kDouble); DataArray = new Double_t [fDataSize]; } if(!strcmp("Int_t", fParameter->GetTypeName())){ fRevision->SetBytes(sizeof(Int_t)*fDataSize); fRevision->SetDataType(BrDbRevision::kInt); DataArray = new Int_t [fDataSize]; } if(!strcmp("Short_t", fParameter->GetTypeName())){ fRevision->SetBytes(sizeof(Short_t)*fDataSize); fRevision->SetDataType(BrDbRevision::kShort); DataArray = new Short_t [fDataSize]; } if(!strcmp("Long_t", fParameter->GetTypeName())){ fRevision->SetBytes(sizeof(Long_t)*fDataSize); fRevision->SetDataType(BrDbRevision::kLong); DataArray = new Long_t [fDataSize]; } if(!DataArray){ throw new BrError("BrCalibrationData:::Create", "Illegal Datatype %s", fParameter->GetTypeName() ); } fRevision->SetArray(DataArray); } //_______________________________________________________________________ void BrCalibrationData::Commit(BrCalibrationsDb* calibDb, Int_t start, Int_t end) { // // Update if need be be request to DB. // The revision should exists. // Fill the valid start and valid stop time. // The otehr parameters are set at Init() if(fAccessMode == 2){ if(fRevision) { fRevision->SetValidStart(start); fRevision->SetValidEnd(end); calibDb->AddRevision(fRevision); } } } //_______________________________________________________________________ void BrCalibrationData::Print(Option_t* option) const { // Options: // // B Show basic information (default) // P Show parameter information // T Show revision type information // R Show revision information // D Show data from revision (needs R) // TString opt(option); opt.ToLower(); // Basic information if (opt.Contains("b")) { cout << " Parameter: " << fParameterName << endl << " Data size: " << fDataSize << endl << " Access mode: " << flush; switch(fAccessMode) { case 0: cout << "init" << endl; break; case 1: cout << "read" << endl; break; case 2: cout << "write" << endl; break; case 3: cout << "load" << endl; break; default: cout << "unknown" << endl; break; } } // Information from the BrDbParameter object. if (opt.Contains("p")) { cout << " Parameter " << flush; if (fParameter) cout << "ID: " << fParameter->GetDBID() << endl << " Detector: " << fParameter->GetReferenceID() << endl << " Type: " << fParameter->GetTypeName() << endl << " Policy: " << fParameter->GetPolicy() << endl << " Comment: " << fParameter->GetComment() << endl; else cout << ": not set" << endl; } // Information from the BrDbRevisionType object. if (opt.Contains("t")) { cout << " Revision type: " << flush; if (fRevisionType) cout << fRevisionType->GetName() << endl << " Comment: " << fRevisionType->GetComment() << endl; else cout << "not set" << endl; } // Information from the BrDbRevision object if (opt.Contains("r")) { cout << " Revision " << flush; if (!fRevision) { cout << ": not set" << endl; return; } cout << "DB ID: " << fRevision->GetDBID() << endl << " Validity: " << setw(10) << fRevision->GetValidStart() << " - " << setw(10) << fRevision->GetValidEnd() << endl << " From data: " << setw(10) << fRevision->GetFromStart() << " - " << setw(10) << fRevision->GetFromEnd() << endl << " Created on: " << fRevision->GetDate()<< endl << " Comment: " << fRevision->GetComment() << endl; // Show the data array if (opt.Contains("d")) { cout << " Data array: " << flush; cout << " Datatype " << fRevision->GetDataType() << endl; cout << " Entries " << fRevision->GetEntries() << endl; if (!fParameter || !fRevision->GetArray()) { cout << "not set" << endl; return; } TString parType(fParameter->GetTypeName()); Int_t i = 0, n = fRevision->GetEntries(); for (i = 0; i < n; i++) { if (i % 8 == 0) cout << endl; if (parType == "Float_t") cout << setw(8) << ((Float_t*)fRevision->GetArray())[i] << " " << flush; else if (parType == "Double_t") cout << setw(8) << ((Double_t*)fRevision->GetArray())[i] << " " << flush; else if (parType == "Int_t") cout << setw(8) << ((Int_t*)fRevision->GetArray())[i] << " " << flush; else if (parType == "Short_t") cout << setw(8) << ((Short_t*)fRevision->GetArray())[i] << " " << flush; } cout << endl; } // option d } // option r } //////////////////////////////////////////////////////////////////////////// // // $Log: BrCalibration.cxx,v $ // Revision 1.10 2002/06/23 17:06:44 videbaek // Add methods to get the RevisionDate out from a calibration with a public method. // See eg.g use in TodDbBrowser in do_app/db // // Revision 1.9 2002/06/13 17:16:24 videbaek // small fix of print layout // // Revision 1.8 2002/06/03 18:19:48 pchristi // Initialised verbosity level to 0. // // Revision 1.7 2002/03/21 23:41:30 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.6 2002/03/01 14:31:37 cholm // Fixed FVs const problem // // Revision 1.5 2002/02/26 21:35:27 videbaek // Added method to get the time i.e. formatted of a revison back. // This is used in the DbBrowser utility programs. // There is a const warning (which I did not figure out yet) // // Revision 1.4 2002/02/14 21:47:55 videbaek // Add methods to GetValid Revision date which can be used to indicate what // revision ws actullay used, not just that a version was found. // // Revision 1.3 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.2 2001/11/26 18:37:40 videbaek // Updated code for revised Revision tables. Added some base method to BrCalibration // that can be used for the accessCheck. // // Revision 1.1 2001/10/08 10:40:11 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.7 2001/09/23 01:49:58 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.6 2001/09/12 15:08:21 cholm // Removed method ListParameters in favour of Print. Also added "*" as // valid arg to RevisionExist - very usefull I think. Other minor fixes. // // Revision 1.5 2001/07/30 12:44:05 cholm // Protected some output behind DebugLevel() and changed some cerr, to // Warning. // // Revision 1.4 2001/07/30 12:31:36 cholm // Added the methods BrCalibration::Print and // BrCalibrationData::Print. // // Revision 1.3 2001/07/27 21:24:25 videbaek // Modified two serious errors in basic Dbaccess routine // BrDbParameter. The policy was taken from the wrong field. // BrCalibrations Db. entried was not updated if no precise match was // found. // No changes in BrCalibration // // Revision 1.2 2001/07/01 12:00:29 cholm // Added method Bool_t RevisionExists(const Char_t* param) const to check // if a given parameter has a revision. The idea was taken from Djamels // BrTofCalbiration, but was moved here, since it's generally useful and // can be generalised and optimied better in this class. // // Revision 1.1.1.1 2001/06/21 14:55:17 hagel // Initial revision of brat2 // // Revision 1.14 2001/06/18 10:32:02 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.13 2001/06/05 18:42:06 cholm // Removed BrDbInc.h an all references to it // // Revision 1.12 2001/01/26 12:35:27 cholm // Set fRevision to zero when deleting in BrCalibrationData::Update // // Revision 1.11 2000/12/08 19:41:13 videbaek // Added data types Int_t, Double_t and Long_t to list. // // Revision 1.10 2000/09/09 18:43:21 videbaek // Make the debug writting dependent on compile time flag. The method // will now execute silently. // // Revision 1.9 2000/07/20 18:14:04 videbaek // Added SetComment(char*, char*) to parameterElement class // metic changed for BrDb.cxx // // Revision 1.8 2000/06/08 10:48:14 cholm // BrCalibration objects can be written to disk (ROOT file) // BrCalibration object can be written to disk (ROOT File). // // Revision 1.7 2000/06/03 22:20:02 videbaek // // // Revision 1.6 2000/06/03 18:12:54 videbaek // Many update to the ParamemertElement and related classes for the calibration access. // // Revision 1.5 2000/05/26 17:48:49 videbaek // Fix access to fParamaterName in subclass update and create // // Revision 1.4 2000/05/25 16:31:04 videbaek // store latest updatex to ParameterElement classes // // Revision 1.3 2000/05/19 17:37:45 cholm // BrDb now checks the host name for the connection. If the host name is the same as the current host, 'localhost' is substituted for that name (needed for MySQL C API apparently). BrCalibrations::GetRevision(int, int, int, int, int), now has the possiblility to take the BrParameter::fPolicy into account. If BrCalibrations::GetRevision(int, int, int, int, int) finds more then one revision, of the appropiate type and validity, the most recent is returned // // Revision 1.2 2000/05/17 10:43:15 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>
|