00001
00002
00003
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00016
00017 #include "TFndSCCMan.h"
00018
00019 #include "TDatime.h"
00020
00021 #if defined(__CINT__)
00022 #include <netinet/in.h>
00023 #endif
00024 #include <sys/ioctl.h>
00025 #include <sys/socket.h>
00026
00027 ClassImp(TFndSCCMan)
00028
00029 TFndSCCMan *gSCCManager;
00030
00031
00032 TFndSCCMan::TFndSCCMan():
00033 fReceiveThread(),
00034 fSockDescr(),fLocSockAddr(),fCurReceivedEv(),
00035 fSendThread(),fCurSent(),
00036 fTimer(),fUpdateInterval(),fCounterUpdate(),fRatioUpdate(),
00037 fMustSendToDAQ(),fMustSaveLocalRaw(),
00038 fSendingData(),
00039 fCurLogFile(),fCurOutRawFile()
00040 {
00041 Init();
00042 }
00043
00044
00045 TFndSCCMan::~TFndSCCMan(){
00046
00047 StopAllThreads();
00048
00049
00050 Printf("Performing delete operations.");
00051
00052 delete fTimer;
00053
00054
00055 gSCCManager = 0;
00056 }
00057
00058
00059 void TFndSCCMan::Init(){
00060
00061 gSCCManager = this;
00062
00063 fCounterUpdate = 0;
00064 fRatioUpdate = 6;
00065
00066
00067
00068 fUpdateInterval = 300;
00069
00070 fTimer = new TTimer();
00071 fTimer->Connect("Timeout()","TFndSCCMan",this,"ScheduledOperation()");
00072
00073 fSendingData = kFALSE;
00074
00075
00076
00077
00078
00079 fLogFilesPath[(Int_t) E_SCC_Log_Status] = gSystem->ExpandPathName("$FND_SCC_LOG");
00080 fLogFilesPath[(Int_t) E_SCC_Log_Store] = gSystem->ExpandPathName("$FND_SCC_LOG_DATA_STORE");
00081 fRawFilesPath = gSystem->ExpandPathName("$FND_SCC_RAW");
00082
00083 }
00084
00085
00086 void TFndSCCMan::StopAllThreads(){
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 for(Int_t slow_id=0;slow_id<K_N_SLOWS;slow_id++){
00102 StopReceiveThread(slow_id);
00103 usleep(80000);
00104 }
00105 Printf("All receiver threads succesfully stopped.");
00106
00107 StopSendThread();
00108 Printf("Sender thread succesfully stopped.");
00109
00110 for(Int_t i=(Int_t)E_SCC_Log_Status; i< (Int_t)E_SCC_Log_End; i++){
00111 CloseLogFile((E_SCC_LogType)i);
00112 }
00113 CloseRawFile();
00114 }
00115
00116
00117 void* TFndSCCMan::Receiver(void *arg){
00118
00119
00120
00121 Int_t sock_idx = (Int_t)(arg);
00122 Int_t slow_det = gSCCManager->SockId2SlowId(sock_idx);
00123
00124 TThread::SetCancelOff();
00125 TThread::SetCancelDeferred();
00126
00127 Printf("Starting receiver thread for \"%s\" (UDP port: %i)",fSlowDetNames[slow_det].Data(),K_SLOW_UDP_PORT[sock_idx]);
00128
00129 Int_t SkDesc = gSCCManager->GetSocketDescriptor(sock_idx);
00130 UInt_t LocLength;
00131
00132 UInt_t RawData[K_SLOW_SOCK_LEN] = {0};
00133 struct sockaddr_in ShrSockAddr;
00134 LocLength=sizeof(gSCCManager->GetLocSockAddr(sock_idx));
00135
00136 Int_t istop = 0;
00137 while(istop == 0) {
00138 TThread::CancelPoint();
00139 Int_t is_there_data=0;
00140
00141 if(gSCCManager->fSendingData == kTRUE){
00142 gROOT->Info("TFndSCCMan::Receiver","Now sending data (waiting...)");
00143
00144 usleep(100000);
00145
00146 usleep(50000);
00147 continue;
00148 }
00149
00150
00151 if(K_DEBUG_LEV > 1) Printf(" ---> waiting for a new event (%s)",fSlowDetNames[slow_det].Data());
00152
00153 while(is_there_data == 0){
00154 TThread::CancelPoint();
00155 ioctl(SkDesc,FIONREAD,(Int_t *)&is_there_data);
00156 if(is_there_data) break;
00157 usleep(100000);
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 Int_t nread = recvfrom(SkDesc, (char*)RawData, sizeof(RawData),0 ,(struct sockaddr*) &ShrSockAddr ,&LocLength);
00170 gSCCManager->IncrReceivedEv(sock_idx);
00172
00173 if(K_DEBUG_LEV > 0) Printf(" ---> %s UDP packet received: nread = %i (event %i);",fSlowDetNames[slow_det].Data(),nread,gSCCManager->GetReceivedEv(sock_idx));
00174 TThread::CancelPoint();
00176 Int_t dpart = 0;
00177 if(sock_idx == SOCK_SIL_MSG){
00178 dpart = 1;
00180 if(K_DEBUG_LEV > 0) {
00181 Printf("+++++++ SIL MESSAGE REPORT ++++++++");
00182 Printf("%s",(Char_t *) RawData);
00183 Printf("+++++++++++++++++++++++++++++++");
00184 }
00186 }
00187 else if(sock_idx == SOCK_SIL){
00188 dpart = 2;
00190 if(K_DEBUG_LEV > 0) {
00191 Printf("+++++++ SIL COMPLETE REPORT ++++++++");
00192 Printf("%s",(Char_t *) RawData);
00193 Printf("+++++++++++++++++++++++++++++++");
00194 }
00196 gSCCManager->GetSilRecode() -> SetReceivedString(RawData);
00197 gSCCManager->GetSilRecode() -> ReadSilString();
00198 gSCCManager->GetSilRecode() -> ReBuildSilSlowRaw();
00199
00200 UInt_t *silraw = gSCCManager->GetSilRecode() ->GetRawBuf();
00201
00202 bzero(RawData,sizeof(RawData));
00203 memcpy(RawData, silraw, silraw[0] );
00204 }
00205
00206
00207 gSCCManager->SetSingleRawData(slow_det,RawData,dpart);
00208 gSCCManager->ReceivedEvent(slow_det,dpart);
00210
00211 }
00212 return 0;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 Int_t TFndSCCMan::IsSingleReceiveThreadRunning(Int_t sock_idx){
00241
00242
00243
00244 Int_t slow_det = SockId2SlowId(sock_idx);
00245 if(K_DEBUG_LEV > 1) Printf("TFndSCCMan::IsSingleReceiveThreadRunning ---> Checking thread state for %s (socket id: %d)",fSlowDetNames[slow_det].Data(),sock_idx);
00246 if(K_DEBUG_LEV > 2){
00247 TThread::Ps();
00248 Printf("Thread address: %d",fReceiveThread[sock_idx]);
00249 }
00250
00251 if(!fReceiveThread[sock_idx]){
00252 if(K_DEBUG_LEV > 1) Printf("TFndSCCMan::IsSingleReceiveThreadRunning ---> Thread not found.");
00253 return -1;
00254 }
00255
00256 Int_t state = fReceiveThread[sock_idx]->GetState();
00257 if(K_DEBUG_LEV > 1) Printf("TFndSCCMan::IsSingleReceiveThreadRunning ---> Thread state ID is %d",state);
00258
00259 if(K_DEBUG_LEV > 1){
00260 if(state != TThread::kRunningState) Printf("TFndSCCMan::IsSingleReceiveThreadRunning ---> Thread found, but not running");
00261 else Printf("TFndSCCMan::IsSingleReceiveThreadRunning ---> Thread found: running.");
00262 }
00263 return state;
00264 }
00265
00266
00267 Int_t TFndSCCMan::StartSingleReceiveThread(Int_t sock_idx){
00268
00269 Int_t slow_det = SockId2SlowId(sock_idx);
00270 TString th_name = TString("Receiver_");
00271 th_name+=fSlowDetNames[slow_det];
00272 if(sock_idx == SOCK_SIL_MSG) th_name+="_msg";
00273
00274 Printf("Starting new receiver thread (\"%s\")",fSlowDetNames[slow_det].Data());
00275
00276 UInt_t LocLength=sizeof(fLocSockAddr[sock_idx]);
00277
00278
00279 bzero((Char_t *)&fLocSockAddr[sock_idx], LocLength);
00280 fLocSockAddr[sock_idx].sin_family = AF_INET;
00281 fLocSockAddr[sock_idx].sin_addr.s_addr = INADDR_ANY;
00282
00283 fLocSockAddr[sock_idx].sin_port = htons(K_SLOW_UDP_PORT[sock_idx]);
00284
00285
00286
00287 if ( (fSockDescr[sock_idx] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00288 perror("Socket creation error");
00289 return 0;
00290 }
00291
00292
00293 if(K_DEBUG_LEV > 0) Printf("New socket created with socket descriptor = %d; now binding it...",fSockDescr[sock_idx]);
00294 Int_t bind_output = bind(fSockDescr[sock_idx],(struct sockaddr*) &fLocSockAddr[sock_idx], LocLength);
00295 if(K_DEBUG_LEV > 0) Printf("...done ( bind_output= %d)",bind_output);
00296
00297 setsockopt(fSockDescr[sock_idx],SOL_SOCKET,SO_SNDBUF,&K_SLOW_SOCK_LEN,sizeof(K_SLOW_SOCK_LEN));
00298
00299
00300
00301 fReceiveThread[sock_idx] = new TThread(th_name,Receiver,(void *)sock_idx);
00302
00303 if(K_DEBUG_LEV > 1) TThread::Ps();
00304 fReceiveThread[sock_idx]->Run();
00305 return 0;
00306
00307 }
00308
00309
00310 Int_t TFndSCCMan::StopSingleReceiveThread(Int_t sock_idx){
00311
00312 Int_t slow_det = SockId2SlowId(sock_idx);
00313 TString ThName = "Receiver_"; ThName+=fSlowDetNames[slow_det];
00314
00315
00316
00317
00318 if(!fReceiveThread[sock_idx]){
00319 if(K_DEBUG_LEV > 0) Printf("%s thread not found",ThName.Data());
00320 return -1;
00321 }
00322 if(fReceiveThread[sock_idx]->GetState() != TThread::kRunningState){
00323 if(K_DEBUG_LEV > 0) Printf("%s thread found, but not running",fReceiveThread[sock_idx]->GetName());
00324 return -2;
00325 }
00326
00327 Printf("Stopping receiver thread (%s)",fReceiveThread[sock_idx]->GetName());
00328 fReceiveThread[sock_idx]->Kill();
00329 TThread::Delete(fReceiveThread[sock_idx]);
00330 delete fReceiveThread[sock_idx];
00331 fReceiveThread[sock_idx] = 0;
00332 if(K_DEBUG_LEV > 0) Printf("Now closing socket (socket descriptor = %d)",fSockDescr[sock_idx]);
00333
00334 Int_t close_output = close(fSockDescr[sock_idx]);
00335 if(K_DEBUG_LEV > 0) Printf("...done ( bind_output= %d)",close_output);
00336 else Printf("...done");
00337 return 0;
00338 }
00339
00340
00341 Int_t TFndSCCMan::IsReceiveThreadRunning(Int_t slow_det){
00342
00343
00344
00345 Int_t ret_val[2] = {0};
00346 Int_t sock_idx[2] = {0};
00347 sock_idx[0] = SlowId2SockId(slow_det,1);
00348 sock_idx[1] = SlowId2SockId(slow_det,2);
00349
00350 ret_val[0] = IsSingleReceiveThreadRunning(sock_idx[0]);
00351 if(sock_idx[1] != -1) ret_val[1] = IsSingleReceiveThreadRunning(sock_idx[1]);
00352 else return ret_val[0];
00353
00354 if(ret_val[0] == -1 && ret_val[1] == -1){
00355 if(K_DEBUG_LEV > 0) Printf(" TFndSCCMan::IsReceiveThreadRunning ---> Thread not found for slow_det %d",slow_det);
00356 return -1;
00357 }
00358
00359 if(ret_val[0] != ret_val[1]) Printf("Warning in TFndSCCMan::IsReceiveThreadRunning ---> No match between two threads");
00360 return ret_val[0];
00361 }
00362
00363
00364 Int_t TFndSCCMan::StartReceiveThread(Int_t slow_det){
00365
00366
00367 Int_t ret_val[2] = {0};
00368
00369 Int_t sock_idx[2] = {0};
00370 sock_idx[0] = SlowId2SockId(slow_det,1);
00371 sock_idx[1] = SlowId2SockId(slow_det,2);
00372 ret_val[0] = StartSingleReceiveThread(sock_idx[0]);
00373
00374 if(sock_idx[1] != -1) ret_val[1] = StartSingleReceiveThread(sock_idx[1]);
00375
00376 Int_t ret_tot = 0;
00377 return ret_tot;
00378
00379 }
00380
00381
00382 Int_t TFndSCCMan::StopReceiveThread(Int_t slow_det){
00383
00384
00385
00386
00387
00388 Int_t ret_val[2] = {0};
00389
00390 Int_t sock_idx[2] = {0};
00391 sock_idx[0] = SlowId2SockId(slow_det,1);
00392 sock_idx[1] = SlowId2SockId(slow_det,2);
00393 ret_val[0] = StopSingleReceiveThread(sock_idx[0]);
00394 if(sock_idx[1] != -1) ret_val[1] = StopSingleReceiveThread(sock_idx[1]);
00395
00396 Int_t ret_tot = 0;
00397 return ret_tot;
00398
00399 }
00400
00401
00402 Int_t TFndSCCMan::StartSendThread(){
00403
00404
00405 fTimer->Start(fUpdateInterval * 1000, kFALSE);
00406 fTimer->TurnOn();
00407
00408
00409
00410
00411
00412
00413
00414 TString cur_lognam = "";
00415 for(Int_t lognum = 1; lognum <= 99999; lognum++){
00416 cur_lognam = "";
00417 TString lnum_str = "";
00418 lnum_str += lognum;
00419 while(lnum_str.Sizeof() < 6 ) lnum_str.Prepend("0");
00420 cur_lognam.Form("SCC_%s.log",lnum_str.Data());
00421
00422 TString chkstr = "";
00423 chkstr.Form( "%s/%s",fLogFilesPath[(Int_t) E_SCC_Log_Status].Data(),cur_lognam.Data() );
00424 if( gSystem->AccessPathName(chkstr.Data()) ) break;
00425 }
00426
00427 OpenLogFile(E_SCC_Log_Status,fLogFilesPath[(Int_t) E_SCC_Log_Status], cur_lognam.Data(),kFALSE);
00428 WriteLogFile(E_SCC_Log_Status,"Date\t\tTime\t\tTOFINO\tSIL\tLMD\tSTB\tTOFONE\tGAS");
00429
00430
00431 for(Int_t lognum = 1; lognum <= 99999; lognum++){
00432 cur_lognam = "";
00433 TString lnum_str = "";
00434 lnum_str += lognum;
00435 while(lnum_str.Sizeof() < 6 ) lnum_str.Prepend("0");
00436 cur_lognam.Form("SCC_data_store_%s.log",lnum_str.Data());
00437
00438 TString chkstr = "";
00439 chkstr.Form( "%s/%s",fLogFilesPath[(Int_t) E_SCC_Log_Store].Data(),cur_lognam.Data() );
00440 if( gSystem->AccessPathName(chkstr.Data()) ) break;
00441 }
00442
00443 OpenLogFile(E_SCC_Log_Store,fLogFilesPath[(Int_t) E_SCC_Log_Store], cur_lognam.Data(),kFALSE);
00444 TString st_tit = "\n --------------------------------------------\n";
00445 st_tit += " | SCC Data store log |\n";
00446 st_tit += " --------------------------------------------\n\n";
00447 WriteLogFile(E_SCC_Log_Store,st_tit);
00448
00449 return 0;
00450 }
00451
00452
00453 Int_t TFndSCCMan::StopSendThread(){
00454
00455
00456 fTimer->TurnOff();
00457
00458 TString ThName = "SCC_SenderThread";
00459 if(!fSendThread){
00460 if(K_DEBUG_LEV > 0) Printf("%s thread not found",ThName.Data());
00461 return -1;
00462 }
00463 if(fSendThread->GetState() != TThread::kRunningState){
00464 if(K_DEBUG_LEV > 0) Printf("%s thread found, but not running",fSendThread->GetName());
00465 return -2;
00466 }
00467
00468 Printf("Stopping sender thread (%s)",fSendThread->GetName());
00469 fSendThread->Kill();
00470 TThread::Delete(fSendThread);
00471 delete fSendThread;
00472 fSendThread = 0;
00473
00474 return 0;
00475 }
00476
00477
00478 Int_t TFndSCCMan::IsSendThreadThreadRunning(){
00479
00480
00481 if(K_DEBUG_LEV > 1) Printf("TFndSCCMan::IsSendThreadRunning ---> Checking thread state...");
00482 if(K_DEBUG_LEV > 2){
00483 TThread::Ps();
00484 Printf("Thread address: %d",fSendThread);
00485 }
00486
00487 if(!fSendThread){
00488 if(K_DEBUG_LEV > 1) Printf("TFndSCCMan::IsSendThreadRunning ---> Thread not found.");
00489 return -1;
00490 }
00491
00492 Int_t state = fSendThread->GetState();
00493 if(K_DEBUG_LEV > 1) Printf("TFndSCCMan::IsSendThreadRunning ---> Thread state ID is %d",state);
00494
00495 if(K_DEBUG_LEV > 1){
00496 if(state != TThread::kRunningState) Printf("TFndSCCMan::IsSendThreadRunning ---> Thread found, but not running");
00497 else Printf("TFndSCCMan::IsSendThreadRunning ---> Thread found: running.");
00498 }
00499 return state;
00500
00501 }
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524 Int_t TFndSCCMan::SockId2SlowId(Int_t sock_idx){
00525
00526 Int_t slow_det = -1;
00527
00528 switch(sock_idx){
00529 case SOCK_TOFI: slow_det = TOFI; break;
00530 case SOCK_TOFO: slow_det = TOFO; break;
00531 case SOCK_LMD : slow_det = LMD; break;
00532 case SOCK_STB : slow_det = STB; break;
00533 case SOCK_GAS : slow_det = GAS; break;
00534 case SOCK_SIL : slow_det = SIL; break;
00535 case SOCK_SIL_MSG: slow_det = SIL; break;
00536 case SOCK_MAG : slow_det = MAG; break;
00537 default: Printf("TFndSCCMan::Receiver BUG! socket index \"%i\" not available!",sock_idx); return -999;
00538 }
00539 return slow_det;
00540 }
00541
00542
00543 Int_t TFndSCCMan::SlowId2SockId(Int_t slow_det,Int_t part){
00544
00545
00546
00547
00548
00549
00550 if(part==2 && slow_det!=SIL) return -1;
00551 switch(slow_det){
00552 case TOFI: return SOCK_TOFI; break;
00553 case TOFO: return SOCK_TOFO; break;
00554 case LMD : return SOCK_LMD; break;
00555 case STB : return SOCK_STB; break;
00556 case GAS : return SOCK_GAS; break;
00557 case SIL :
00558 if(part==1) return SOCK_SIL_MSG;
00559 else if(part==2) return SOCK_SIL;
00560 else Printf("TFndSCCMan::SlowId2SockId BUG!");
00561 break;
00562 case MAG : return SOCK_MAG; break;
00563 default: Printf("TFndSCCMan::Receiver BUG! slow detector index \"%i\" not available!",slow_det); return -999;
00564 }
00565 Printf("TFndSCCMan::Receiver BUG! You should never read this message!");
00566 return -999;
00567 }
00568
00569
00570 void TFndSCCMan::PrintSlowDetDataValues(Int_t slow_det){
00571
00572 if( !IsDataReady(slow_det) ) return;
00573
00574 for(UInt_t ch=0;ch<fSingleRawData[slow_det][HdrDet_nchans];ch++){
00575 UInt_t data_offset = (UInt_t)( (fSingleRawData[slow_det][HdrDet_dataoff]) / 4);
00576 UInt_t entry = fSingleRawData[slow_det][data_offset + ch];
00577
00578 Bool_t power;
00579 UInt_t voltage;
00580 UInt_t current;
00581 Bool_t trip;
00582 DecodeDataElement(entry, power, voltage, current, trip);
00583
00585
00586
00587 if( slow_det == (Int_t)(STB) ){
00588 TDatime StbSysTime(fSingleRawData[(Int_t)(STB)][(Int_t)(HdrDet_systime)]);
00589 if(StbSysTime.Convert() <= FROOT::DTak_2006_SftwStopTime) power = !power;
00590 }
00592
00593 TString pow_str = (power) ? "ON" : "OFF";
00594 TString trip_str = (trip) ? "TRIP!" : "";
00595
00596 TString pr_msg;
00597 pr_msg.Form("%s \t PW: %s \t Voltage: %u \t Current: %u \t %s",
00598 GetChannelName(slow_det,ch).Data(),
00599 pow_str.Data(),
00600 voltage,
00601 current,
00602 trip_str.Data()
00603 );
00604
00605 Printf(pr_msg.Data());
00606 }
00607 }
00608
00609
00610 void TFndSCCMan::PrintSlowDetMessage(Int_t slow_det){
00611
00612 if( !IsDataReady(slow_det) ) return;
00613
00614 UInt_t msg_offset = (UInt_t)( (fSingleRawData[slow_det][HdrDet_msgoff]) / 4);
00615
00616 TString msg;
00617 msg.Form("%s", (const Char_t *) (&fSingleRawData[slow_det][msg_offset]));
00618 Printf(msg);
00619
00620 }
00621
00622
00623 void TFndSCCMan::PrintSlowDetMiscValues(Int_t slow_det){
00624
00625 if( !IsDataReady(slow_det) ) return;
00626
00627 UInt_t mnum = 0;
00628
00629 UInt_t misc_offset = (UInt_t)( (fSingleRawData[slow_det][HdrDet_miscoff]) / 4);
00630 UInt_t end_num = (UInt_t)( (fSingleRawData[slow_det][HdrDet_evlen]) / 4);
00631
00632 if(K_DEBUG_LEV> 0){
00633 Printf("misc_offset: %u",misc_offset);
00634 Printf("end_num: %u",end_num);
00635 }
00636
00637 UInt_t nmaxmisc = (UInt_t)(K_MISC_MAX_SIZE / 4 );
00638 Float_t divider[nmaxmisc];
00639 TString misc_nam[nmaxmisc];
00640
00641 for(Int_t i=0;i<nmaxmisc;i++){
00642 divider[i] = 1;
00643 misc_nam[i].Form("Misc[%i]",i);
00644 }
00645
00646
00647 switch(slow_det){
00648 case TOFI:
00649
00650 break;
00651 case TOFO:
00652
00653 break;
00654 case LMD:
00655 misc_nam[0] = "Mixture (C4H10 %) ";
00656 divider[0] = 100;
00657 break;
00658 case STB:
00659 misc_nam[0] = "Delta-P Mixer ";
00660 misc_nam[1] = "Temp Mixer ";
00661 misc_nam[2] = "Ethan flux ";
00662 misc_nam[3] = "Argon flux ";
00663 for(Int_t mid= 4;mid<=9;mid++) misc_nam[mid].Form("Flow IN %d",mid-3);
00664 for(Int_t mid=10;mid<=15;mid++) misc_nam[mid].Form("Flow OUT %d",mid-9);
00665
00666 for(Int_t i=0;i<nmaxmisc;i++) divider[i] = 100;
00667
00668 break;
00669 case GAS:
00670
00671 break;
00672 case SIL:
00673
00674 return;
00675 break;
00676 case MAG:
00677
00678 break;
00679 default:
00680 Printf("GetChannelName","Slow control ID \"%d\" not available",slow_det);
00681 return;
00682 }
00683
00684
00685 for(UInt_t ch = misc_offset; ch < end_num; ch++){
00686 Float_t res_data = ( (Float_t) GetMiscInfo(slow_det,mnum)) / divider[mnum];
00687 Printf("%s: %.2f",misc_nam[mnum].Data(),res_data);
00688 mnum++;
00689 }
00690
00691 }
00692
00693
00694 Int_t TFndSCCMan::PrintSlowDetInfo(Int_t slow_det, Bool_t check_receive_thread){
00695
00696
00697
00698
00699 if( !IsDataReady(slow_det) ) return -1;
00700
00701 if(K_DEBUG_LEV> 0) Printf("TFndSCCMan::PrintSlowDetInfo called for %s",fSlowDetNames[slow_det].Data());
00702
00703 if(check_receive_thread && IsReceiveThreadRunning(slow_det) != TThread::kRunningState){
00704 Warning("PrintSlowDetInfo","Can not print info for %s: receiver not running",fSlowDetNames[slow_det].Data());
00705 return -1;
00706 }
00707
00708 Printf("\n\n --------------------------------------------");
00709 Printf("Printing information for detector \"%s\"",fSlowDetNames[slow_det].Data());
00710
00711
00712 Printf("\n ----------------> HEADER <------------------");
00713 PrintHeader(slow_det);
00714
00715
00716 Printf("\n -----------------> DATA <-------------------");
00717 PrintSlowDetDataValues(slow_det);
00718
00719
00720 Printf("\n ---------------> MESSAGES <-----------------");
00721 PrintSlowDetMessage(slow_det);
00722
00723
00724 Printf("\n -----------------> MISC <-------------------");
00725 PrintSlowDetMiscValues(slow_det);
00726 Printf(" --------------------------------------------\n\n");
00727
00728 if(K_DEBUG_LEV> 0) Printf("TFndSCCMan::PrintSlowDetInfo completed for %s",fSlowDetNames[slow_det].Data());
00729 return 0;
00730 }
00731
00732
00733 Int_t TFndSCCMan::PrintAllInfo(Bool_t check_receive_thread) {
00734
00735
00736
00737
00738 Bool_t at_least_one = kFALSE;
00739 Printf("TFndSCCgui::PrintAllInfo called");
00740
00741 for(Int_t i=0;i<K_N_SLOWS;i++){
00742 Int_t res = PrintSlowDetInfo(i,check_receive_thread);
00743 if(res != 0) continue;
00744 at_least_one = kTRUE;
00745 }
00746
00747 if(!at_least_one){
00748 Warning("PrintAllInfo","Can not print info for any slow control");
00749 return -1;
00750 }
00751
00752 return 0;
00753 }
00754
00755
00756 Int_t TFndSCCMan::ScheduledOperation(){
00757
00758
00759
00760
00761 Bool_t at_least_one = kFALSE;
00762 for(Int_t slow_det = (Int_t)TOFI ; slow_det <= (Int_t)MAG ; slow_det++){
00763 if(IsReceiveThreadRunning(slow_det) == TThread::kRunningState) at_least_one = kTRUE;
00764 }
00765
00766 if(!at_least_one){
00767 Info("ScheduledOperation","No receiver running: can not build complete data");
00768 return -1;
00769 }
00770
00771 Info("ScheduledOperation","Performing scheduled operation");
00772
00773 fSendingData = kTRUE;
00774
00775 fCounterUpdate++;
00776
00777
00778 if(fCounterUpdate >= fRatioUpdate ) {
00779 UpdateCurrentLogFile();
00780 fCounterUpdate = 0;
00781 }
00782
00783 ScheduledOperationPerformed(E_SCC_MAN_GuiComm_Building);
00784
00785 TString build_message;
00786 if( BuildGlobalEvent(build_message) != 0){
00787 fSendingData = kFALSE;
00788 return -1;
00789 }
00790 WriteLogFile(E_SCC_Log_Store,build_message);
00791
00792
00794 if(fMustSaveLocalRaw){
00795
00796 UpdateCurrentOutRawFile();
00797 Info("ScheduledOperation","Data stored to local raw file");
00798 ScheduledOperationPerformed(E_SCC_MAN_GuiComm_Stored);
00799 }
00800
00801 if(fMustSendToDAQ){
00802
00803
00804
00805 ScheduledOperationPerformed(E_SCC_MAN_GuiComm_Sent);
00806 }
00807
00809
00810 ScheduledOperationPerformed(E_SCC_MAN_GuiComm_Idle,kTRUE);
00811
00812
00813
00814
00815 fSendingData = kFALSE;
00816
00817
00818 return 0;
00819 }
00820
00821
00822
00823 void TFndSCCMan::OpenLogFile(E_SCC_LogType log_type,const TString &fdir, const TString &fnam,const Bool_t &reset_old){
00824
00825 if( gSystem->AccessPathName( gSystem->ExpandPathName(fdir.Data()) ) ){
00826 Printf("\n\n\n *********************************************************************");
00827 Error("OpenLogFile","directory \"%s\" not found: can not start");
00828 Info("OpenLogFile","You may need to give the \"mount -a\" command as super-user (root user)");
00829 Printf(" *********************************************************************\n\n\n");
00830 gApplication->Terminate();
00831 }
00832
00833 CloseLogFile(log_type);
00834
00835 TString path_str = "";
00836 path_str.Form("%s/%s",gSystem->ExpandPathName(fdir.Data()),fnam.Data());
00837
00838
00839 fCurLogFile[(Int_t)log_type] = new ofstream();
00840 if(reset_old) fCurLogFile[(Int_t)log_type]->open(path_str.Data(),ofstream::out | ofstream::trunc);
00841 else fCurLogFile[(Int_t)log_type]->open(path_str.Data(),ofstream::out | ofstream::app);
00842
00843 }
00844
00845
00846 void TFndSCCMan::CloseLogFile(E_SCC_LogType log_type){
00847
00848 if(!fCurLogFile[(Int_t)log_type]) return;
00849 fCurLogFile[(Int_t)log_type]->close();
00850 delete fCurLogFile[(Int_t)log_type];
00851 fCurLogFile[(Int_t)log_type] = 0;
00852
00853 }
00854
00855
00856 void TFndSCCMan::WriteLogFile(E_SCC_LogType log_type,const TString &line){
00857
00858 if(!fCurLogFile[(Int_t)log_type]) return;
00859 *fCurLogFile[(Int_t)log_type] << line.Data() << endl;
00860
00861 }
00862
00863
00864 void TFndSCCMan::UpdateCurrentLogFile(){
00865
00866 if(!fCurLogFile[(Int_t)E_SCC_Log_Status]) return;
00867
00868 TDatime timenow;
00869 timenow.Set();
00870
00871 TString cur_date = "";
00872 cur_date.Form("%2d/%2d/%4d",
00873 timenow.GetDay(),
00874 timenow.GetMonth(),
00875 timenow.GetYear()
00876 );
00877
00878 TString cur_time = "";
00879 cur_time.Form("%2d:%2d:%2d",
00880 timenow.GetHour(),
00881 timenow.GetMinute(),
00882 timenow.GetSecond()
00883 );
00884
00885 TString det_status[K_N_SLOWS];
00886 det_status[TOFI] = "-----";
00887 det_status[TOFO] = "-----";
00888 det_status[LMD] = "-----";
00889 det_status[STB] = "-----";
00890 det_status[GAS] = "-----";
00891 det_status[SIL] = "-----";
00892 det_status[MAG] = "-----";
00893
00894
00895 TString CmpStr[3] = {0};
00896 CmpStr[0] = "ok";
00897 CmpStr[1] = "warning";
00898 CmpStr[2] = "error";
00899
00900 for(Int_t i=0; i<K_N_SLOWS; i++ ){
00901 TString d_mes = GetMessagePart(i);
00902 if(d_mes.IsNull()) continue;
00903 Printf(d_mes.Data());
00904 if(!strncasecmp(CmpStr[0],d_mes.Data(),CmpStr[0].Sizeof()-1)) det_status[i] = "ACTIVE";
00905 else if(!strncasecmp(CmpStr[1],d_mes.Data(),CmpStr[1].Sizeof()-1)) det_status[i] = "WARNING";
00906 else if(!strncasecmp(CmpStr[2],d_mes.Data(),CmpStr[2].Sizeof()-1)) det_status[i] = "ERROR";
00907 }
00908
00909
00910
00911 TString msg = "";
00912 msg.Form("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
00913 cur_date.Data(),
00914 cur_time.Data(),
00915 det_status[TOFI].Data(),
00916 det_status[SIL] .Data(),
00917 det_status[LMD] .Data(),
00918 det_status[STB] .Data(),
00919 det_status[TOFO].Data(),
00920 det_status[GAS] .Data()
00921 );
00922
00923 WriteLogFile(E_SCC_Log_Status,msg);
00924 }
00925
00926
00927
00928
00929 void TFndSCCMan::OpenOutRawFile(const Bool_t &reset_old){
00930
00931
00932 TString fdir = fRawFilesPath;
00933
00934 TDatime datim;
00935 datim.Set();
00936
00937 TString fnam;
00938 fnam.Form("%d_%d_%d.scc.raw",
00939 datim.GetDay(),
00940 datim.GetMonth(),
00941 datim.GetYear()
00942 );
00943
00944
00945 if( gSystem->AccessPathName( gSystem->ExpandPathName(fdir.Data()) ) ){
00946 Printf("\n\n\n *********************************************************************");
00947 Error("OpenOutRawFile","directory \"%s\" not found: can not start");
00948 Info("OpenOutRawFile","You may need to give the \"mount -a\" command as super-user (root user)");
00949 Printf(" *********************************************************************\n\n\n");
00950 gApplication->Terminate();
00951 }
00952
00953 CloseOutRawFile();
00954
00955 TString path_str = "";
00956 path_str.Form("%s/%s",gSystem->ExpandPathName(fdir.Data()),fnam.Data());
00957
00958 fCurOutRawFile = new ofstream();
00959 if(reset_old) fCurOutRawFile->open(path_str.Data(),ofstream::out | ios::binary | ofstream::trunc);
00960 else fCurOutRawFile->open(path_str.Data(),ofstream::out | ios::binary | ofstream::app);
00961
00962 }
00963
00964
00965 void TFndSCCMan::CloseOutRawFile(){
00966
00967 if(!fCurOutRawFile) return;
00968 fCurOutRawFile->close();
00969 delete fCurOutRawFile;
00970 fCurOutRawFile = 0;
00971
00972 }
00973
00974
00975 void TFndSCCMan::WriteOutRawFile(UInt_t *data){
00976
00978
00979
00981
00982 if(!fCurOutRawFile) return;
00983 if(data[0] <= 0 || data[0] >= K_TOT_SCC_MAX_SIZE){
00984 Error("WriteOutRawFile","raw-buffer too big");
00985 return;
00986 }
00987
00988 Printf("\nWriting event (%u bytes)",data[0]);
00989 if(K_DEBUG_LEV > 0)
00990 PrintAllInfo();
00991
00992 fCurOutRawFile->write((Char_t *)(data),(UInt_t)(data[0]));
00993
00994 if(0){
00995 cout << endl << endl << "----------------------------------" << endl;
00996 for(Int_t i=0; i< (Int_t)(data[0]) ; i++ ){
00997 cout << "data[" << i << "]: " << data[i] << endl;
00998 }
00999 cout << "----------------------------------" << endl << endl << endl;
01000 }
01001
01002 }
01003
01004
01005 void TFndSCCMan::UpdateCurrentOutRawFile(){
01006
01007 OpenOutRawFile(kFALSE);
01008 WriteOutRawFile(&fGlobalEvent[0]);
01009 CloseOutRawFile();
01010 }
01011
01012
01013
01014 Int_t TFndSCCMan::OpenRawFile(const TString &fpath){
01015
01016
01017
01018
01019 if(K_DEBUG_LEV > 0) Printf("TFndSCCMan::OpenRawFile ===> opening file \"%s\"",fpath.Data());
01020
01021 fRawFile = fopen(fpath.Data(),"rb");
01022 if(fRawFile == NULL){
01023 if(K_DEBUG_LEV > 0) Printf("TFndSCCMan::OpenRawFile ===> opening file \"%s\" (trying zip)",fpath.Data());
01024 fRawFile = popen(fpath.Data(),"rb");
01025 if(fRawFile == NULL){
01026 if(K_DEBUG_LEV > 0) Printf("TFndSCCMan::OpenRawFile ===> file \"%s\" not found",fpath.Data());
01027 return -1;
01028 }
01029 }
01030
01031 return 0;
01032 }
01033
01034
01035 UInt_t *TFndSCCMan::GetNextRawEvent(Int_t &exit_val){
01036
01037
01038
01039
01040 UInt_t evlen_bytes = 0;
01041 if(! fRawFile){
01042 Warning("GetNextRawEvent","Could not get event: raw-file not defined.");
01043 return 0;
01044 }
01045
01046 if(fread(&evlen_bytes, sizeof(UInt_t), 1 , fRawFile)!=1) {
01047 if(K_DEBUG_LEV > 0) Info("GetNextRawEventFromFile", "End of Raw-file reached");
01048 exit_val = -1;
01049 return 0;
01050 }
01051
01052 if(K_DEBUG_LEV > 0) cout << "TFndSCCMan::GetNextRawEvent ===> evlen (bytes): " << evlen_bytes << endl;
01053
01054 Float_t div_by_four = ((Float_t)evlen_bytes) / 4.;
01055 Float_t rem_by_four = div_by_four - TMath::Floor(div_by_four);
01056 if(rem_by_four !=0 ){
01057 Error("GetNextRawEvent","evlen_bytes not multiple of four (%u)",evlen_bytes);
01058 exit_val = -1;
01059 return 0;
01060 }
01061
01062 UInt_t evlen = (UInt_t) ( ((Float_t)(evlen_bytes)) / 4.);
01063
01064
01065 bzero(fGlobalEvent,sizeof(fGlobalEvent));
01066 fGlobalEvent[0] = (UInt_t) evlen_bytes;
01067 Int_t read_length = 1 + fread(fGlobalEvent+1, sizeof(UInt_t), evlen-1, fRawFile);
01068 if(read_length != evlen){
01069 Error("GetNextRawEventFromFile","wrong length from freed (%d ; %d)",read_length,evlen);
01070 exit_val = -1;
01071 return 0;
01072 }
01073
01074 GlobalEventToGlobalHeader();
01075 GlobalEventToDetEvents();
01076
01077 exit_val = 0;
01078 return fGlobalEvent;
01079 }
01080
01081
01082 Int_t TFndSCCMan::GlobalEventToGlobalHeader(){
01083
01084
01085
01086
01087 UInt_t HdrEntries = (UInt_t)((Float_t)(K_SCC_HDR_SIZE) / (Float_t)(4.));
01088
01089 for(UInt_t id=0;id<HdrEntries;id++){
01090 fGlobalHdr[id] = fGlobalEvent[id];
01091 }
01092
01093 if( fGlobalHdr[HdrTot_Magic] != 0xBABA ){
01094 Error("GlobalEventToGlobalHeader","Wrong magic number in global header: \"%X\"",fGlobalHdr[HdrTot_Magic]);
01095 bzero(fGlobalHdr,HdrEntries);
01096 return -1;
01097 }
01098
01099 return 0;
01100 }
01101
01102
01103 Int_t TFndSCCMan::DetEventToMsgPart(const Int_t &slow_det){
01104
01105
01106
01107
01108
01109 fMsgPart[slow_det] = "OK";
01110 }
01111
01112
01113 Int_t TFndSCCMan::GlobalEventToDetEvents(){
01114
01115
01116
01117
01118 UInt_t t_siz = fGlobalEvent[HdrTot_EvLen];
01119
01120
01121 UInt_t tofi_off = (fGlobalEvent[HdrTot_TofiOff] !=0) ? (UInt_t)( (Float_t)( fGlobalEvent[HdrTot_TofiOff] -1 ) / 4) : 0;
01122 UInt_t tofi_size = (fGlobalEvent[tofi_off] !=0 && fGlobalEvent[tofi_off] < t_siz) ? (UInt_t)( (Float_t)( fGlobalEvent[tofi_off] ) / 4) : 0;
01123
01124 UInt_t tofo_off = (fGlobalEvent[HdrTot_TofoOff] !=0) ? (UInt_t)( (Float_t)( fGlobalEvent[HdrTot_TofoOff] -1 ) / 4) : 0;
01125 UInt_t tofo_size = (fGlobalEvent[tofo_off] !=0 && fGlobalEvent[tofo_off] < t_siz) ? (UInt_t)( (Float_t)( fGlobalEvent[tofo_off] ) / 4) : 0;
01126
01127 UInt_t lmd_off = (fGlobalEvent[HdrTot_LmdOff] !=0) ? (UInt_t)( (Float_t)( fGlobalEvent[HdrTot_LmdOff] -1 ) / 4) : 0;
01128 UInt_t lmd_size = (fGlobalEvent[lmd_off] !=0 && fGlobalEvent[lmd_off] < t_siz) ? (UInt_t)( (Float_t)( fGlobalEvent[lmd_off] ) / 4) : 0;
01129
01130 UInt_t stb_off = (fGlobalEvent[HdrTot_StbOff] !=0) ? (UInt_t)( (Float_t)( fGlobalEvent[HdrTot_StbOff] -1 ) / 4) : 0;
01131 UInt_t stb_size = (fGlobalEvent[stb_off] !=0 && fGlobalEvent[stb_off] < t_siz) ? (UInt_t)( (Float_t)( fGlobalEvent[stb_off] ) / 4) : 0;
01132
01133 UInt_t gas_off = (fGlobalEvent[HdrTot_GasOff] !=0) ? (UInt_t)( (Float_t)( fGlobalEvent[HdrTot_GasOff] -1 ) / 4) : 0;
01134 UInt_t gas_size = (fGlobalEvent[gas_off] !=0 && fGlobalEvent[gas_off] < t_siz) ? (UInt_t)( (Float_t)( fGlobalEvent[gas_off] ) / 4) : 0;
01135
01136 UInt_t sil_off = (fGlobalEvent[HdrTot_SilOff] !=0) ? (UInt_t)( (Float_t)( fGlobalEvent[HdrTot_SilOff] -1 ) / 4) : 0;
01137 UInt_t sil_size = (fGlobalEvent[sil_off] !=0 && fGlobalEvent[sil_off] < t_siz) ? (UInt_t)( (Float_t)( fGlobalEvent[sil_off] ) / 4) : 0;
01138
01139 UInt_t mag_off = (fGlobalEvent[HdrTot_MagOff] !=0) ? (UInt_t)( (Float_t)( fGlobalEvent[HdrTot_MagOff] -1 ) / 4) : 0;
01140 UInt_t mag_size = (fGlobalEvent[mag_off] !=0 && fGlobalEvent[mag_off] < t_siz) ? (UInt_t)( (Float_t)( fGlobalEvent[mag_off] ) / 4) : 0;
01141
01142
01143 if(tofi_off != 0 && tofi_size != 0){
01144 for(UInt_t i=tofi_off; i< tofi_off + tofi_size; i++) fSingleRawData[TOFI][i-tofi_off] = fGlobalEvent[i];
01145 fDataReady[TOFI] = kTRUE;
01146 }
01147 else{
01148 fDataReady[TOFI] = kFALSE;
01149 if(K_DEBUG_LEV> 0) Warning("GlobalEventToDetEvents","can not grab single raw-data for \"%s\" (offset: %u)",fSlowDetNames[TOFI].Data(),tofi_off);
01150 }
01151
01152
01153 if(tofo_off != 0 && tofo_size != 0){
01154 for(UInt_t i=tofo_off; i< tofo_off + tofo_size; i++) fSingleRawData[TOFO][i-tofo_off] = fGlobalEvent[i];
01155 fDataReady[TOFO] = kTRUE;
01156 }
01157 else{
01158 fDataReady[TOFO] = kFALSE;
01159 if(K_DEBUG_LEV> 0) Warning("GlobalEventToDetEvents","can not grab single raw-data for \"%s\" (offset: %u)",fSlowDetNames[TOFO].Data(),tofo_off);
01160 }
01161
01162
01163 if(lmd_off != 0 && lmd_size != 0){
01164 for(UInt_t i=lmd_off; i< lmd_off + lmd_size; i++) fSingleRawData[LMD][i-lmd_off] = fGlobalEvent[i];
01165 fDataReady[LMD] = kTRUE;
01166 }
01167 else{
01168 fDataReady[LMD] = kFALSE;
01169 if(K_DEBUG_LEV> 0) Warning("GlobalEventToDetEvents","can not grab single raw-data for \"%s\" (offset: %u)",fSlowDetNames[LMD].Data(),lmd_off);
01170 }
01171
01172
01173 if(stb_off != 0 && stb_size != 0){
01174 for(UInt_t i=stb_off; i< stb_off + stb_size; i++) fSingleRawData[STB][i-stb_off] = fGlobalEvent[i];
01175 fDataReady[STB] = kTRUE;
01176 }
01177 else{
01178 fDataReady[STB] = kFALSE;
01179 if(K_DEBUG_LEV> 0) Warning("GlobalEventToDetEvents","can not grab single raw-data for \"%s\" (offset: %u)",fSlowDetNames[STB].Data(),stb_off);
01180 }
01181
01182
01183 if(gas_off != 0 && gas_size != 0){
01184 for(UInt_t i=gas_off; i< gas_off + gas_size; i++) fSingleRawData[GAS][i-gas_off] = fGlobalEvent[i];
01185 fDataReady[GAS] = kTRUE;
01186 }
01187 else{
01188 fDataReady[GAS] = kFALSE;
01189 if(K_DEBUG_LEV> 0) Warning("GlobalEventToDetEvents","can not grab single raw-data for \"%s\" (offset: %u)",fSlowDetNames[GAS].Data(),gas_off);
01190 }
01191
01192
01193 if(sil_off != 0 && sil_size != 0){
01194 for(UInt_t i=sil_off; i< sil_off + sil_size; i++) fSingleRawData[SIL][i-sil_off] = fGlobalEvent[i];
01195 fDataReady[SIL] = kTRUE;
01196 }
01197 else{
01198 fDataReady[SIL] = kFALSE;
01199 if(K_DEBUG_LEV> 0) Warning("GlobalEventToDetEvents","can not grab single raw-data for \"%s\" (offset: %u)",fSlowDetNames[SIL].Data(),sil_off);
01200 }
01201
01202
01203 if(mag_off != 0 && mag_size != 0){
01204 for(UInt_t i=mag_off; i< mag_off + mag_size; i++) fSingleRawData[MAG][i-mag_off] = fGlobalEvent[i];
01205 fDataReady[MAG] = kTRUE;
01206 }
01207 else{
01208
01209 fDataReady[MAG] = kFALSE;
01210
01211 }
01212
01213
01214 for(Int_t slow_det = (Int_t)TOFI ; slow_det <= (Int_t)MAG ; slow_det++){
01215 UInt_t offm = ( fSingleRawData[slow_det][HdrDet_miscoff] ) ? (UInt_t) ( (Float_t)(fSingleRawData[slow_det][HdrDet_miscoff]) / 4.) : 0;
01216 UInt_t endm = ( fSingleRawData[slow_det][HdrDet_evlen] ) ? (UInt_t) ( (Float_t)(fSingleRawData[slow_det][HdrDet_evlen]) / 4.) : 0;
01217 if(offm && endm) for(UInt_t m_i=offm; m_i < endm; m_i++)
01218 fMiscArray[slow_det][m_i - offm] = fSingleRawData[slow_det][m_i];
01219 }
01220
01221
01222 Int_t res_msg = 0;
01223 for(Int_t slow_id=0;slow_id<K_N_SLOWS;slow_id++){
01224 res_msg += DetEventToMsgPart(slow_id);
01225 }
01226
01227 if(res_msg) return -1;
01228 return 0;
01229 }
01230
01231
01232 Int_t TFndSCCMan::CloseRawFile(){
01233
01234
01235
01236
01237 if(!fRawFile){
01238 if(K_DEBUG_LEV> 0) Warning("CloseRawFile","no file to be closed.");
01239 return -1;
01240 }
01241
01242 fclose(fRawFile);
01243
01244 fRawFile = 0;
01245 return 0;
01246
01247 }