/// Class to read magnet current over a time range /// and provide the average / RMS values. /// Blair Jamieson (C) 2010 #include #include #include "IMagnetCurrent.hxx" ///-------------------------------------------------------------------- /// build new Magnet Current reading with intended time range of use specified IMagnetCurrent::IMagnetCurrent( long autimemin, long autimemax ){ fTime = autimemin; fStartTime = autimemin; fEndTime = autimemax; fTimeStamp = 0x0; Init(); } ///-------------------------------------------------------------------- /// build new Magnet Current reading with intended time range of use specified IMagnetCurrent::IMagnetCurrent( int ayearmin, int amonthmin, int adaymin, int ahourmin, int aminmin, int asecmin, int ayearmax, int amonthmax, int adaymax, int ahourmax, int aminmax, int asecmax ){ fTimeStamp=0x0; SetTimeNoRead(ayearmax, amonthmax, adaymax, ahourmax, aminmax, asecmax); fEndTime = fTime; SetTimeNoRead(ayearmin, amonthmin, adaymin, ahourmin, aminmin, asecmin); fStartTime = fTime; Init(); } /// Initialize magnet current object void IMagnetCurrent::Init(){ char * aCtime;//[256]; aCtime = ctime( (const time_t*)&fStartTime ); int aslen = strlen(aCtime); //if (fTimeStampMin!=0x0) delete [] fTimeStampMin; fTimeStampMin = new char[aslen+1]; sprintf(fTimeStampMin,"%s",aCtime); fTimeStampMin[aslen-1] = 0x0; aCtime = ctime( (const time_t*)&fEndTime ); aslen = strlen(aCtime); //if (fTimeStampMax!=0x0) delete [] fTimeStampMax; fTimeStampMax = new char[aslen+1]; sprintf(fTimeStampMax,"%s",aCtime); fTimeStampMax[aslen-1] = 0x0; // set up the field names SetupFieldNames(); fI_vec.clear(); // Now read magnet currents properties Read(); // setup mean, rms GetTimeInterval(fStart,fEnd); int nquery = 0; do { if (fI >= -0.1){ // filter bad readings fIAvg.Add( fTime, fI ); } else { COMETLog("Bad reading at time "<2 ){ int adt = int( fI_vec[1].first - fI_vec[0].first); if ( adt > 0 ) nread = (fEndTime - astart)/adt; else nread = (fEndTime - astart)/300; // assume every 5 minutes? if ( nread > 20000 ) nread = 20000; if ( nread < 100 ) nread = 100; //std::cout<<"adt="< fTime && fI_names.size() != 0){ // This access method is guaranteed to return a table row before fTime, as long as // such a row actually exists. double backup_current = ISlowControlDatabase::Get().QueryShowField(fTime,"magnet_curr_65",fI_names[0].c_str()); double backup_time = ISlowControlDatabase::Get().QueryShowField(fTime,"magnet_curr_65","_i_time"); COMETLog("IMagnetCurrent::ReadSlowDB_I : Found some unexpected gaps in magnet current GSC table.\n" << "Using backup table row: " << backup_current << " "<< backup_time); fI_vec[0].first = backup_time; fI_vec[0].second[0] = backup_current; } return; } ///------------------------------------------------------------------------ /// Read another chunk of data from offline database bool IMagnetCurrent::ReadOfflineDB_I(){ COMET::ICOMETEvent *event = COMET::IEventFolder::GetCurrentEvent(); COMET::ICOMETContext context = event->GetContext(); context.SetPartition(0); COMET::IResultSetHandle rs(context); Int_t numRows(rs.GetNumRows()); if(numRows == 0) { COMETLog("Not able to read from offline db"); return false; } COMETLog("Applying query at " << UnixTimeToDateTime(context.GetTimeStamp()) << " ... result IMagnetCurrent_DB_Table set contains " << numRows << " rows"); fI_vec.clear(); for (Int_t irow = 0; irow current_fields; std::pair > current_data; current_data.first = rs.GetRow(irow)->GetTime(); current_fields.push_back(rs.GetRow(irow)->GetCurrent()); current_data.second = current_fields; fI_vec.push_back(current_data); } return true; } ///-------------------------------------------------------------------- /// Print out the values for the current time void IMagnetCurrent::Print(){ std::cout<<" Record for "< fI_idx+2 ){ start_time = (long) fI_vec[ fI_idx ].first; end_time = (long) fI_vec[ fI_idx+1 ].first; } return; } ///-------------------------------------------------------------------- /// Get unix time long IMagnetCurrent::GetUnixTime( int ayear, int amonth, int aday, int ahour, int amin, int asec){ tm mt; // use time structure from time.h // to build unix time from year,month, etc. mt.tm_year = ayear - 1900; mt.tm_mon = amonth-1; mt.tm_mday = aday; mt.tm_hour = ahour; mt.tm_min = amin; mt.tm_sec = asec; mt.tm_isdst = -1; return (long)mktime(&mt); } ///-------------------------------------------------------------------- /// Set the time for this object void IMagnetCurrent::SetTimeNoRead( int ayear, int amonth, int aday, int ahour, int amin, int asec){ fTime = (int)GetUnixTime(ayear, amonth, aday, ahour, amin, asec ); } ///-------------------------------------------------------------------- /// Set timestamp from unix integer timestamp void IMagnetCurrent::SetupTimeStamp(){ char * aCtime;//[256]; aCtime = ctime( (const time_t*)&fTime ); int aslen = strlen(aCtime); if (fTimeStamp!=0x0) delete [] fTimeStamp; fTimeStamp = new char[aslen+1]; sprintf(fTimeStamp,"%s",aCtime); fTimeStamp[aslen-1] = 0x0; } ///-------------------------------------------------------------------- /// Set the time for this object void IMagnetCurrent::GetTime( int &ayear, int &amonth, int &aday, int &ahour, int &amin, int &asec) { tm mt; // use time structure from time.h to get time localtime_r( (const time_t*) &fTime, &mt ); // to build unix time from year,month, etc. ayear = 1900 + mt.tm_year; amonth = mt.tm_mon; aday = mt.tm_mday; ahour = mt.tm_hour; amin = mt.tm_min; asec = mt.tm_sec; } ///------------------------------------------------------------------------ /// Get index into each of the vectors of database query /// results for the current time void IMagnetCurrent::UpdateVectorTimeIdx(){ // Find index for current time for magnet current fMagI_isok = false; fI_idx=0; if ( fI_vec.size()<2 ) return; for (unsigned int i=0; i= long( (fI_vec[i].first) ) && fTime < long( (fI_vec[i+1].first) ) ) { fI_idx = i; fMagI_isok = true; break; } } return; } ///-------------------------------------------------------------------- /// Check if anouther chunk of data needs to be read from /// slow control database void IMagnetCurrent::CheckReadDB(){ if ( fI_vec.size() <= 3 || fTime < long( fI_vec[ 0 ].first ) || fTime > long( fI_vec[ fI_vec.size()-3 ].first ) ){ //std::cout<<" About to ReadSlowDB_I"<