• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

onlinemonitor/spadicV10monitor/TSpadicV10Source.cxx (r4864/r3724)

Go to the documentation of this file.
00001 #include "TSpadicV10Source.h"
00002 
00003 
00004 #include <errno.h>
00005 #include <stdlib.h>
00006 #include <limits.h>
00007 
00008 #include "TClass.h"
00009 
00010 #include "TGo4Log.h"
00011 #include "TGo4UserSourceParameter.h"
00012 #include "TGo4MbsEvent.h"
00013 //#include "TGo4MbsSubEvent.h"
00014 #include "TGo4EventErrorException.h"
00015 #include "TGo4EventTimeoutException.h"
00016 #include "TGo4EventEndException.h"
00017 #include "base/commons.h"
00018 
00019 #include "sp605/Message.h"
00020 
00021 
00022 #define SPADICV10SOURCE_EVENTBUF 4096
00023 
00024 // class required only to use methods, which are available in TGo4MbsSource
00025 
00026 TSpadicV10Source::TSpadicV10Source(TGo4UserSourceParameter* par) :
00027    TGo4EventSource(),
00028    fbIsOpen(kFALSE),
00029    fxFile(0),
00030    fArg(),
00031    fBoard(0),
00032    fFullId(0)
00033 {
00034    TGo4Log::Debug(" New SpadicV10Source %s: par: %s", GetName(), par->GetName());
00035 
00036    fArg = par->GetName();
00037 
00038    //cout << " !!! SPADICV1 !!!   NOT IMPLEMENTED !!!
00039    TGo4Log::Info("Create New SpadicV10Source  arg: %s", fArg.Data());
00040 
00041 
00042    Open();
00043 }
00044 
00045 TSpadicV10Source::TSpadicV10Source() :
00046       TGo4EventSource(),
00047       fbIsOpen(kFALSE),
00048       fxFile(0),
00049       fArg(),
00050       fBoard(0),
00051       fFullId(0)
00052 {
00053 }
00054 
00055 
00056 TSpadicV10Source::~TSpadicV10Source()
00057 {
00058    Close();
00059 }
00060 
00061 Bool_t TSpadicV10Source::CheckEventClass(TClass* cl)
00062 {
00063    return cl->InheritsFrom(TGo4MbsEvent::Class());
00064 }
00065 
00066 Bool_t TSpadicV10Source::BuildEvent(TGo4EventElement* dest)
00067 {
00068    TGo4MbsEvent* mbs = (TGo4MbsEvent*) dest;
00069 
00070    //if (fBoard==0) throw TGo4EventErrorException(this);
00071 
00072 //   void* buf(0);
00073 //   unsigned len(0);
00074 
00075 //   if (!fBoard->getNextBuffer(buf,len, 5.))
00076 //      throw TGo4EventTimeoutException(this);
00077 
00079    static uint16_t data[SPADICV10SOURCE_EVENTBUF]; // intermediate buffer for single mbs event
00080    uint16_t* pdata = &data[0];
00081    unsigned len(0);
00082    int8_t spadicid =-1;
00084 
00085    char sbuf[1024], buffer[1024];
00086    while (len < SPADICV10SOURCE_EVENTBUF) {
00087       // read another event from open file into our buffer
00088       do {
00089          fxFile->getline(sbuf, sizeof(sbuf), '\n'); // read whole line
00090          if (fxFile->eof() || !fxFile->good()) {
00091             // reached last line or read error?
00092             SetCreateStatus(1);
00093             SetErrMess(Form("End of input file %s", GetName()));
00094             SetEventStatus(1);
00095             throw TGo4EventEndException(this);
00096          }
00097       } while (strstr(sbuf, "#") || strstr(sbuf, "!")); // skip any comments
00098 
00099       Int_t status = 1;
00100       // process on event information in our buffer
00101       // scan the last input line for values:
00102       uint16_t val = 0;
00103       errno = 0; /* To distinguish success/failure after call */
00104       val = strtol(sbuf, 0, 16); // interpret string as hex value
00105       //cout << " value="<<hex<<val << endl;
00106       len++;
00107       if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
00108             || (errno != 0 && val == 0)) {
00109          status = 1;
00110       } else {
00111          status = 0; // only ok if at least one value scanned
00112          *pdata++ = val;
00113 
00114       } // if errno
00115 
00116       // test here for error in input event
00117 
00118       if (status != 0) {
00119          mbs->SetValid(kFALSE);
00120          // somethings wrong, display error message from f_evt_error()
00121          SetErrMess("TSpadicV10Source Event Source --  ERROR !!!");
00122          throw TGo4EventErrorException(this);
00123       }
00124 
00125       // set lower bits of groupid as spadicid in crate number:
00126       if(spadicid<0)
00127       {
00128          uint8_t head = (val >> 12) & 0xF;
00129           if (head == 0x8) {
00130             //cout <<"TSpadicV10Source found begin of message word"<< hex << (int) val<< dec << endl;
00131             spadicid= (val >> 4) & 0x7F;
00132             //cout <<"TSpadicV10Source found spadic id:"<< (int) spadicid << endl;
00133             }
00134       }
00135 
00136 
00137       // check here for correct message termination near end of buffer
00138       unsigned delta = SPADICV10SOURCE_EVENTBUF - len;
00139       if (delta < 50) {
00140          uint8_t head = (val >> 12) & 0xF;
00141          if (head == 0xB) {
00142 //            cout
00143 //                  << "TSpadicV10Source::BuildEvent Found final end of message at delta:"
00144 //                  << delta << endl;
00145             break;
00146          } else if (head == 0xC) {
00147 //            cout
00148 //                  << "TSpadicV10Source::BuildEvent Found final buffer  overflow message at delta:"
00149 //                  << delta << endl;
00150             break;
00151          } else if (head == 0xD) {
00152 //            cout
00153 //                  << "TSpadicV10Source::BuildEvent Found final epoch marker at delta:"
00154 //                  << delta << endl;
00155             break;
00156          } else if (head == 0xF) {
00157 //            cout
00158 //                  << "TSpadicV10Source::BuildEvent Found final info message at delta:"
00159 //                  << delta << endl;
00160             break;
00161          } // if head
00162       } // if delta
00163 
00164    } // while len
00165      //cout <<"----- BuildEvent added subevent of len "<<len<<", fullid:"<<fFullId<<endl;
00166    TGo4MbsSubEvent* sub = mbs->AddSubEvent(fFullId, (Short_t*) &data, len + 2);
00167    // sub->SetSubcrate(spadicid); set sp605 here later
00168    mbs->SetValid(kTRUE);
00169 
00170    return kTRUE;
00171 }
00172 
00173 
00174 Int_t TSpadicV10Source::Open()
00175 {
00176    if (fbIsOpen) return -1;
00177    // open connection/file
00178    fxFile = new std::ifstream(fArg.Data());
00179    if ((fxFile == 0) || !fxFile->good()) {
00180       delete fxFile;
00181       fxFile = 0;
00182       SetCreateStatus(1);
00183       SetErrMess(Form("Eror opening user file:%s", GetName()));
00184       throw TGo4EventErrorException(this);
00185    }
00186    fbIsOpen = kTRUE;
00187    fFullId = (0x0000FFFF & roc::proc_SpadicV10Raw) |              // this is procid - Go4 identifier for different data kinds
00188              (0x00FF0000 & (0 << 16)) |                           // this is subcrate - sp605 id
00189              (0xFF000000 & (sp605::formatSpadic10Direct << 24));  // this is control  - message format
00190    return 0;
00191 }
00192 
00193 
00194 Int_t TSpadicV10Source::Close()
00195 {
00196    if(!fbIsOpen) return -1;
00197 
00198    delete fxFile;
00199    fxFile = 0;
00200    fbIsOpen = kFALSE;
00201    return 0;
00202 }

Generated on Tue Dec 10 2013 04:52:23 for ROCsoft by  doxygen 1.7.1