00001
00002
00003
00004
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00019
00020
00021 #include "TFndRun.h"
00022
00023 #include "TFndGtsMon.h"
00024 #include "TFndTofMon.h"
00025 #include "TFndSilMon.h"
00026 #include "TFndLmdMon.h"
00027 #include "TFndStbMon.h"
00028
00029
00030 #if defined(__CINT__)
00031 #include <netinet/in.h>
00032 #endif
00033 #include <sys/ioctl.h>
00034 #include <sys/socket.h>
00035
00036
00037 #include "msql.h"
00038
00039 #include "Bytes.h"
00040
00041 TFndRun *fndrun = 0;
00042
00043 ClassImp(TFndRun)
00044
00045 TFndHdt *TFndRun::fgFinHdt = 0;
00046
00047 TFndRun::TFndRun(const TString &name, const TString &title):
00048 TNamed(name,title)
00049 {
00050
00051
00052 if(fndrun){
00053 Error("TFndRun", "cannot create 2 objects of TFndRun!");
00054 TerminateFroot();
00055 }
00056
00057 fRunConfiguration = 0;
00058 fFeeMap = 0;
00059
00060 fGenInfo = 0;
00061
00062 fGtsMonFrame = 0;
00063 fTofMonFrame = 0;
00064 fSilMonFrame = 0;
00065 fLmdMonFrame = 0;
00066 fStbMonFrame = 0;
00067
00068 fGtsMon = 0;
00069 fTofMon = 0;
00070 fIsmMon = 0;
00071 fOsmMon = 0;
00072 fLmdMon = 0;
00073 fStbMon = 0;
00074
00075 fProcessRec = 0;
00076
00077
00078 fFinRdt = 0;
00079 if(name.IsNull()) SetName(name);
00080 if(title.IsNull()) SetTitle(title);
00081
00082 Init();
00083 }
00084
00085
00086 TFndRun::~TFndRun(){
00087
00088 if (fndrun != this) fndrun = this;
00089
00090 Info("~TFndRun","Cleaning memory in TFndRun...");
00091 delete fRunConfiguration;
00092 delete fFeeMap;
00093 delete fGenInfo;
00094
00095
00096 Info("~TFndRun","Deleting monitor producers");
00097 Info("~TFndRun"," (GTS)");
00098 delete fGtsMon;
00099 Info("~TFndRun"," (TOF)");
00100 delete fTofMon;
00101 Info("~TFndRun"," (ISM)");
00102 delete fIsmMon;
00103 Info("~TFndRun"," (OSM)");
00104 delete fOsmMon;
00105 Info("~TFndRun"," (LMD)");
00106 delete fLmdMon;
00107 Info("~TFndRun"," (STB)");
00108 delete fStbMon;
00109
00110 if(fOnlineFlag == ONL_OFF){
00111 Info("~TFndRun","Deleting monitor consumers");
00112 delete fGtsMonFrame;
00113 delete fTofMonFrame;
00114 delete fSilMonFrame;
00115 delete fLmdMonFrame;
00116 delete fStbMonFrame;
00117 }
00118
00119 Info("~TFndRun","Deleting hits buffers");
00120 delete fFinRdt;
00121 delete fFinHdt;
00122
00124 fndrun = 0;
00125
00127
00128 Info("~TFndRun","Memory allocated for TFndRun cleaned.");
00129
00130 }
00131
00132
00133 Int_t TFndRun::Init(){
00134
00135
00136
00137 fndrun = this;
00138 fRunConfiguration = new TFndRunConfiguration();
00139
00140 fIsNewRun = kFALSE;
00141 SetDBHost("$MSQL_DB_HOST");
00142 SetDataPath("$RDT");
00143 fMaxNofEvents = std::numeric_limits<Int_t>::max();
00144
00145 fFeeMap = new TFndFeeMap();
00146 fRunTypeID == -1;
00147 fNumOfProcessedRuns = 0;
00148 fNumOfProcessedEvents = 0;
00149
00150 fGenInfo = new TFndGenInfo();
00151
00152 fSockLength = FIN_RAW_DATA_SOCK_LEN;
00153 request_counter = 0;
00154 fUdpSd = -1;
00155
00156 if (!fgFinHdt) fgFinHdt = new TFndHdt();
00157 fFinHdt = fgFinHdt;
00158
00159 fFinRdt = new TFndRdt();
00160
00161 fCurLogFile = 0;
00162 for(Int_t i= (Int_t)D_GTS; i<= (Int_t)D_STB; i++) fMustFillMonitor[i] = kTRUE;
00163
00164 }
00165
00166
00167 Int_t TFndRun::InitNewRun(TString runtype,const Int_t &runnum,const Bool_t &UsedByPrean){
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 fRunNumber = runnum;
00186 fGebIp = FROOT::Ip2Int(gSystem->Getenv("FND_GEBIP"));
00187
00188 if (fProcessing) {
00189 Warning("InitNewRun", "Nothing to do: already processing");
00190 return 1;
00191 }
00192
00193 if(SetRunType(runtype) == -1) return -3;
00194
00195 if(!UsedByPrean){
00196 Info("InitNewRun", "New process starting");
00197
00198
00199 if (runnum == -1) {
00200 Info("InitNewRun", "Initializing new run in online mode");
00201 fOnlineFlag = ONL_ALL;
00202 fRunType = "ONLM";
00203 RecreateSocketToDAQ();
00204 }
00205 else {
00206 fOnlineFlag = ONL_OFF;
00207
00208 fRunFilename = BuildRunName(fRunType,fRunNumber);
00209 Info("InitNewRun", "Initializing new run in offline mode (run \"%s\")",fRunFilename.Data());
00210 Long_t raw_file_size = OpenRawFile(fDataPath, fRunFilename);
00211 if (raw_file_size == -1) {
00212 if(fDebug) Error("InitNewRun", "raw file not found");
00213 return -1;
00214 }
00215 else if (raw_file_size < k_MinRawFileSize) {
00216 Error("InitNewRun", "raw file too small (%ld)", raw_file_size);
00217 return -2;
00218 }
00219 }
00220 }
00221 else{
00222 if (runnum == -1) {
00223 Info("InitNewRun", "Initializing new run in online mode");
00224 fOnlineFlag = ONL_ALL;
00225 fRunType = "ONLM";
00226 }
00227 else {
00228 fOnlineFlag = ONL_OFF;
00229
00230 fRunFilename = BuildRunName(fRunType,fRunNumber);
00231 Info("InitNewRun", "Initializing new run in offline mode (run \"%s\")",fRunFilename.Data());
00232 Long_t raw_file_size = OpenRawFile(fDataPath, fRunFilename,kTRUE);
00233 if (raw_file_size == -1) {
00234 if(fDebug) Error("InitNewRun", "raw file not found");
00235 return -1;
00236 }
00237 else if (raw_file_size < k_MinRawFileSize) {
00238 Error("InitNewRun", "raw file too small (%ld)", raw_file_size);
00239 return -2;
00240 }
00241 }
00242 }
00243
00244
00245 if(fOnlineFlag < (Int_t)(ONL_OFF) || fOnlineFlag > (Int_t)(ONL_ALL)){
00246 Error("GetNextRawEvent","You have just found a BUG! (fOnlineFlag = %d)",fOnlineFlag);
00247 TerminateFroot(0,"BUG detected: Requested online flag value not available");
00248 }
00249
00250 fCurRunType = "";
00251 fCurRunNum = 0;
00252 fIsNewRun = kTRUE;
00253 fProcessing = kTRUE;
00254 fNumOfProcessedRuns++;
00255 return 0;
00256 }
00257
00258
00259 Int_t TFndRun::FinishRun(Bool_t last,const Bool_t &UsedByPrean){
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 if(!fProcessing){
00270 if(fDebug) Warning("FinishRun", "Nothing to do: no process running");
00271 return 1;
00272 }
00273
00274 if(fOnlineFlag){
00275 Info("FinishRun", "Terminating online process");
00276 ShutdownSocketToDAQ();
00277 }
00278 else{
00279 Info("FinishRun", "Terminating process for run \"%s\" number %d", fRunType.Data(), fRunNumber);
00280 if(fRawFile) fclose(fRawFile);
00281 }
00282
00283 fRunNumber = 0;
00284
00285 fProcessing = kFALSE;
00286
00287 if(!UsedByPrean){
00288 if(fDebug) Info("FinishRun", "performing end-of-run operations.");
00289 WriteMonitors(kTRUE);
00290 }
00291
00292 if (!last){
00293 Info("FinishRun", "Current run completed: ready to start a new run.");
00294 }
00295 else{
00296 Info("FinishRun", "Process terminated: performing end-of-process operations.");
00297 fNumOfProcessedRuns = 0;
00298 fNumOfProcessedEvents = 0;
00299 CleanEvent();
00300 }
00301
00302 return 0;
00303 }
00304
00305
00306 Int_t TFndRun::SetRunType(TString runtype){
00307
00308
00309
00310
00311
00312 fRunTypeID = -1;
00313 for(Int_t i=(Int_t)(RT_FINU);i<=(Int_t)(RT_ONLM);i++){
00314 if((Int_t)(runtype.CompareTo(RunType_Name(i))) == 0){ fRunTypeID = i; break; }
00315 }
00316 if(fRunTypeID == -1){
00317 Warning("SetRunType","runtype \"%s\" not accepted",runtype.Data());
00318 fRunType = "";
00319 return -1;
00320 }
00321 fRunType = runtype;
00322
00323 return 0;
00324 }
00325
00326 void TFndRun::SetRunType(Int_t runtype){
00327
00328 if(runtype>(Int_t)(RT_ONLM)) Warning("SetRunType","runtype %d not accepted",runtype);
00329 fRunTypeID = runtype;
00330 fRunType = RunType_Name(runtype);
00331 }
00332
00333
00334 void TFndRun::ClearEvent(){
00335
00336
00337 if(fDebug){
00338 cout << "TFndRun::ClearEvent Info: Clearing event " << endl;
00339 cout << "TFndRun::ClearEvent Info: rdt: " << fFinRdt << endl;
00340 }
00341 fFinRdt->Reset();
00342 fFinHdt->Reset();
00343
00344
00345 bzero(fRawEvent,sizeof(fRawEvent));
00346
00347
00348 if(fDebug) cout << "TFndRun::ClearEvent Info: Event cleared" << endl;
00349 }
00350
00351
00352 void TFndRun::CleanEvent(){
00353
00354
00355
00356
00357 if(fDebug){
00358 cout << "TFndRun::CleanEvent Info: Cleaning event " << endl;
00359 cout << "TFndRun::CleanEvent Info: rdt: " << fFinRdt << endl;
00360 }
00361 delete fFinRdt; fFinRdt = NULL;
00362 delete fFinHdt; fFinHdt = NULL;
00363
00364
00365
00366 if(fDebug) cout << "TFndRun::CleanEvent Info: Event cleaned" << endl;
00367 }
00368
00369
00370 Int_t TFndRun::CheckMsqlConnection(){
00371
00372
00373 if(fDebug) Info("CheckMsqlConnection", " Checking connection to msql: host \"%s\"", ExpandPathName(fDBHost).Data());
00374
00375 Int_t ntry = 0;
00376 Int_t nmaxtry = 30;
00377 Int_t sd = -1;
00378
00379 while(1){
00380 sd = msqlConnect((Char_t *)fDBHost.Data());
00381 if(sd != -1){
00382 gSystem->Sleep(50);
00383 msqlClose(sd);
00384 gSystem->Sleep(50);
00385 return sd;
00386 }
00387 if(ntry){
00388 Info("CheckMsqlConnection", "No connection to DB on \"%s\": retrying (%d).", fDBHost.Data(), ntry);
00389 Warning("CheckMsqlConnection", "msql connection not available: retrying...");
00390 }
00391 ntry++;
00392 if(ntry>=nmaxtry){
00393 Warning("CheckMsqlConnection", "msql connection Maximum number of attempts reached");
00394 return sd;
00395 }
00396 gSystem->ProcessEvents();
00397 gSystem->Sleep(1000);
00398 }
00399
00400 if(fDebug) Info("CheckMsqlConnection", " Socket to msql opened with identifier %d on \"%s\"", sd, fDBHost.Data());
00401 msqlClose(sd);
00402
00403 return sd;
00404 }
00405
00406
00407 Int_t TFndRun::ConnectToDB(){
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 if(!fFeeMap){
00419 Error("ConnectToDB","Pointer to Database Interface not defined");
00420 return -2;
00421 }
00422 if(fFeeMap->IsConnected()){
00423 Error("ConnectToDB","Already connected to DB");
00424 return -3;
00425 }
00426 if (CheckMsqlConnection() < 0) {
00427 Error("InitNewRun", "No connection to database");
00428 return -4;
00429 }
00430
00431 Info("ConnectToDB", "Connecting to database (DB-host: \"%s\")",fDBHost.Data());
00432 Int_t sd = fFeeMap->ConnectToHost(fDBHost);
00433 if(sd == -1) Warning("ConnectToDB","No connection to database (DB internal error)");
00434 return sd;
00435 }
00436
00437
00438 Int_t TFndRun::DisconnectFromDB(){
00439
00440
00441
00442
00443
00444
00445
00446 if(!fFeeMap){
00447 Warning("DisconnectFromDB","Pointer to Database Interface not found");
00448 return -1;
00449 }
00450
00451 fFeeMap->DisconnectFromHost();
00452 return 0;
00453 }
00454
00455
00456 Int_t TFndRun::FetchDBinfo(Int_t source,Bool_t verbose){
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485 if(!fFeeMap){
00486 Error("FetchDBinfo","Pointer to Database Interface not defined");
00487 return -2;
00488 }
00489 if(!fFeeMap->IsConnected()){
00490 Error("FetchDBinfo","Not connected to DB");
00491 return -3;
00492 }
00493
00494 Info("FetchDBinfo","Fetching info from DB host");
00495 Int_t ierr = fFeeMap->FetchRunInfo(verbose);
00496 if(fDebug) Info("FetchDBinfo","ierr = %d",ierr);
00497
00498
00499 if(!fFeeMap){
00500 Error("FetchDBinfo","Why not connected yet?");
00501 TerminateFroot();
00502 }
00503
00504
00505
00506
00507
00508
00509
00510 if(fDebug ==0) Info("FetchDBinfo","done");
00511 return 0;
00512
00513 }
00514
00515
00516 Int_t TFndRun::CreateSocketToDAQ(E_Fnd_DaqRaw_Udp_Ports det_port){
00517
00518 fUdpSd= socket(AF_INET, SOCK_DGRAM, 0);
00519
00520 bzero((char*)&fLocSockAddr,sizeof(fLocSockAddr));
00521 fLocSockAddr.sin_family = AF_INET;
00522 fLocSockAddr.sin_addr.s_addr = INADDR_ANY;
00523 fLocSockAddr.sin_port = 0;
00524
00525 Info("CreateSocketToDAQ","new socket created with socket descriptor %d; now binding...",fUdpSd);
00526 int bind_err = bind(fUdpSd, (struct sockaddr *) &fLocSockAddr, sizeof(fLocSockAddr));
00527 if(bind_err == -1) TerminateFroot(0,"TFndRun::CreateSocketToDAQ ---> Bind operation failed!");
00528 Info("CreateSocketToDAQ","...done (bind_err = %d)",bind_err);
00529
00531
00532
00533
00535
00536 bzero((char*)&fShrSockAddr,sizeof(fShrSockAddr));
00537 fShrSockAddr.sin_family = AF_INET;
00538 fShrSockAddr.sin_addr.s_addr = htonl(fGebIp);
00539 fShrSockAddr.sin_port = htons(det_port);
00540 fShrSockAddr_size = sizeof(fShrSockAddr);;
00541
00542
00543 int sock_err = setsockopt(fUdpSd,SOL_SOCKET,SO_RCVBUF,&fSockLength,sizeof(fSockLength));
00544 if(sock_err==-1) TerminateFroot(0,"TFndRun::CreateSocketToDAQ ---> setsockopt failed failed!");
00545 return 0;
00546
00547 }
00548
00549
00550 Int_t TFndRun::CheckSocketToDAQ(){
00551
00552
00553 Int_t check_result=0;
00554 Int_t nn = 0;
00555 while(nn < 50){
00556 nn++;
00557
00558 ioctl(fUdpSd,FIONREAD,(Int_t *)&check_result);
00559 if(check_result != 0) break;
00560 usleep(3000);
00561
00562 if(nn > 2) usleep(10000);
00563 if(nn > 3) usleep(40000);
00564 if(nn > 5) usleep(500000);
00565 if(nn > 7) usleep(500000);
00566 if(nn > 9) usleep(100000);
00567
00568
00569
00570
00571
00572 }
00573 return check_result;
00574 }
00575
00576
00577 Int_t TFndRun::ShutdownSocketToDAQ(){
00578 return shutdown(fUdpSd,SHUT_RDWR);
00579 }
00580
00581
00582 UInt_t *TFndRun::GetNextRawEventFromSocket(int &exit_val){
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596 bzero(fRawEvent,sizeof(fRawEvent));
00597
00598
00599
00600 Int_t request_buf=1;
00601
00602 Int_t n_max_requests = 50;
00603
00604 int n_trials = 0;
00605 while(1){
00606 n_trials++; if(n_trials> n_max_requests){
00607 exit_val = -2;
00608 return 0;
00609 }
00610 request_counter++;
00611
00612
00613 int send_err = sendto(fUdpSd,(char*)&request_buf ,sizeof(Int_t) ,0 ,(struct sockaddr *) &fShrSockAddr,fShrSockAddr_size);
00614 if(send_err==-1){
00615 Error("GetNextRawEventFromSocket","send-request operation failed!");
00616 sleep(1);
00617 continue;
00618 }
00619
00620 int check_result=CheckSocketToDAQ();
00621
00622 if(check_result) break;
00623 else Info("GetNextRawEventFromSocket","...server not ready: trying again");
00624 }
00625
00626 Int_t nread = recvfrom(fUdpSd ,(char*)fRawEvent ,sizeof(fRawEvent) ,0 ,(struct sockaddr*)&fShrSockAddr , (socklen_t *) &fShrSockAddr_size);
00627
00628 if(fDebug > 0) printf("UDP fRawEvent received from server: nread = %d; event = \"%x\".\n",nread,fRawEvent);
00629
00631 if(nread == -1){
00632 Error("GetNextRawEventFromSocket","nread = -1");
00633 exit_val = -2;
00634 return 0;
00635 }
00636 else if(nread == 0){
00637 Error("GetNextRawEventFromSocket","connection closed by GEB");
00638 exit_val = -2;
00639 return 0;
00640 }
00641
00642 Int_t u_length = nread / sizeof(fRawEvent[0]);
00643 if( u_length >= sizeof(fRawEvent) ){
00644 Warning("GetNextRawEventFromSocket","received buffer size greater than local buf-size");
00645 exit_val = -2;
00646 return 0;
00647 }
00648 if( u_length != fRawEvent[0] ){
00649 Warning("GetNextRawEventFromSocket","received buffer size different from event size");
00650 exit_val = -2;
00651 return 0;
00652 }
00653
00654 if(fDebug > 0) if(u_length == fRawEvent[0]) Info("GetNextRawEventFromSocket","received event OK");
00655
00656
00657 if(fDebug > 1){
00658 for(unsigned int j=0; j<5; j++){
00659 if(fRawEvent[j] != 0) cout << "fRawEvent[" << j << "] = " << fRawEvent[j] << endl;
00660 }
00661 }
00662 else if(fDebug > 2){
00663 unsigned int len_test = fRawEvent[0];
00664 for(unsigned int j=0; j<len_test; j++){
00665 if(fRawEvent[j] != 0) cout << "fRawEvent[" << j << "] = " << fRawEvent[j] << endl;
00666 }
00667 }
00669
00670 CheckRawSwapping();
00671
00672 exit_val = 0;
00673 return fRawEvent;
00674 }
00675
00676
00677 UInt_t *TFndRun::GetNextRawEventFromFile(int &exit_val){
00678
00679
00680 UInt_t evlen;
00681 if(fDebug > 1) Info("GetNextRawEventFromFile", "Get event from disk");
00682 if(fread(&evlen, sizeof(UInt_t), 1 , fRawFile)!=1) {
00683 Info("GetNextRawEventFromFile", "End of Run reached");
00684 exit_val = 2;
00685 return 0;
00686 }
00687 bzero(fRawEvent,sizeof(fRawEvent));
00688 fRawEvent[0] = (UInt_t) evlen;
00689 Int_t read_length = 1 + fread(fRawEvent+1, sizeof(UInt_t), evlen-1, fRawFile);
00690 if(read_length != evlen) Error("GetNextRawEventFromFile","wrong length from freed");
00691
00692 CheckRawSwapping();
00693 return fRawEvent;
00694 }
00695
00696
00697 UInt_t *TFndRun::GetNextRawEvent(Int_t &exit_value){
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710 exit_value = 0;
00711
00712 if(fNumOfProcessedEvents >= fMaxNofEvents) {
00713 Info("GetNextRawEvent", "Requested number of events reached: process completed");
00714 exit_value = 1;
00715 return 0;
00716 }
00717 if(fDebug) Info("GetNextRawEvent", "fOnlineFlag: %d", fOnlineFlag);
00718
00720 if(fOnlineFlag == (Int_t)(ONL_OFF)){
00721 GetNextRawEventFromFile(exit_value);
00722 }
00723 else{
00724 GetNextRawEventFromSocket(exit_value);
00725 if(exit_value == -2){
00726 RecreateSocketToDAQ();
00727 return 0;
00728 }
00729 }
00730
00731
00732
00733 fNumOfProcessedEvents++;
00734 return fRawEvent;
00735
00736 }
00737
00738
00739 void TFndRun::CheckRawSwapping(){
00740
00741
00742 UInt_t evlen = fRawEvent[0];
00743
00744 Int_t unswpd_len = 0;
00745 Int_t swpd_len = 0;
00746 Int_t swap = 0;
00747 unswpd_len = evlen;
00748 swpd_len = host2net(evlen);
00749 swap = 0;
00750
00751
00752 if(unswpd_len <= 0) {
00753 evlen = swpd_len;
00754 swap = 1;
00755 }
00756 else if(swpd_len <= 0) {
00757 evlen = unswpd_len;
00758 swap = 0;
00759 }
00760 else {
00761 if(swpd_len < unswpd_len) {
00762 evlen = swpd_len;
00763 swap = 1;
00764 }
00765 else {
00766 evlen = unswpd_len;
00767 swap = 0;
00768 }
00769 }
00770
00771 if(swap == 1) for (UInt_t j=1; j<evlen; j++) fRawEvent[j] = host2net(fRawEvent[j]);
00772 }
00773
00774
00775 Bool_t TFndRun::IsNewRun(){
00776
00777 if( fCurRunType.IsNull() ||
00778 fCurRunType.CompareTo(DecodeRunType(fRawEvent[3]) )!=0 ||
00779 fRawEvent[4] != fCurRunNum)
00780 {
00781 fIsNewRun = kTRUE;
00782 if(fDebug) Info("IsNewRun"," New run set to true (fRunType: \"%s\"; raw: \"%s\")",fCurRunType.Data(),DecodeRunType(fRawEvent[3]).Data());
00783 if(fDebug > 1){
00784 Info("IsNewRun","fCurRunType.CompareTo(DecodeRunType(fRawEvent[3]) ): %d",fCurRunType.CompareTo(DecodeRunType(fRawEvent[3]) ) );
00785 Info("IsNewRun","fRawEvent[4] = %u ; fCurRunNum = %u", fRawEvent[4],fCurRunNum );
00786 }
00787 }
00788 return fIsNewRun;
00789 }
00790
00791
00792 Int_t TFndRun::ReadRawEvent(Bool_t using_db){
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802 if(fDebug) Info("ReadRawEvent", "entering method: online flag = %d",fOnlineFlag);
00803
00804 if(fOnlineFlag == ONL_ALL) IsNewRun();
00805
00806 if(fRawEvent[13] != 0) Info("ReadRawEvent", "event 0 at event: %d, location: %u", fRawEvent[2], fRawEvent[13]);
00807
00808 if(fDebug && (fOnlineFlag == ONL_ALL || fOnlineFlag == ONL_OFF)) Info("ReadRawEvent", "entering method: Run %u; Event %u", fRawEvent[4],fRawEvent[2]);
00809
00810 if(fIsNewRun == kTRUE) {
00811 Info("ReadRawEvent",
00812 "Performing new run operations (run type: \"%s\"; run number = %u; event number = %u",
00813 DecodeRunType(fRawEvent[3]).Data(),
00814 fRawEvent[4],
00815 fRawEvent[2]);
00816 delete fGenInfo;
00817 fGenInfo = new TFndGenInfo(fRawEvent);
00818 fRunTime = fRawEvent[5];
00819
00820
00821 if(using_db){
00822 Info("ReadRawEvent","Fetching information from DB");
00823 if(!fFeeMap->IsConnected()){
00824 if(ConnectToDB() < 0) return -1;
00825 }
00826 if(FetchDBinfo(K_DB_SOURCE_HOST) != 0) TerminateFroot();
00827 DisconnectFromDB();
00828 fFinRdt->Reload();
00829 }
00830
00831 fIsNewRun = kFALSE;
00832 fCurRunType = DecodeRunType(fRawEvent[3]);
00833 fCurRunNum = fRawEvent[4];
00834 Info("ReadRawEvent","New run operations succesfully performed for run %u",fCurRunNum);
00835 if(fDebug) Info("ReadRawEvent","New run set to false");
00836 if(fOnlineFlag == ONL_ALL){
00837 Info("ReadRawEvent","\n\n\n");
00838 Info("ReadRawEvent"," -----------------------------------");
00839 Info("ReadRawEvent"," ---- NEW RUN started (%s %u)",fCurRunType.Data(),fCurRunNum);
00840 Info("ReadRawEvent"," -----------------------------------\n\n\n");
00841
00842 fFinHdt->Reset();
00843 fFinHdt->SetEventProperties(fRawEvent[4],fRawEvent[2]);
00844 return 0;
00845 }
00846 }
00847
00848 if(!fFinRdt) TerminateFroot(0,"TFndRun::ReadRawEvent ---> fFinRdt not present!");
00849
00850 fFinHdt->Reset();
00851 fFinHdt->SetEventProperties(fRawEvent[4],fRawEvent[2]);
00852
00853 if(fDebug) Info("ReadRawEvent", "mapping RDT to HDT");
00854 if(!using_db) return 0;
00855
00856 if(using_db){
00857
00858 fFinRdt->RdtToHdt(fRawEvent,0);
00859 fFinHdt->EvalHits();
00860 FillMonitors();
00861 }
00862
00863 if(fDebug) Info("ReadRawEvent", "exiting method");
00864 return 0;
00865 }
00866
00867
00868 Int_t TFndRun::GetNextEvent(Bool_t fill_hdt){
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878 if(fDebug) Info("GetNextEvent", "entering method");
00879 Int_t istop = 0;
00880
00881 if(fDebug) cerr << "getting new event; last processed event: " << fNumOfProcessedEvents << endl;
00882
00883
00884 if(fRawEvent && fRawEvent[0]!=0){
00885 Warning("GetNextEvent","You forgot to call TFndRun::ClearEvent! forcing Clear operation (ev: %u).",GetEvNum_Raw());
00886 ClearEvent();
00887 }
00888 GetNextRawEvent(istop);
00889 if(istop) return istop;
00890
00891 if(fDebug) cerr << "event " << GetEvNum_Raw() << " got" << " (fNumOfProcessedEvents: " << fNumOfProcessedEvents << endl;
00892
00893 if(fill_hdt) istop = ReadRawEvent();
00894
00895 if(fDebug) cerr << "event " << fNumOfProcessedEvents << " completed" << endl;
00896 return istop;
00897 }
00898
00899
00900 Int_t TFndRun::ReconstructEvent(){
00901
00902
00903
00904 Info("ReconstructEvent", "Global Event address : %p", fRawEvent);
00905 Info("ReconstructEvent", "Global Event Length : %u", fRawEvent[0]);
00906 Info("ReconstructEvent", "Raw event number (hardware) : %u", fRawEvent[1]);
00907 Info("ReconstructEvent", "Raw event number (software) : %u", fRawEvent[2]);
00908
00909 return 0;
00910 }
00911
00912
00913 void TFndRun::StartMonitors(){
00914
00915
00916
00917 Bool_t at_least_one = kFALSE;
00918 for(Int_t i= (Int_t)D_GTS; i<= (Int_t)D_STB; i++) at_least_one = at_least_one || fMustFillMonitor[i];
00919 if(!at_least_one){
00920 Info("StartMonitors","No monitor will start (histograms)");
00921 return;
00922 }
00923
00924 if( !fRunFilename.IsNull() ){
00925 fMonHisFilename=fRunFilename;
00926 TString def_pth= ExpandPathName("$FND_MON_HISTOS");
00927 if( !def_pth.IsNull() && gSystem->AccessPathName(def_pth)==0 ) fMonHisFilename.Form("%s/%s",def_pth.Data(),fRunFilename.Data());
00928 }
00929
00930 if( fMonHisFilename.IsNull() ) Info("StartMonitors", "Start Monitors in online mode");
00931 else Info("StartMonitors", "Start Monitors with fMonHisFilename: %s", fMonHisFilename.Data());
00932
00933 if(fMustFillMonitor[(Int_t)D_GTS] ){
00934 Info("StartMonitors","Starting GTS");
00935 fGtsMon = new TFndGtsMon(fMonHisFilename.Data());
00936 }
00937
00938 if(fMustFillMonitor[(Int_t)D_TOF] ){
00939 Info("StartMonitors","Starting TOF");
00940 StartTofMonitor();
00941 }
00942
00943 if(fMustFillMonitor[(Int_t)D_SIL] ){
00944 Info("StartMonitors","Starting ISIM");
00945 StartIsmMonitor();
00946
00947 Info("StartMonitors","Starting OSIM");
00948 StartOsmMonitor();
00949 }
00950
00951 if(fMustFillMonitor[(Int_t)D_LMD] ){
00952 Info("StartMonitors","Starting LMD");
00953 StartLmdMonitor();
00954 }
00955
00956 if(fMustFillMonitor[(Int_t)D_STB] ){
00957 Info("StartMonitors","Starting STB");
00958 StartStbMonitor();
00959 }
00960
00961 Info("StartMonitors","all monitors started ");
00962 return;
00963 }
00964
00965 void TFndRun::StartGtsMonitor(){
00966
00967
00968 fGtsMon = new TFndGtsMon(fMonHisFilename.Data());
00969 return;
00970 }
00971
00972
00973 void TFndRun::StartTofMonitor(Int_t adc_bins,Float_t adc_min,Float_t adc_max,Int_t tdc_bins,Float_t tdc_min,Float_t tdc_max){
00974
00975 if(!fRunConfiguration){
00976 Warning("StartTofMonitor","Run Configuration file not loaded: can not start monitor");
00977 return;
00978 }
00979 if(adc_bins > K_MAX_BINS_ADC || adc_bins <= 0) fRunConfiguration->GetAdcRange(D_TOF,adc_bins,adc_min,adc_max);
00980 if(tdc_bins > K_MAX_BINS_TDC || tdc_bins <= 0) fRunConfiguration->GetTdcRange(D_TOF,tdc_bins,tdc_min,tdc_max);
00981
00982 fTofMon = new TFndTofMon(fMonHisFilename.Data(),adc_bins,adc_min,adc_max,tdc_bins,tdc_min,tdc_max);
00983
00984 return;
00985
00986 }
00987
00988
00989 void TFndRun::StartIsmMonitor(){
00990
00991 fIsmMon = new TFndSilMon(fMonHisFilename.Data(),1);
00992 return;
00993 }
00994
00995
00996 void TFndRun::StartOsmMonitor(){
00997
00998 fOsmMon = new TFndSilMon(fMonHisFilename.Data(),2);
00999 return;
01000 }
01001
01002
01003 void TFndRun::StartLmdMonitor(Int_t adc_bins,Float_t adc_min,Float_t adc_max,Int_t tdc_bins,Float_t tdc_min,Float_t tdc_max){
01004
01005 if(!fRunConfiguration){
01006 Warning("StartLmdMonitor","Run Configuration file not loaded: can not start monitor");
01007 return;
01008 }
01009 if(adc_bins > K_MAX_BINS_ADC || adc_bins <= 0) fRunConfiguration->GetAdcRange(D_LMD,adc_bins,adc_min,adc_max);
01010 if(tdc_bins > K_MAX_BINS_TDC || tdc_bins <= 0) fRunConfiguration->GetTdcRange(D_LMD,tdc_bins,tdc_min,tdc_max);
01011 fLmdMon = new TFndLmdMon(fMonHisFilename.Data(),adc_bins,adc_min,adc_max,tdc_bins,tdc_min,tdc_max);
01012 return;
01013 }
01014
01015
01016 void TFndRun::StartStbMonitor(Int_t tdc_bins,Float_t tdc_min,Float_t tdc_max){
01017
01018 if(!fRunConfiguration){
01019 Warning("StartStbMonitor","Run Configuration file not loaded: can not start monitor");
01020 return;
01021 }
01022 if(tdc_bins > K_MAX_BINS_TDC || tdc_bins <= 0) fRunConfiguration->GetTdcRange(D_STB,tdc_bins,tdc_min,tdc_max);
01023
01024 fStbMon = new TFndStbMon(fMonHisFilename.Data(),tdc_bins,tdc_min,tdc_max);
01025 return;
01026 }
01027
01028
01029 void TFndRun::StartGtsMonitor(TString *str){
01030
01031
01032
01033 if(!fGtsMon) {
01034 if(fOnlineFlag == ONL_OFF) {
01035 Info("StartGtsMonitor", "Start Gts monitor in offline mode.");
01036 fGtsMon = new TFndGtsMon(str);
01037 }
01038 else {
01039 Info("StartGtsMonitor", "Start Gts monitor in online mode.");
01040 fGtsMon = new TFndGtsMon();
01041 }
01042 }
01043 return;
01044 }
01045
01046
01047 void TFndRun::StartTofMonitor(TString *str){
01048
01049
01050
01051 if(!fTofMon) {
01052 if(fOnlineFlag == ONL_OFF) {
01053 Info("StartTofMonitor", "Start Tof monitor in offline mode");
01054 fTofMon = new TFndTofMon(str);
01055 }
01056 else {
01057 Info("StartTofMonitor", "Start Tof monitor in online mode");
01058 fTofMon = new TFndTofMon();
01059 }
01060 }
01061 return;
01062 }
01063
01064
01065 void TFndRun::StartIsmMonitor(TString *str){
01066
01067
01068
01069 if(!fIsmMon) {
01070 if(fOnlineFlag == ONL_OFF){
01071 Info("StartIsmMonitor", "Start Ism monitor in offline mode");
01072 fIsmMon = new TFndSilMon(str->Data(),1);
01073 }
01074 else {
01075 Info("StartIsmMonitor", "Start Ism monitor in online mode");
01076 fIsmMon = new TFndSilMon();
01077 }
01078 }
01079 return;
01080 }
01081
01082
01083 void TFndRun::StartOsmMonitor(TString *str){
01084
01085
01086
01087 if(!fOsmMon) {
01088 if(fOnlineFlag == ONL_OFF){
01089 Info("StartOsmMonitor", "Start Osm monitor in offline mode");
01090 fOsmMon = new TFndSilMon(str->Data(),2);
01091 }
01092 else {
01093 Info("StartOsmMonitor", "Start Osm monitor in online mode");
01094 fOsmMon = new TFndSilMon();
01095 }
01096 }
01097 return;
01098 }
01099
01100
01101 void TFndRun::StartLmdMonitor(TString *str){
01102
01103
01104
01105 if(!fLmdMon) {
01106 if(fOnlineFlag == ONL_OFF) {
01107 Info("StartLmdMonitor", "Start Lmd monitor in offline mode");
01108 fLmdMon = new TFndLmdMon(str);
01109 }
01110 else {
01111 Info("StartLmdMonitor", "Start Lmd monitor in online mode");
01112 fLmdMon = new TFndLmdMon();
01113 }
01114 }
01115 return;
01116 }
01117
01118
01119 void TFndRun::StartStbMonitor(TString *str){
01120
01121
01122
01123 if(!fStbMon) {
01124 if(fOnlineFlag == ONL_OFF) {
01125 Info("StartStbMonitor", "Start Stb monitor in offline mode");
01126 fStbMon = new TFndStbMon(str);
01127 }
01128 else {
01129 Info("StartStbMonitor", "Start Stb monitor in online mode");
01130 fStbMon = new TFndStbMon();
01131 }
01132 }
01133 return;
01134 }
01135
01136
01137 Int_t TFndRun::StartMonFrame(Int_t det,TString RootFileName,TString RootFileName2){
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149 Int_t ret_val = 0;
01150
01151 if(GetMonFrame(det)){
01152 DeleteMonFrame(det);
01153 ret_val = 1;
01154 }
01155
01156 switch(det){
01157 case D_GTS:
01158 fGtsMonFrame = new TFndGtsMonFrame(RootFileName);
01159 fGtsMonFrame->Open();
01160 break;
01161 case D_TOF:
01162 fTofMonFrame = new TFndTofMonFrame(RootFileName);
01163 fTofMonFrame->Open();
01164 break;
01165 case D_SIL:
01166 fSilMonFrame = new TFndSilMonFrame(RootFileName,RootFileName2);
01167 fSilMonFrame->Open();
01168 break;
01169 case D_LMD:
01170 fLmdMonFrame = new TFndLmdMonFrame(RootFileName);
01171 fLmdMonFrame->Open();
01172 break;
01173 case D_STB:
01174 fStbMonFrame = new TFndStbMonFrame(RootFileName);
01175 fStbMonFrame->Open();
01176 break;
01177 default:
01178 Error("StartMonFrame","det \"%d\" not accepted",det);
01179 return -1;
01180 }
01181 return ret_val;
01182 }
01183
01184
01185 TFndMonFrame *TFndRun::GetMonFrame(Int_t det){
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195 switch(det){
01196 case D_GTS: return (TFndMonFrame *) fGtsMonFrame;
01197 case D_TOF: return (TFndMonFrame *) fTofMonFrame;
01198 case D_SIL: return (TFndMonFrame *) fSilMonFrame;
01199 case D_LMD: return (TFndMonFrame *) fLmdMonFrame;
01200 case D_STB: return (TFndMonFrame *) fStbMonFrame;
01201 default:
01202 Error("StartMonFrame","det \"%d\" not accepted",det);
01203 return 0;
01204 }
01205 return 0;
01206 }
01207
01208
01209 Int_t TFndRun::DeleteMonFrame(Int_t det){
01210
01211
01212
01213
01214
01215
01216
01217 switch(det){
01218 case D_GTS: delete fGtsMonFrame; fGtsMonFrame = 0; break;
01219 case D_TOF: delete fTofMonFrame; fTofMonFrame = 0; break;
01220 case D_SIL: delete fSilMonFrame; fSilMonFrame = 0; break;
01221 case D_LMD: delete fLmdMonFrame; fLmdMonFrame = 0; break;
01222 case D_STB: delete fStbMonFrame; fStbMonFrame = 0; break;
01223 default:
01224 Error("StartMonFrame","det \"%d\" not accepted",det);
01225 return -1;
01226 }
01227 return 0;
01228 }
01229
01230
01231 void TFndRun::FillMonitors(){
01232
01233
01234 if(fDebug > 0) Info("FillMonitors", "entering method");
01235 if(fGtsMon) fGtsMon->Fill();
01236 if(fTofMon) fTofMon->Fill();
01237 if(fIsmMon) fIsmMon->Fill();
01238 if(fOsmMon) fOsmMon->Fill();
01239 if(fLmdMon) fLmdMon->Fill();
01240 if(fStbMon) fStbMon->Fill();
01241 if(fDebug > 0) Info("FillMonitors", "exiting method");
01242 return;
01243 }
01244
01245
01246 void TFndRun::WriteMonitors(Bool_t close){
01247
01248
01249
01250 Info("WriteMonitors", "Writing Histograms To file (close: %d)",close);
01251 if(fGtsMon) close ? fGtsMon->WriteToFile() : fGtsMon->Update();
01252
01253 if(fTofMon) close ? fTofMon->WriteToFile() : fTofMon->Update();
01254
01255 if(fIsmMon) close ? fIsmMon->WriteToFile() : fIsmMon->Update();
01256
01257 if(fOsmMon) close ? fOsmMon->WriteToFile() : fOsmMon->Update();
01258
01259 if(fLmdMon) close ? fLmdMon->WriteToFile() : fLmdMon->Update();
01260
01261 if(fStbMon) close ? fStbMon->WriteToFile() : fStbMon->Update();
01262 Info("WriteMonitors", "done");
01263 return;
01264 }
01265
01266
01267 void TFndRun::ReadConfigFile(TString ConfFileName){
01268
01269
01270 TString ConfFuncName = "RunConfig()";
01271
01272 if(gSystem->AccessPathName(ConfFileName)){
01273 Warning("ReadConfigFile", "file \"%s\" not found.", ConfFileName.Data());
01274 Warning("ReadConfigFile", "Configuration file \"%s\" not found in current directory: using defaults.", ConfFileName.Data());
01275 ConfFileName = FROOT::ExpandPathName("$FROOTSYS/mcr_called/RunConfig.C");
01276 Info("ReadConfigFile","The default configuration file is \"%s\"",(Char_t *)ConfFileName.Data());
01277 }
01278 if(gSystem->AccessPathName(ConfFileName)){
01279 Error("ReadConfigFile","Default configuration file \"%s\" not found! exiting program.",(Char_t *)ConfFileName.Data());
01280 TerminateFroot();
01281 }
01282
01283 if(fDebug > 1) Info("ReadConfigFile", "Loading Macro %s", ConfFileName.Data());
01284 gROOT->LoadMacro(ConfFileName);
01285 if(fDebug > 1) Info("ReadConfigFile", "Loading configuration method \"%s\"", ConfFileName.Data());
01286 Info("ReadConfigFile", " Loading \"%s\"", (Char_t *) (ConfFileName.Data()));
01287 printf("TFndRun::ReadConfigFile Info: Loading \"%s\"\n", (Char_t *) (ConfFileName.Data()));
01288 gInterpreter->ProcessLine(ConfFuncName);
01289
01290 fDebug = fRunConfiguration->Debug();
01291 fDebugRawEv = fRunConfiguration->DebugRaw();
01292
01293 for(Int_t i= (Int_t)D_GTS; i<= (Int_t)D_STB; i++) fMustFillMonitor[i] = fRunConfiguration->GetMustFillMonitor(i);
01294
01295
01296 return;
01297 }
01298
01299 Long_t TFndRun::OpenRawFile(TString fdir, TString fnam,Bool_t check_only){
01300
01301
01302
01303
01304
01305 Bool_t is_zipped = kFALSE;
01306
01307 TString gzip = TString("gzip -cd ");
01308 TString sufx = TString(".gz");
01309 TString fname = FROOT::ExpandPathName(fdir);
01310 fname+="/";
01311 fname+=fnam;
01312 fname+=".raw";
01313
01314
01315
01316
01317
01318
01319
01320 if(!check_only){
01321
01322 gzip+=fname.Data();
01323 gzip+=sufx.Data();
01324 if(fDebug > 0) cout << "TFndRun::OpenRawFile Info: Opening " << fname.Data() << endl;
01325 fRawFile = fopen(fname.Data(),"rb");
01326 if (fRawFile==NULL) {
01327 if(fDebug) cout << "TFndRun::OpenRawFile Info: Opening zipped file" << endl;
01328 is_zipped = kTRUE;
01329 fRawFile = popen(gzip.Data(),"r");
01330 if (fRawFile==NULL) {
01331 printf("TFndRun::OpenRawFile Warning: file %s does not exist\n", fname.Data());
01332 return -1;
01333 }
01334 }
01335
01336 TString msg_sf = is_zipped ? ".gz" : "";
01337
01338 if(fDebug > 1 && !check_only) printf("TFndRun::OpenRawFile Info: file %s%s opened.\n", fname.Data(), msg_sf.Data());
01339 }
01340
01341
01342
01343
01344 is_zipped = kFALSE;
01345 Long_t id = 0;
01346 Long_t size = 0;
01347 Long_t flags = 0;
01348 Long_t modtime = 0;
01349 Int_t InfoRes = -1;
01350
01351 InfoRes = gSystem->GetPathInfo(fname.Data(),&id,&size,&flags,&modtime);
01352 if(InfoRes){
01353 is_zipped = kTRUE;
01354 fname+=sufx;
01355 InfoRes = gSystem->GetPathInfo(fname.Data(),&id,&size,&flags,&modtime);
01356 }
01357
01358 if(!InfoRes){
01359 if(fDebug > 1)
01360 printf("TFndRun::OpenRawFile Info: file size: %d (zipped: %d)\n", size, is_zipped);
01361 if(fDebug > 2)
01362 printf("TFndRun::OpenRawFile Info: name: \"%s\"; id: %ld; size: %ld; flags: %ld; mod time\n",
01363 fname.Data(), id, size, flags, modtime);
01364
01365 return size;
01366 }
01367 else{
01368 printf("TFndRun::OpenRawFile Info: can not stat file \"%s\"\n",fname.Data());
01369 return -1;
01370 }
01371 }
01372
01374
01375
01376 void TFndRun::OpenLogFile(const TString &fdir, const TString &fnam){
01377
01378 CloseLogFile();
01379
01380 TString path_str;
01381 path_str.Form("%s/%s",ExpandPathName(fdir).Data(),fnam.Data());
01382
01383 fCurLogFile = new ofstream();
01384
01385 fCurLogFile->open(path_str.Data(),ofstream::out | ofstream::app);
01386
01387 }
01388
01389
01390 void TFndRun::CloseLogFile(){
01391
01392 if(!fCurLogFile) return;
01393 fCurLogFile->close();
01394 delete fCurLogFile;
01395 fCurLogFile = 0;
01396 }
01397
01398
01399 void TFndRun::WriteLogFile(const TString &line){
01400
01401 if(!fCurLogFile){
01402
01403 return;
01404 }
01405 *fCurLogFile << line.Data() << endl;
01406 }
01407