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 
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 
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    
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    
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00079    static uint16_t data[SPADICV10SOURCE_EVENTBUF]; 
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       
00088       do {
00089          fxFile->getline(sbuf, sizeof(sbuf), '\n'); 
00090          if (fxFile->eof() || !fxFile->good()) {
00091             
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, "!")); 
00098 
00099       Int_t status = 1;
00100       
00101       
00102       uint16_t val = 0;
00103       errno = 0; 
00104       val = strtol(sbuf, 0, 16); 
00105       
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; 
00112          *pdata++ = val;
00113 
00114       } 
00115 
00116       
00117 
00118       if (status != 0) {
00119          mbs->SetValid(kFALSE);
00120          
00121          SetErrMess("TSpadicV10Source Event Source --  ERROR !!!");
00122          throw TGo4EventErrorException(this);
00123       }
00124 
00125       
00126       if(spadicid<0)
00127       {
00128          uint8_t head = (val >> 12) & 0xF;
00129           if (head == 0x8) {
00130             
00131             spadicid= (val >> 4) & 0x7F;
00132             
00133             }
00134       }
00135 
00136 
00137       
00138       unsigned delta = SPADICV10SOURCE_EVENTBUF - len;
00139       if (delta < 50) {
00140          uint8_t head = (val >> 12) & 0xF;
00141          if (head == 0xB) {
00142 
00143 
00144 
00145             break;
00146          } else if (head == 0xC) {
00147 
00148 
00149 
00150             break;
00151          } else if (head == 0xD) {
00152 
00153 
00154 
00155             break;
00156          } else if (head == 0xF) {
00157 
00158 
00159 
00160             break;
00161          } 
00162       } 
00163 
00164    } 
00165      
00166    TGo4MbsSubEvent* sub = mbs->AddSubEvent(fFullId, (Short_t*) &data, len + 2);
00167    
00168    mbs->SetValid(kTRUE);
00169 
00170    return kTRUE;
00171 }
00172 
00173 
00174 Int_t TSpadicV10Source::Open()
00175 {
00176    if (fbIsOpen) return -1;
00177    
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) |              
00188              (0x00FF0000 & (0 << 16)) |                           
00189              (0xFF000000 & (sp605::formatSpadic10Direct << 24));  
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 }