00001 //----------------------------------------------------------------------- 00002 // The GSI Online Offline Object Oriented (Go4) Project 00003 // Experiment Data Processing at EE department, GSI 00004 //----------------------------------------------------------------------- 00005 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH 00006 // Planckstr. 1, 64291 Darmstadt, Germany 00007 // Contact: http://go4.gsi.de 00008 //----------------------------------------------------------------------- 00009 // This software can be used under the license agreements as stated 00010 // in Go4License.txt file which is part of the distribution. 00011 //----------------------------------------------------------------------- 00013 // Event Source for Trb data format 00014 // V 0.3 12-Jul-2011 00015 // Joern Adamczewski-Musch, GSI Darmstadt 00016 // j.adamczewski@gsi.de 00017 00018 #ifndef _TTrbEVENTSOURCE_H_ 00019 #define _TTrbEVENTSOURCE_H_ 00020 00021 #include "TGo4EventSource.h" 00022 #include <fstream> 00023 #include <stdint.h> 00024 00025 #include "TGo4MbsEvent.h" 00026 00027 #define Trb_BUFSIZE 0x80000 00028 00029 class TTrbRawEvent; 00030 class TGo4UserSourceParameter; 00031 00032 #pragma pack(push, 1) 00033 00034 /* 00035 //Description of the Event Structure 00036 // 00037 //An event consists of an event header and of varying number of subevents, each with a subevent header. The size of the event header is fixed to 0x20 bytes 00038 // 00039 //Event header 00040 //evtHeader 00041 //evtSize evtDecoding evtId evtSeqNr evtDate evtTime runNr expId 00042 // 00043 // * evtSize - total size of the event including the event header, it is measured in bytes. 00044 // * evtDecoding - event decoding type: Tells the analysis the binary format of the event data, so that it can be handed to a corresponding routine for unpacking into a software usable data structure. For easier decoding of this word, the meaning of some bits is already predefined: 00045 // evtDecoding 00046 // msB --- --- lsB 00047 // 0 alignment decoding type nonzero 00048 // o The first (most significant) byte is always zero. 00049 // o The second byte contains the alignment of the subevents in the event. 0 = byte, 1 = 16 bit word, 2 = 32 bit word... 00050 // o The remaining two bytes form the actual decoding type, e.g. fixed length, zero suppressed etc. 00051 // o The last byte must not be zero, so the whole evtDecoding can be used to check for correct or swapped byte order. 00052 // 00053 //It is stated again, that the whole evtDecoding is one 32bit word. The above bit assignments are merely a rule how to select this 32bit numbers. 00054 // 00055 // * evtId - event identifier: Tells the analysis the semantics of the event data, e.g. if this is a run start event, data event, simulated event, slow control data, end file event, etc.. 00056 // evtId 00057 // 31 30 - 16 15 - 12 11 - 8 5 - 7 4 3- 0 00058 // error bit reserved version reserved MU decision DS flag ID 00059 // o error bit - set if one of the subsystems has set the error bit 00060 // o version - 0 meaning of event ID before SEP03; 1 event ID after SEP03 00061 // o MU decision - 0 = negative LVL2 decision; >0 positive LVL2 decision 00062 // MU trigger algo result 00063 // 1 negative decision 00064 // 2 positive decision 00065 // 3 positive decision due to too many leptons or dileptons 00066 // 4 reserved 00067 // 5 reserved 00068 // o DS flag - LVL1 downscaling flag; 1 = this event is written to the tape independent on the LVL2 trigger decision 00069 // o ID - defines the trigger code 00070 // ID before SEP03 description 00071 // 0 simulation 00072 // 1 real 00073 // 2,3,4,5,6,7,8,9 calibration 00074 // 13 beginrun 00075 // 14 endrun 00076 // 00077 // ID after SEP03 description 00078 // 0 simulation 00079 // 1,2,3,4,5 real 00080 // 7,9 calibration 00081 // 1 real1 00082 // 2 real2 00083 // 3 real3 00084 // 4 real4 00085 // 5 real5 00086 // 6 special1 00087 // 7 offspill 00088 // 8 special3 00089 // 9 MDCcalibration 00090 // 10 special5 00091 // 13 beginrun 00092 // 14 endrun 00093 // * evtSeqNr - event number: This is the sequence number of the event in the file. The pair evtFileNr/evtSeqNr 00094 // 00095 //is unique throughout all events ever acquired by the system. 00096 // 00097 // * evtDate - date of event assembly (filled by the event builder, rough precision): 00098 // evtDate ISO-C date format 00099 // msB --- --- lsB 00100 // 0 year month day 00101 // 00102 // 1. The first (most significant) byte is zero 00103 // 2. The second byte contains the years since 1900 00104 // 3. The third the months since January [0-11] 00105 // 4. The last the day of the month [1-31] 00106 // 00107 // * evtTime - time of assembly (filled by the event builder, rough precision): 00108 // evtTime ISO-C time format 00109 // msB --- --- lsB 00110 // 0 hour minute second 00111 // 00112 // 1. The first (most significant) byte is zero 00113 // 2. The second byte contains the hours since midnight [0-23] 00114 // 3. The third the minutes after the hour [0-59] 00115 // 4. The last the seconds after the minute [0-60] 00116 // 00117 // * runNr - file number: A unique number assigned to the file. The runNr is used as key for the RTDB. 00118 // * evtPad - padding: Makes the event header a multiple of 64 bits long. 00119 */ 00120 00121 struct Trb_Event 00122 { 00123 Int_t evtSize; 00124 Int_t evtDecoding; 00125 Int_t evtId; 00126 Int_t evtSeqNr; 00127 Int_t evtDate; 00128 Int_t evtTime; 00129 Int_t runNr; 00130 Int_t evtPad; 00131 00133 Bool_t IsSwapped() 00134 { 00135 return (evtDecoding > 0xffffff); 00136 } 00137 00139 Int_t Value(Int_t *member) 00140 { 00141 if (IsSwapped()) 00142 { 00143 uint32_t tmp0; 00144 uint32_t tmp1; 00145 00146 tmp0 = *member; 00147 ((char *) &tmp1)[0] = ((char *) &tmp0)[3]; 00148 ((char *) &tmp1)[1] = ((char *) &tmp0)[2]; 00149 ((char *) &tmp1)[2] = ((char *) &tmp0)[1]; 00150 ((char *) &tmp1)[3] = ((char *) &tmp0)[0]; 00151 return tmp1; 00152 } 00153 else 00154 { 00155 return *member; 00156 } 00157 } 00158 00159 size_t GetSize() 00160 { 00161 return (size_t) (Value(&evtSize)); 00162 } 00163 00164 Int_t GetId() 00165 { 00166 return Value(&evtId); 00167 } 00168 00169 Int_t GetSeqNr() 00170 { 00171 return Value(&evtSeqNr); 00172 } 00173 }; 00174 00175 /* 00176 //Subevent 00177 // 00178 //Every event contains zero to unspecified many subevents. As an empty event is allowed, data outside of any subevent are not allowed. A subevents consists out of a fixed size subevent header and a varying number of data words. 00179 // 00180 // * The subevent header 00181 // subEvtHeader 00182 // subEvtSize subEvtDecoding subEvtId subEvtTrigNr 00183 // o subEvtSize - size of the subevent: This includes the the subevent header, it is measured in bytes. 00184 // o subEvtDecoding - subevent decoding type: Tells the analysis the binary format of the subevent data, so that it can be handed to a corresponding routine for unpacking into a software usable data structure. For easier decoding of this word, the meaning of some bits is already predefined: 00185 // subEvtDecoding 00186 // msB --- --- lsB 00187 // 0 data type decoding type nonzero 00188 // + The first (most significant) byte is always zero 00189 // + The second byte contains the word length of the subevent data. 0 = byte, 1 = 16 bit word, 2 = 32 bit word... 00190 // + The remaining two bytes form the actual decoding type, e.g. fixed length, zero suppressed etc. 00191 // + The last byte must not be zero, so the whole subEvtDecoding can be used to check for correct or swapped byte order. It is stated again, that the whole subEvtDecoding is one 32bit word. The above bit assignments are merely a rule how to select this 32bit numbers. 00192 // o subEvtId - subevent identifier: Tells the analysis the semantics of the subevent data, e.g. every subevent builder may get its own subEvtId. So the data structure can be analyzed by the corresponding routine after unpacking. 00193 // 1-99 DAQ 00194 // 100-199 RICH 00195 // 200-299 MDC 00196 // 300-399 SHOWER 00197 // 400-499 TOF 00198 // 500-599 TRIG 00199 // 600-699 SLOW 00200 // 700-799 TRB_RPC common TRB, but contents is RPC 00201 // 800-899 TRB_HOD pion-hodoscope 00202 // 900-999 TRB_FW forward wall 00203 // 1000-1099 TRB_START start detector 00204 // 1100-1199 TRB_TOF TOF detector 00205 // 1200-1299 TRB RICH RICH detector 00206 // 00207 //Additionally, all subEvtIds may have the MSB set. This indicates a sub event of the corresponding id that contains broken data (e.g. parity check failed, sub event was too long etc.). 00208 // 00209 // * 00210 // o subEvtTrigNr - subevent Trigger Number: This is the event tag that is produced by the trigger system and is used for checking the correct assembly of several sub entities. In general, this number is not counting sequentially. The lowest significant byte represents the trigger tag generated by the CTU and has to be same for all detector subsystems in one event. The rest is filled by the EB. 00211 // * The data words: The format of the data words (word length, compression algorithm, sub-sub-event format) is defined by the subEvtDecoding and apart from this it is completely free. The meaning of the data words (detector, geographical position, error information) is defined by the subEvtId and apart from this it is completely unknown to the data acquisition system. 00212 */ 00213 00214 struct Trb_Subevent 00215 { 00216 Int_t subEvtSize; 00217 Int_t subEvtDecoding; 00218 Int_t subEvtId; 00219 Int_t subEvtTrigNr; 00220 00222 Bool_t IsSwapped() 00223 { 00224 return (subEvtDecoding > 0xffffff); 00225 } 00226 00227 unsigned Alignment() 00228 { 00229 return 1 << ( GetDecoding() >> 16 & 0xff); 00230 } 00231 00233 Int_t Value(Int_t *member) 00234 { 00235 00236 if (IsSwapped()) 00237 { 00238 uint32_t tmp0; 00239 uint32_t tmp1; 00240 00241 tmp0 = *member; 00242 ((char *) &tmp1)[0] = ((char *) &tmp0)[3]; 00243 ((char *) &tmp1)[1] = ((char *) &tmp0)[2]; 00244 ((char *) &tmp1)[2] = ((char *) &tmp0)[1]; 00245 ((char *) &tmp1)[3] = ((char *) &tmp0)[0]; 00246 return tmp1; 00247 } 00248 else 00249 { 00250 return *member; 00251 } 00252 } 00253 00254 size_t GetSize() 00255 { 00256 return (size_t) (Value(&subEvtSize)); 00257 } 00258 00259 Int_t GetDecoding() 00260 { 00261 return Value(&subEvtDecoding); 00262 } 00263 00264 Int_t GetId() 00265 { 00266 return Value(&subEvtId); 00267 } 00268 00269 Int_t GetTrigNr() 00270 { 00271 return Value(&subEvtTrigNr); 00272 } 00273 00275 Int_t Data(unsigned idx) 00276 { 00277 const void* my= (char*) (this) + sizeof(Trb_Subevent); 00278 //const void* my= (char*) (this) + 16; 00279 Int_t val; 00280 00281 switch (Alignment()) 00282 { 00283 case 4: 00284 if (IsSwapped()) 00285 { 00286 uint32_t tmp0; 00287 uint32_t tmp1; 00288 tmp0 = ((uint32_t *) my)[idx]; 00289 ((char *) &tmp1)[0] = ((char *) &tmp0)[3]; 00290 ((char *) &tmp1)[1] = ((char *) &tmp0)[2]; 00291 ((char *) &tmp1)[2] = ((char *) &tmp0)[1]; 00292 ((char *) &tmp1)[3] = ((char *) &tmp0)[0]; 00293 val = tmp1; 00294 } 00295 else 00296 { 00297 val = ((uint32_t *) my)[idx]; 00298 } 00299 break; 00300 case 2: 00301 if (IsSwapped()) 00302 { 00303 uint16_t v; 00304 //swab(((uint16_t *) (my)[idx]), &v, 2); 00305 swab(((uint16_t *) (my)) + idx, &v, 2); 00306 val = v; 00307 } 00308 else 00309 { 00310 val = ((uint16_t *) my)[idx]; 00311 } 00312 break; 00313 default: 00314 val = ((uint8_t *) my)[idx]; 00315 break; 00316 } 00317 return val; 00318 } 00319 }; 00320 00321 #pragma pack(pop) 00322 00323 class TTrbSource : public TGo4EventSource 00324 { 00325 public: 00326 TTrbSource(); 00327 00329 TTrbSource(const char* name, const char* args, Int_t port); 00330 00332 TTrbSource(TGo4UserSourceParameter* par); 00333 00334 virtual ~TTrbSource(); 00335 00337 virtual Int_t Open(); 00338 00340 virtual Int_t Close(); 00341 00343 virtual Bool_t CheckEventClass(TClass* cl); 00344 00351 virtual Bool_t BuildEvent(TGo4EventElement* dest); 00352 00353 const char* GetArgs() const { return fxArgs.Data(); } 00354 00355 void SetArgs(const char* arg) { fxArgs=arg; } 00356 00357 Int_t GetPort() const { return fiPort; } 00358 00359 void SetPort(Int_t val) { fiPort=val; } 00360 00361 protected: 00363 Bool_t NextBuffer(); 00364 00366 Bool_t NextEvent(TGo4MbsEvent* target); 00367 00369 std::streamsize ReadFile(Char_t* dest, size_t len); 00370 00371 private: 00372 Bool_t fbIsOpen; 00373 00375 TString fxArgs; 00376 00378 Int_t fiPort; 00379 00381 std::ifstream* fxFile; 00382 00384 // Bool_t fbNeedNewBuffer; 00385 00387 TGo4SubEventHeader10 fxSubevHead; 00388 00390 Char_t* fxBuffer; 00391 00393 Trb_Event* fxHadEvent; 00394 00396 std::streamsize fxBufsize; 00397 00399 Short_t* fxCursor; 00400 00402 Short_t* fxBufEnd; 00403 00405 Int_t fiEventLen; 00406 00407 ClassDef(TTrbSource, 1) 00408 }; 00409 00410 #endif // _TTrbEVENTSOURCE_H_