Go to the documentation of this file.00001 #include "TSpadicSource.h"
00002
00003 #include "TClass.h"
00004 #include "TUrl.h"
00005
00006 #include "TGo4Log.h"
00007 #include "TGo4UserSourceParameter.h"
00008 #include "TGo4MbsEvent.h"
00009 #include "TGo4EventErrorException.h"
00010 #include "TGo4EventTimeoutException.h"
00011 #include "TGo4EventEndException.h"
00012
00013 #ifdef ROC_WITHSPADIC
00014
00015 #include "roc/Board.h"
00016 #include "dabc/timing.h"
00017 #include "spadiclib.h"
00018 #include "spadic/Message.h"
00019
00020 #endif
00021
00022
00023
00024 TSpadicSource::TSpadicSource(TGo4UserSourceParameter* par) :
00025 TGo4EventSource(),
00026 fArg(),
00027 fFullId(0),
00028 fLib(0),
00029 fPackages(),
00030 fBoardId(1),
00031 fHitDelay(20),
00032 fTriggerPerSecond(0),
00033 fLastTrigger(0.)
00034
00035 {
00036 fArg = par->GetName();
00037
00038 Open();
00039 }
00040
00041 TSpadicSource::TSpadicSource() :
00042 TGo4EventSource(),
00043 fArg(),
00044 fFullId(0),
00045 fLib(0),
00046 fPackages(),
00047 fBoardId(1),
00048 fHitDelay(20)
00049 {
00050 }
00051
00052
00053 TSpadicSource::~TSpadicSource()
00054 {
00055 Close();
00056 }
00057
00058 Bool_t TSpadicSource::CheckEventClass(TClass* cl)
00059 {
00060 return cl->InheritsFrom(TGo4MbsEvent::Class());
00061 }
00062
00063 #ifdef ROC_WITHSPADIC
00064
00065 Bool_t TSpadicSource::BuildEvent(TGo4EventElement* dest)
00066 {
00067 if (fLib==0) throw TGo4EventErrorException(this);
00068
00069 dabc::TimeStamp start = dabc::Now();
00070 dabc::TimeStamp now = start;
00071
00072 while ((now - start) < 10.) {
00073
00074 now = dabc::Now();
00075
00076 if (fTriggerPerSecond > 0)
00077 if ((now.AsDouble() - fLastTrigger) > 1./fTriggerPerSecond) {
00078
00079 fLib->write(0x11, 0x01);
00080 fLib->write(0x11, 0x00);
00081 fLastTrigger = now.AsDouble();
00082 }
00083
00084
00085 if (fPackages.size()==0) {
00086 fLib->readPackages(fPackages, 50);
00087
00088
00089 }
00090
00091 if (fPackages.size()==0) continue;
00092
00093 unsigned package_size = fPackages.front().size();
00094
00095 if (sizeof(fMsgBuffer) < package_size) {
00096 TGo4Log::Error("Local buffer in TSpadicSource is too small, required is %u", package_size);
00097 throw TGo4EventErrorException(this);
00098 }
00099
00100 for (unsigned nn = 0; nn < package_size; nn++)
00101 fMsgBuffer[nn] = fPackages.front()[nn];
00102
00103 fPackages.pop_front();
00104
00105 spadic::Message msg(fMsgBuffer);
00106
00107 if (!msg.CheckMessage()) {
00108 TGo4Log::Error("Bad message format !!!");
00109 continue;
00110 }
00111
00112 TGo4MbsEvent* mbs = (TGo4MbsEvent*) dest;
00113
00114 mbs->SetCount(msg.GetEventIDNumber());
00115
00116 return mbs->AddSubEvent(fFullId, (Short_t*) fMsgBuffer, package_size/sizeof(Short_t) + 2) != 0;
00117 }
00118
00119
00120 throw TGo4EventTimeoutException(this);
00121 return false;
00122 }
00123
00124
00125 Int_t TSpadicSource::Open()
00126 {
00127 TGo4Log::Info("Open SpadicSource %s", fArg.Data());
00128
00129 TString buf = fArg;
00130 buf.ToUpper();
00131 if ((buf.Index("SUSIBO")==0) && (buf.Length()>6)) {
00132 fBoardId = 1;
00133
00134 buf.Remove(0, 6);
00135
00136 int cnt=0;
00137
00138 while (cnt<buf.Length() && (buf[cnt]>='0') && (buf[cnt]<='9')) cnt++;
00139
00140 if (cnt>0) {
00141 buf.Remove(cnt);
00142 fBoardId = buf.Atoi();
00143 }
00144 }
00145
00146 TUrl url(fArg.Data(), kTRUE);
00147 url.ParseOptions();
00148 int v = url.GetIntValueFromOptions("trigger");
00149 if (v>0) fTriggerPerSecond = v;
00150 v = url.GetIntValueFromOptions("delay");
00151 if (v>=0) fHitDelay = v;
00152 v = url.GetIntValueFromOptions("testloop");
00153
00154 fLib = new SuS::SpadicLib();
00155
00156 TGo4Log::Info("Open SpadicSource Susibo:%d HitDelay:%d TriggerPerSec:%d", fBoardId, fHitDelay, fTriggerPerSecond);
00157
00158 if (!fLib->connectSpadic(SuS::SpadicIO::Ftdi, true, true, fBoardId, fHitDelay)) {
00159 TGo4Log::Error("Cannot connect to Susibo board %d", fBoardId);
00160 delete fLib;
00161 fLib = 0;
00162 return -1;
00163 }
00164
00165 fFullId = roc::proc_TRD_Spadic | (fBoardId << 16) | (spadic::Message::packageStruct << 24);
00166
00167
00168 if (v>0) {
00169
00170
00171 TGo4Log::Debug("Try test loop in the beginning");
00172
00173 for (int cnt=0;cnt<v;cnt++) {
00174 dabc::Sleep(0.1);
00175
00176 if (fLib) {
00177
00178 fLib->write(0x11, 0x01);
00179 fLib->write(0x11, 0x00);
00180
00181 fLib->readPackages(fPackages, 50);
00182 }
00183
00184 TGo4Log::Debug("Loop = %d size = %u", cnt, fPackages.size());
00185
00186 }
00187
00188 TGo4Log::Debug("Test loop finished, result = %u", fPackages.size());
00189 }
00190
00191 return 0;
00192 }
00193
00194
00195 Int_t TSpadicSource::Close()
00196 {
00197 if(fLib) {
00198 fLib->disconnectSpadic();
00199 int timeout(1000);
00200 while (!fLib->isReady() && timeout-- > 0) dabc::Sleep(0.001);
00201 if(timeout <= 0) TGo4Log::Error("could not disconnect SPADIC %d, timeout", fBoardId);
00202 delete fLib;
00203 fLib = 0;
00204 }
00205
00206 fFullId = 0;
00207
00208 return 0;
00209 }
00210
00211 #else
00212
00213 Bool_t TSpadicSource::BuildEvent(TGo4EventElement* dest)
00214 {
00215 TGo4Log::Error("SPADIC source is not supported");
00216 throw TGo4EventErrorException(this);
00217 return kFALSE;
00218 }
00219
00220 Int_t TSpadicSource::Open()
00221 {
00222 TGo4Log::Error("SPADIC source is not supported");
00223 return -1;
00224 }
00225
00226
00227 Int_t TSpadicSource::Close()
00228 {
00229 TGo4Log::Error("SPADIC source is not supported");
00230 return -1;
00231 }
00232
00233
00234 #endif