00001 #include "SpillServerMon.h"
00002 #include "FillSpillServerMon.h"
00003 #include "SpillServerMonFinder.h"
00004
00005 #include "MessageService/MsgService.h"
00006 #include "MinosObjectMap/MomNavigator.h"
00007 #include "JobControl/JobCommand.h"
00008 #include "JobControl/JobCModuleRegistry.h"
00009
00010 #include "Validity/VldRange.h"
00011 #include "Validity/VldTimeStamp.h"
00012 #include "Validity/VldContext.h"
00013
00014 #include "DatabaseInterface/DbiResultPtr.h"
00015 #include "DatabaseInterface/DbiSqlContext.h"
00016 #include "DatabaseInterface/DbiValidityRec.h"
00017 #include "DatabaseInterface/DbiCascader.h"
00018 #include "DatabaseInterface/DbiTableProxyRegistry.h"
00019 #include "DatabaseInterface/DbiWriter.h"
00020
00021 #include "RawData/RawRecord.h"
00022 #include "RawData/RawHeader.h"
00023 #include "RawData/RawDaqHeader.h"
00024
00025 #include "RawData/RawDaqHeaderBlock.h"
00026 #include "RawData/RawSpillServerMonitorBlock.h"
00027
00028
00029
00030
00031
00032 ClassImp(FillSpillServerMon)
00033
00034 CVSID("$Id: FillSpillServerMon.cxx,v 1.6 2008/09/20 14:18:22 blake Exp $");
00035
00036 JOBMODULE(FillSpillServerMon,"FillSpillServerMon","FillSpillServerMon");
00037
00038 FillSpillServerMon::FillSpillServerMon() :
00039 fRecord(0),
00040 fCounter(0),
00041 fTime(0),
00042 fBegTime(-1),
00043 fEndTime(-1),
00044 fWriteToDatabase(0),
00045 fReadFromDatabase(0),
00046 fWriteOutResults(0),
00047 fWriteReportedSpills(1),
00048 fWritePredictedSpills(1),
00049 fWriteFakeSpills(0),
00050 fWriteLocalSpills(0),
00051 fSpillFile(0),
00052 fSpillTree(0)
00053 {
00054 MSG("SpillServerMon",Msg::kDebug) << " *** FillSpillServerMon::FillSpillServerMon() *** " << endl;
00055
00056 fDetector = Detector::kUnknown;
00057
00058 }
00059
00060 FillSpillServerMon::~FillSpillServerMon()
00061 {
00062 MSG("SpillServerMon",Msg::kDebug) << " *** FillSpillServerMon::~FillSpillServerMon() *** " << endl;
00063
00064 }
00065
00066 void FillSpillServerMon::BeginJob()
00067 {
00068 MSG("SpillServerMon",Msg::kInfo) << " *** FillSpillServerMon::BeginJob() *** " << endl;
00069
00070 }
00071
00072 JobCResult FillSpillServerMon::Get(MomNavigator* mom)
00073 {
00074 MSG("SpillServerMon",Msg::kInfo) << " *** FillSpillServerMon::Get(...) *** " << endl;
00075
00076 JobCResult result(JobCResult::kPassed);
00077
00078 TObject* momobject = 0;
00079 fRecord=0;
00080
00081
00082 TIter momitr(mom->FragmentIter());
00083 while((momobject = momitr())){
00084 if(momobject->InheritsFrom("RawRecord")){
00085 RawRecord* rawrec = dynamic_cast<RawRecord*>(momobject);
00086 fRecord=1; fCounter++;
00087 MSG("SpillServerMon",Msg::kInfo) << " *** RAW RECORD [" << fCounter << "] *** " << endl;
00088
00089
00090 if(fDetector==Detector::kUnknown){
00091 fDetector=rawrec->GetHeader()->GetVldContext().GetDetector();
00092 MSG("SpillServerMon",Msg::kDebug) << " Detector = " << fDetector << endl;
00093 }
00094
00095
00096 TIter rawrecitr = rawrec->GetRawBlockIter();
00097 TObject* tob = 0;
00098 while((tob = rawrecitr())){
00099 MSG("SpillServerMon",Msg::kDebug) << " " << tob->GetName() << endl;
00100 this->ProcessBlock(tob);
00101 }
00102
00103 }
00104 }
00105
00106 if( !fRecord ){
00107 MSG("SpillServerMon",Msg::kWarning) << " *** FAILED TO FIND RAW RECORD *** " << endl;
00108 return result.SetFailed();
00109 }
00110
00111 return result;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 const Registry& FillSpillServerMon::DefaultConfig() const
00126 {
00127 MSG("SpillServerMon",Msg::kDebug) << " *** FillSpillServerMon::DefaultConfig() *** " << endl;
00128
00129 static Registry r;
00130 r.SetName("SpillServerMon.config.default");
00131 r.UnLockValues();
00132 r.Set("WriteToDatabase",fWriteToDatabase);
00133 r.Set("ReadFromDatabase",fReadFromDatabase);
00134 r.Set("WriteOutResults",fWriteOutResults);
00135 r.Set("WriteReportedSpills",fWriteReportedSpills);
00136 r.Set("WritePredictedSpills",fWritePredictedSpills);
00137 r.Set("WriteFakeSpills",fWriteFakeSpills);
00138 r.Set("WriteLocalSpills",fWriteLocalSpills);
00139 r.LockValues();
00140
00141 return r;
00142 }
00143
00144 void FillSpillServerMon::Config(const Registry& r)
00145 {
00146 MSG("SpillServerMon",Msg::kDebug) << " *** FillSpillServerMon::Config() *** " << endl;
00147
00148 Int_t tmpint;
00149
00150 if(r.Get("WriteToDatabase",tmpint)) fWriteToDatabase = tmpint;
00151 if(r.Get("ReadFromDatabase",tmpint)) fReadFromDatabase = tmpint;
00152 if(r.Get("WriteOutResults",tmpint)) fWriteOutResults = tmpint;
00153 if(r.Get("WriteReportedSpills",tmpint)) fWriteReportedSpills = tmpint;
00154 if(r.Get("WritePredictedSpills",tmpint)) fWritePredictedSpills = tmpint;
00155 if(r.Get("WriteFakeSpills",tmpint)) fWriteFakeSpills = tmpint;
00156 if(r.Get("WriteLocalSpills",tmpint)) fWriteLocalSpills = tmpint;
00157
00158 MSG("SpillServerMon",Msg::kDebug)
00159 << " configuration: " << endl
00160 << " WriteToDatabase=" << fWriteToDatabase << endl
00161 << " ReadFromDatabase=" << fReadFromDatabase << endl
00162 << " WriteOutResults=" << fWriteOutResults << endl
00163 << " WriteReportedSpills=" << fWriteReportedSpills << endl
00164 << " WritePredictedSpills=" << fWritePredictedSpills << endl
00165 << " WriteFakeSpills=" << fWriteFakeSpills << endl
00166 << " WriteLocalSpills=" << fWriteLocalSpills << endl;
00167
00168 return;
00169 }
00170
00171 void FillSpillServerMon::HandleCommand(JobCommand* command)
00172 {
00173 TString cmd = command->PopCmd();
00174 if(cmd=="Set"){
00175 TString opt = command->PopOpt();
00176
00177 }
00178
00179 return;
00180 }
00181
00182 void FillSpillServerMon::EndJob()
00183 {
00184 MSG("SpillServerMon",Msg::kInfo) << " *** FillSpillServerMon::EndJob() *** " << endl;
00185
00186 MSG("SpillServerMon",Msg::kDebug) << " BegTime=" << fBegTime << " EndTime=" << fEndTime << endl;
00187
00188
00189 if(fWriteOutResults){
00190 this->WriteOutResults(0);
00191 }
00192
00193
00194 if( fWriteToDatabase ){
00195 this->WriteToDatabase();
00196 if(fWriteOutResults){
00197 this->WriteOutResults(1);
00198 }
00199 }
00200
00201
00202 this->ClearVectors();
00203
00204
00205 if( fReadFromDatabase ){
00206 this->ReadFromDatabase();
00207 if( fWriteOutResults ){
00208 this->WriteOutResults(2);
00209 }
00210 }
00211
00212
00213 this->ClearVectors();
00214
00215 if(fSpillFile){
00216 MSG("SpillServerMon",Msg::kInfo) << " *** saving info to file ... " << endl;
00217 TDirectory* tmpd = gDirectory;
00218 fSpillFile->cd();
00219 fSpillTree->Write();
00220 fSpillFile->Close();
00221 gDirectory = tmpd;
00222 MSG("SpillServerMon",Msg::kInfo) << " ... info saved to file *** " << endl;
00223 }
00224 }
00225
00226 void FillSpillServerMon::ClearVectors()
00227 {
00228 MSG("SpillServerMon",Msg::kDebug) << " *** FillSpillServerMon::ClearVectors() *** " << endl;
00229
00230 vecTimeStamp.clear();
00231 vecSpillStatus.clear();
00232 vecSpillType.clear();
00233 vecSpillTimeNDsec.clear();
00234 vecSpillTimeNDnanosec.clear();
00235 vecSpillTimeFDsec.clear();
00236 vecSpillTimeFDnanosec.clear();
00237 vecSpillTimeError.clear();
00238 vecRequestSec.clear();
00239 vecWindowOpen.clear();
00240 vecWindowClose.clear();
00241 }
00242
00243 void FillSpillServerMon::PrintResults()
00244 {
00245 MSG("SpillServerMon",Msg::kDebug) << " *** FillSpillServerMon::PrintResults() *** " << endl;
00246 }
00247
00248 void FillSpillServerMon::ProcessBlock(TObject* obj)
00249 {
00250
00251 if(obj->InheritsFrom("RawSpillServerMonitorBlock")){
00252 MSG("SpillServerMon",Msg::kDebug) << " ... found RawSpillServerMonitorBlock " << endl;
00253 RawSpillServerMonitorBlock* rdb = (RawSpillServerMonitorBlock*)(obj);
00254
00255 fTime = rdb->GetTimeStamp().GetSec();
00256 if( fBegTime<0 ) fBegTime=fTime;
00257 if( fTime>fEndTime ) fEndTime=fTime;
00258
00259 const mdSpillData_t& spilldata = rdb->GetSpillData();
00260 Int_t timestamp = fTime;
00261 Int_t spillstatus = spilldata.isKnown;
00262 Int_t spilltype = spilldata.isSpill;
00263 Int_t spilltimeND_sec = spilldata.spillTimeND.sec;
00264 Int_t spilltimeND_nsec = spilldata.spillTimeND.nsec;
00265 Int_t spilltimeFD_sec = spilldata.spillTimeFD.sec;
00266 Int_t spilltimeFD_nsec = spilldata.spillTimeFD.nsec;
00267 Int_t request_sec = spilldata.request_sec;
00268 Int_t windowopen_nsec = spilldata.windowOpen_nsec;
00269 Int_t windowclose_nsec = spilldata.windowClose_nsec;
00270
00271 Double_t spilltimeerror_temp = spilldata.spillTimeError_nsec;
00272 if( spilltimeerror_temp>2000000000.0 ) spilltimeerror_temp = 2000000000.0;
00273 if( spilltimeerror_temp<0 ) spilltimeerror_temp = -99999.0;
00274 Int_t spilltimeerror_nsec = (Int_t)(spilltimeerror_temp);
00275
00276 MSG("SpillServerMon",Msg::kDebug) << " SPILL INFO: " << endl
00277 << " timestamp=" << timestamp << endl
00278 << " spillstatus=" << spillstatus << endl
00279 << " spilltype=" << spilltype << endl
00280 << " spilltimeND_sec=" << spilltimeND_sec << endl
00281 << " spilltimeND_nsec=" << spilltimeND_nsec << endl
00282 << " spilltimeFD_sec=" << spilltimeFD_sec << endl
00283 << " spilltimeFD_nsec=" << spilltimeFD_nsec << endl
00284 << " spilltimeerror_nsec=" << spilltimeerror_nsec << endl
00285 << " request_sec=" << request_sec << endl
00286 << " windowopen_nsec=" << windowopen_nsec << endl
00287 << " windowclose_nsec=" << windowclose_nsec << endl;
00288
00289 Int_t writeme=0;
00290 if( ( spilltype == kSpillType_None && 0 )
00291 || ( spilltype == kSpillType_Reported && fWriteReportedSpills )
00292 || ( spilltype == kSpillType_Predicted && fWritePredictedSpills )
00293 || ( spilltype == kSpillType_Fake && fWriteFakeSpills )
00294 || ( spilltype == kSpillType_Local && fWriteLocalSpills ) ) writeme=1;
00295
00296 if( writeme ){
00297 MSG("SpillServerMon",Msg::kDebug) << " STORING SPILL... " << endl;
00298 vecTimeStamp.push_back(timestamp);
00299 vecSpillStatus.push_back(spillstatus);
00300 vecSpillType.push_back(spilltype);
00301 vecSpillTimeNDsec.push_back(spilltimeND_sec);
00302 vecSpillTimeNDnanosec.push_back(spilltimeND_nsec);
00303 vecSpillTimeFDsec.push_back(spilltimeFD_sec);
00304 vecSpillTimeFDnanosec.push_back(spilltimeFD_nsec);
00305 vecSpillTimeError.push_back(spilltimeerror_nsec);
00306 vecRequestSec.push_back(request_sec);
00307 vecWindowOpen.push_back(windowopen_nsec);
00308 vecWindowClose.push_back(windowclose_nsec);
00309 }
00310 }
00311
00312 }
00313
00314 void FillSpillServerMon::WriteToDatabase()
00315 {
00316 MSG("SpillServerMon",Msg::kDebug) << " *** FillSpillServerMon::WriteToDatabase() *** " << endl;
00317
00318 DbiCascader& cascader = DbiTableProxyRegistry::Instance().GetCascader();
00319
00320 if( !cascader.TableExists("SPILLSERVERMON") ){
00321 MSG("SpillServerMon",Msg::kDebug) << " Creating temporary SPILLSERVERMON table " << endl;
00322
00323 string tableDescr = "(SEQNO int, TIMESTAMP int, SPILLSTATUS int, SPILLTYPE int, SPILLTIMEND_SEC int, SPILLTIMEND_NANOSEC int, SPILLTIMEFD_SEC int, SPILLTIMEFD_NANOSEC int, SPILLTIMEERROR int, REQUEST_SEC int, WINDOWOPEN_NANOSEC int, WINDOWCLOSE_NANOSEC int)";
00324
00325 Int_t dbNoTempSpillServerMon = cascader.CreateTemporaryTable("SPILLSERVERMON",tableDescr);
00326
00327 MSG("SpillServerMon",Msg::kDebug) << " DbNoTempSpillServerMon=" << dbNoTempSpillServerMon << endl;
00328 }
00329
00330 if( cascader.TableExists("SPILLSERVERMON") ){
00331 if( vecTimeStamp.size()>0 ){
00332 MSG("SpillServerMon",Msg::kDebug) << " Writing to SPILLSERVERMON Table... " << endl;
00333
00334 VldTimeStamp start(fBegTime,0);
00335 VldTimeStamp finish(fEndTime+1,0);
00336 TString logentry("spillserver monitoring (");
00337 logentry.Append(start.AsString("s"));
00338 logentry.Append(" to ");
00339 logentry.Append(finish.AsString("s"));
00340 logentry.Append(")");
00341
00342 VldRange range(fDetector,SimFlag::kData,start,finish,logentry.Data());
00343
00344 Int_t aggNo = -1;
00345 Dbi::Task task = 0;
00346 VldTimeStamp creationDate;
00347
00348 DbiWriter<SpillServerMon> writer(range,aggNo,task,creationDate,0,logentry.Data());
00349
00350 for( unsigned int i=0; i<vecTimeStamp.size(); i++){
00351 SpillServerMon row(vecTimeStamp[i],vecSpillStatus[i],vecSpillType[i],vecSpillTimeNDsec[i],vecSpillTimeNDnanosec[i],vecSpillTimeFDsec[i],vecSpillTimeFDnanosec[i],vecSpillTimeError[i],vecRequestSec[i],vecWindowOpen[i],vecWindowClose[i]);
00352 writer << row;
00353 }
00354
00355 writer.Close();
00356
00357 MSG("SpillServerMon",Msg::kVerbose) << " " << logentry.Data() << endl;
00358 MSG("SpillServerMon",Msg::kVerbose) << " " << "Number Of Rows = " << vecTimeStamp.size() << endl;
00359 }
00360 }
00361
00362 return;
00363 }
00364
00365 void FillSpillServerMon::ReadFromDatabase()
00366 {
00367 MSG("SpillServerMon",Msg::kDebug) << " *** FillSpillServerMon::ReadFromDatabase() *** " << endl;
00368
00369 DbiCascader& cascader = DbiTableProxyRegistry::Instance().GetCascader();
00370
00371 if( cascader.TableExists("SPILLSERVERMON") ){
00372 if( vecTimeStamp.size()==0 ){
00373 MSG("SpillServerMon",Msg::kDebug) << " Reading from SPILLSERVERMON Table... " << endl;
00374
00375
00376
00377
00378
00379
00380 VldTimeStamp timestamp(fBegTime,0);
00381 VldContext vldc(fDetector,SimFlag::kData,timestamp);
00382 DbiResultPtr<SpillServerMon> resptr("SPILLSERVERMON",vldc,0);
00383
00384 MSG("SpillServerMon",Msg::kDebug) << " Number Of Rows = " << resptr.GetNumRows() << endl;
00385
00386 for( unsigned int i=0; i<resptr.GetNumRows();i++ ){
00387 const SpillServerMon* myspill = resptr.GetRow(i);
00388 vecTimeStamp.push_back(myspill->GetTimeStamp());
00389 vecSpillStatus.push_back(myspill->GetSpillStatus());
00390 vecSpillType.push_back(myspill->GetSpillType());
00391 vecSpillTimeNDsec.push_back(myspill->GetSpillTimeND_sec());
00392 vecSpillTimeNDnanosec.push_back(myspill->GetSpillTimeND_nsec());
00393 vecSpillTimeFDsec.push_back(myspill->GetSpillTimeFD_sec());
00394 vecSpillTimeFDnanosec.push_back(myspill->GetSpillTimeFD_nsec());
00395 vecSpillTimeError.push_back(myspill->GetSpillTimeError());
00396 vecRequestSec.push_back(myspill->GetRequest_sec());
00397 vecWindowOpen.push_back(myspill->GetWindowOpen_nsec());
00398 vecWindowClose.push_back(myspill->GetWindowClose_nsec());
00399 }
00400 }
00401 }
00402
00403 return;
00404 }
00405
00406 void FillSpillServerMon::WriteOutResults(Int_t source)
00407 {
00408 MSG("SpillServerMon",Msg::kVerbose) << " *** FillSpillServerMon::WriteOutResults() *** " << endl;
00409
00410 if(!fSpillFile){
00411 TString mystring("spillservermonitor.root");
00412 TDirectory* tmpd = gDirectory;
00413 fSpillFile = new TFile(mystring.Data(),"RECREATE");
00414 fSpillTree = new TTree("SpillServerMonitor","SpillServerMonitor");
00415 fSpillTree->SetAutoSave(300);
00416 fSpillTree->Branch("Source",&Source,"Source/I");
00417 fSpillTree->Branch("TimeStamp",&TimeStamp,"TimeStamp/I");
00418 fSpillTree->Branch("SpillStatus",&SpillStatus,"SpillStatus/I");
00419 fSpillTree->Branch("SpillType",&SpillType,"SpillType/I");
00420 fSpillTree->Branch("SpillTimeND_sec",&SpillTimeND_sec,"SpillTimeND_sec/I");
00421 fSpillTree->Branch("SpillTimeND_nsec",&SpillTimeND_nsec,"SpillTimeND_nsec/I");
00422 fSpillTree->Branch("SpillTimeFD_sec",&SpillTimeFD_sec,"SpillTimeFD_sec/I");
00423 fSpillTree->Branch("SpillTimeFD_sec",&SpillTimeFD_sec,"SpillTimeFD_sec/I");
00424 fSpillTree->Branch("SpillTimeFD_nsec",&SpillTimeFD_nsec,"SpillTimeFD_nsec/I");
00425 fSpillTree->Branch("SpillTimeError",&SpillTimeError,"SpillTimeError/I");
00426 fSpillTree->Branch("Request_sec",&Request_sec,"Request_sec/I");
00427 fSpillTree->Branch("WindowOpen_nsec",&WindowOpen_nsec,"WindowOpen_nsec/I");
00428 fSpillTree->Branch("WindowClose_nsec",&WindowClose_nsec,"WindowClose_nsec/I");
00429 gDirectory = tmpd;
00430 }
00431
00432 for( unsigned int i=0; i<vecTimeStamp.size(); i++){
00433 Source=source;
00434 TimeStamp=vecTimeStamp[i];
00435 SpillStatus=vecSpillStatus[i];
00436 SpillType=vecSpillType[i];
00437 SpillTimeND_sec=vecSpillTimeNDsec[i];
00438 SpillTimeND_nsec=vecSpillTimeNDnanosec[i];
00439 SpillTimeFD_sec=vecSpillTimeFDsec[i];
00440 SpillTimeFD_nsec=vecSpillTimeFDnanosec[i];
00441 SpillTimeError=vecSpillTimeError[i];
00442 Request_sec=vecRequestSec[i];
00443 WindowOpen_nsec=vecWindowOpen[i];
00444 WindowClose_nsec=vecWindowClose[i];
00445
00446 if(fSpillFile){
00447 TDirectory* tmpd = gDirectory;
00448 fSpillFile->cd();
00449 fSpillTree->Fill();
00450 gDirectory = tmpd;
00451 }
00452 }
00453
00454 }