StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFcsRawDaqReader.cxx
1 /*
2  *
3  * \class StFcsRawMaker
4  *
5  */
6 
7 /*
8  mRdr->det("fcs")->get("adc") (or "zs" instead of "adc")
9  sec DAQ PC e.g. 1="fcs01"
10  row DEP bd# (1-2=ecal, 4=hcal, 6=pres)
11  pad DEP ch (0-15=ch#, 16-31=empty, 33=trigger data for DEP16)
12  flags various stamps in time (ignore for now)
13  data ADC
14 
15  mRdr->det("stgc")->get("raw");
16  sec DAQ PC, e.g. 1="stgc01"
17  rdo RDO board# (only 1 for 2019)
18 
19  mRdr->det("stgc")->get("altro",r)
20  r RDO board# (only 1 for 2019)
21  row FEE ALTRO id (2 per FEEBd)
22  pad Chip channel (16ch)
23  tb Timebin
24  adc ADC
25 */
26 
27 
28 #include "StFcsRawDaqReader.h"
29 #include "StRoot/StEvent/StEvent.h"
30 
31 #include "StRoot/St_base/StMessMgr.h"
32 #include "RTS/src/DAQ_FCS/daq_fcs.h"
33 #include "RTS/src/DAQ_STGC/daq_stgc.h"
34 #include "RTS/src/DAQ_READER/daq_dta.h"
35 #include "StRoot/StEvent/StTriggerData.h"
36 #include "StRoot/StEvent/StTriggerData2022.h"
37 #include "StRoot/StEvent/StFcsCollection.h"
38 #include "StRoot/StEvent/StFcsHit.h"
39 #include "StRoot/StFcsDbMaker/StFcsDbMaker.h"
40 #include "StRoot/StFcsDbMaker/StFcsDb.h"
41 #include "RTS/src/DAQ_READER/daqReader.h"
42 
43 #include <string.h>
44 #include <time.h>
45 
46 StFcsRawDaqReader::StFcsRawDaqReader( const Char_t* name, const Char_t *daqFileName) :
47  StMaker(name),mDaqFileName(daqFileName){
48  std::string daqFileNameS( daqFileName );
49 };
50 
51 StFcsRawDaqReader::~StFcsRawDaqReader(){
52  if(mRdr) delete mRdr;
53 };
54 
55 Int_t StFcsRawDaqReader::prepareEnvironment(){
56  mEvent = (StEvent*)GetInputDS("StEvent");
57  if(mEvent) {
58  LOG_DEBUG <<"::prepareEnvironment() found StEvent"<<endm;
59  } else {
60  mEvent=new StEvent();
61  AddData(mEvent);
62  LOG_DEBUG <<"StFcsRawDaqReader::prepareEnvironment() has added StEvent"<<endm;
63  }
64  mFcsCollectionPtr=mEvent->fcsCollection();
65  if(!mFcsCollectionPtr) {
66  mFcsCollectionPtr=new StFcsCollection();
67  mEvent->setFcsCollection(mFcsCollectionPtr);
68  LOG_DEBUG <<"StFcsRawDaqReader::prepareEnvironment() has added StFcsCollection"<<endm;
69  } else {
70  mFcsCollectionPtr=mEvent->fcsCollection();
71  LOG_DEBUG <<"StFcsRawDaqReader::prepareEnvironment() found StFcsCollection"<<endm;
72  };
73  return kStOK;
74 };
75 
76 Int_t StFcsRawDaqReader::Init(){
77  GetEvtHddr()->SetEventNumber(1);
78  LOG_INFO << "Opening "<< mDaqFileName.data() <<endm;
79 
80  mRdr = new daqReader( const_cast< Char_t* >( mDaqFileName.data() ) );
81  if(!mRdr) {
82  LOG_FATAL << "Error constructing daqReader" << endm;
83  return kStFatal;
84  }
85  mRdr->get(0,EVP_TYPE_ANY);
86  int unixtime=mRdr->evt_time;
87  struct tm* local = localtime((const time_t*)&unixtime);
88  int date=(local->tm_year+1900)*10000 + (local->tm_mon+1)*100 + local->tm_mday;
89  int time=local->tm_hour*10000 + local->tm_min*100 + local->tm_sec;
90  LOG_INFO << Form("Event Unix Time = %d %0d %06d",mRdr->evt_time,date,time) << endm;
91 
92  StFcsDbMaker* mFcsDbMkr = static_cast<StFcsDbMaker*>(GetMaker("fcsDbMkr"));
93  if(!mFcsDbMkr){
94  LOG_FATAL << "Error finding StFcsDbMaker"<< endm;
95  return kStFatal;
96  }
97  mFcsDbMkr->SetDateTime(date,time);
98  LOG_INFO << "Using date and time " << mFcsDbMkr->GetDateTime().GetDate() << ", "
99  << mFcsDbMkr->GetDateTime().GetTime() << endm;
100 
101  mFcsDb = static_cast<StFcsDb*>(GetDataSet("fcsDb"));
102  if(!mFcsDb){
103  LOG_FATAL << "Error finding StFcsDb"<< endm;
104  return kStFatal;
105  }
106  return kStOK;
107 };
108 
110  LOG_DEBUG << "Starting StFcsRawDaqReader::Make()"<<endm;
111 
112  static int nskip=0;
113  static int nskiptot=0;
114  mTrg=0;
115  prepareEnvironment();
116 
117  /*
118  mRdr->get(0,EVP_TYPE_ANY);
119  if(mRdr->status == EVP_STAT_EOR || (mMaxSector>0 && nInSec>=mMaxEvtPerSector)) {
120  LOG_INFO <<"End of File reached..."<<endm;
121  if(mMaxSector==0){
122  return kStEOF;
123  }else{
124  mSector++;
125  if(mSector>mMaxSector) return kStEOF;
126  TString fn(mDaqFileName.data());
127  fn.ReplaceAll("_s01_",Form("_s%02d_",mSector));
128  LOG_INFO << "Opening "<< fn.Data() <<endm;
129  delete mRdr;
130  mRdr = new daqReader( const_cast< Char_t* >( fn.Data() ) );
131  if(!mRdr) {
132  LOG_FATAL << "Error constructing daqReader" << endm;
133  return kStFatal;
134  }
135  mRdr->get(0,EVP_TYPE_ANY);
136  nInSec=0;
137  nskip=0;
138  }
139  }
140  int trgcmd = mRdr->trgcmd;
141  if(trgcmd != 4 && trgcmd !=10){ // 4=phys/ped 10=LED
142  nskip++;
143  nskiptot++;
144  printf("trgcmd=%d skipping nskip=%d nskiptot=%d\n",trgcmd,nskip,nskiptot);
145  return kStOK;
146  }
147  */
148 
149  int GoodOrEOR=0;
150  while(GoodOrEOR==0){
151  mRdr->get(0,EVP_TYPE_ANY);
152  if(mRdr->status != EVP_STAT_EOR){
153  int trgcmd = mRdr->trgcmd;
154  if(trgcmd != 4 && trgcmd !=10){ // 4=phys/ped 10=LED
155  nskip++;
156  nskiptot++;
157  if(mDebug) LOG_INFO << Form("trgcmd=%d skipping nskip=%d nskiptot=%d",trgcmd,nskip,nskiptot)<<endm;
158  continue;
159  }
160  }
161  if(mRdr->status==EVP_STAT_EOR || (mMaxSector>0 && mEvtInSector>=mMaxEvtPerSector)) {
162  if(mMaxSector==0){
163  LOG_INFO <<"End of File reached..."<<endm;
164  return kStEOF;
165  }else{
166  LOG_INFO <<"EOF or max event for a sector file..."<<endm;
167  mSector++;
168  if(mSector>mMaxSector) return kStEOF;
169  TString fn(mDaqFileName.data());
170  fn.ReplaceAll("_s01_",Form("_s%02d_",mSector));
171  LOG_INFO << "Opening "<< fn.Data() <<endm;
172  delete mRdr;
173  mRdr = new daqReader( const_cast< Char_t* >( fn.Data() ) );
174  if(!mRdr) {
175  LOG_FATAL << "Error constructing daqReader" << endm;
176  return kStFatal;
177  }
178  mEvtInSector=0;
179  nskip=0;
180  continue;
181  }
182  }else{
183  GoodOrEOR=1;
184  }
185  }
186 
187  int trgcmd = mRdr->trgcmd;
188  if(trgcmd != 4 && trgcmd !=10){ // 4=phys/ped 10=LED
189  LOG_INFO << Form("This should not happen!!! trgcmd=%d skipping nskip=%d nskiptot=%d",trgcmd,nskip,nskiptot)<<endm;
190  return kStOK;
191  }
192  if(nskip>0){
193  printf("Skipped nskip=%d nskiptot=%d\n",nskip,nskiptot);
194  nskip=0;
195  }
196  mEvtInSector++; //only count for valid trgCmd
197 
198  mTrgMask = mRdr->daqbits64;
199  if(mDebug){
200  printf("daqbits64=");
201  for(int i=63; i>=0; i--){
202  if(i%8==7) printf(" ");
203  printf("%1llx",(mTrgMask>>i)&0x1);
204  };
205  printf("\n");
206  }
207 
208  daq_dta *dd = 0;
209  dd = mRdr->det("trg")->get("raw");
210  int startrg=0,fcstrg=0;
211  if(!dd){
212  LOG_WARN << "trg/raw not found" << endm;
213  }else{
214  while(dd->iterate()) {
215  u_char *trg_raw = dd->Byte;
216  struct simple_desc {
217  short len ;
218  char evt_desc ;
219  char ver ;
220  } *desc ;
221  desc = (simple_desc *) trg_raw ;
222  //printf("Trigger: raw bank has %d bytes: ver 0x%02X, desc %d, len %d\n",dd->ncontent,desc->ver,desc->evt_desc,desc->len);
223  if(desc->ver==0x47){
224  TriggerDataBlk2022* trgdata2022 = (TriggerDataBlk2022*)dd->Byte;
225  LOG_INFO << "Creating StTriggerData for ver=0x47 (2022) with run="<<mRun<<endm;
226  mEvent->setTriggerData((StTriggerData*)new StTriggerData2022(trgdata2022,mRun,1,mDebug));
227  LOG_DEBUG << "Added StTriggerData to StEvent"<<endm;
228  //AddData(new TObjectSet("StTriggerData",new StTriggerData2019(trgdata2019,mRun,1,mDebug),kTRUE));
229  //LOG_DEBUG << "Adding Dataset StTriggerData"<<endm;
230  //mTrg = (StTriggerData*) (GetData("StTriggerData")->GetObject());
231  mTrg = mEvent->triggerData();
232  LOG_DEBUG << "Got back Dataset StTriggerData addr="<<mTrg<<endm;
233 
234  mFcsTcuBit = mTrg->lastDSM(5);
235  //unsigned short lastdsm4 = mTrg->lastDSM(4);
236  //unsigned short fcs0 = (lastdsm4 >> 10) & 0x1;
237  //unsigned short fcs1 = (lastdsm4 >> 5) & 0x1;
238  //unsigned short fcs2 = (lastdsm4 >> 7) & 0x1;
239  //unsigned short fcs3 = (lastdsm4 >> 8) & 0x1;
240  //unsigned short fcs4 = (lastdsm4 >> 9) & 0x1;
241  //unsigned short fcs5 = (lastdsm4 >> 12) & 0x1;
242  //unsigned short fcs6 = (lastdsm4 >> 13) & 0x1;
243  //unsigned short fcs7 = (lastdsm4 >> 14) & 0x1;
244  //unsigned short fcs8 = (lastdsm4 >> 15) & 0x1;
245  //mFcsTcuBit = fcs0 + (fcs1<<1) + (fcs2<<2) + (fcs3<<3)
246  // + (fcs4<<4) + (fcs5<<5) + (fcs6<<6) + (fcs7<<7) + (fcs8<<8);
247  LOG_DEBUG << Form("FCS TCU Bits = 0x%04x",mFcsTcuBit)<<endm;
248 
249  unsigned long long l2sum=mTrg->l2sum();
250  //startrg = (l2sum & 0xFF8000FFFFFFFFFF)?1:0;
251  //fcstrg = (l2sum & 0x007FFF0000000000)?1:0;
252  //LOG_DEBUG << Form("L2SUM = 0x%016llx STAR=%1d FCS=%1d",l2sum,startrg,fcstrg) << endm;
253 
254  }else{
255  printf("Unknown StTriggerData version = %x\n",desc->ver);
256  }
257  }
258  }
259 
260  /*
261  //Get DEPIO board info from "adc"
262  mFcsDepOut=0;
263  dd = mRdr->det("fcs")->get("adc");
264  if(dd){
265  while(dd->iterate()) {
266  //int sec = ((dd->sec >> 11) & 0x1F) + 1;
267  //int rdo = ((dd->sec >> 8) & 0x7) + 1;
268  int ehp = (dd->sec >> 6) & 0x3;
269  int ns = (dd->sec >> 5) & 1;
270  int dep = dd->row ;
271  int ch = dd->pad ;
272  //printf("DEPIO EHP=%1d NS=%1d DEP=%02d CH=%02d N=%d\n",
273  // ehp,ns,dep,ch,dd->ncontent);
274  if(ehp==3 && ns==0 && dep==0 && (ch==4 || ch==5)){
275  u_int n=dd->ncontent;
276  u_short *d16 = (u_short *)dd->Void;
277  if(ch==4) mFcsDepOut += (d16[96] & 0xFF);
278  if(ch==5) mFcsDepOut += (d16[96] & 0xFF) << 8;
279  //for(int i=0; i<n; i++) printf(" tb=%3d d16=0x%04x\n",i,d16[i]);
280  }
281  }
282  }
283  */
284 
285  int ndata=0, nvaliddata=0;
286  // char* mode[2]={"adc","zs"};
287  string mode[2]={"adc","zs"};
288  if(mReadMode==1){
289 
290  }else{
291  mReadMode=0;
292  }
293  dd = mRdr->det("fcs")->get(mode[mReadMode].c_str());
294  if(!dd){
295  mFcsCollectionPtr->setDataExist(0);
296  }else{
297  mFcsCollectionPtr->setDataExist(1);
298  while(dd->iterate()) {
299  int sec = ((dd->sec >> 11) & 0x1F) + 1;
300  int rdo = ((dd->sec >> 8) & 0x7) + 1;
301  int ehp = (dd->sec >> 6) & 0x3;
302  int ns = (dd->sec >> 5) & 1;
303  int dep = dd->row ;
304  int ch = dd->pad ;
305  u_int n=dd->ncontent;
306  int detid,id,crt,sub;
307  mFcsDb->getIdfromDep(ehp,ns,dep,ch,detid,id,crt,sub);
308  //printf("EHP=%1d NS=%1d DEP=%02d CH=%02d DET=%1d id=%4d\n",ehp,ns,dep,ch,detid,id);
309  //if(ch>=32) continue;
310  u_short *d16 = (u_short *)dd->Void;
311  StFcsHit* hit=0;
312  unsigned short tmp[1024];
313  if(mReadMode==0){
314  hit = new StFcsHit(0,detid,id,ns,ehp,dep,ch,n,d16);
315  }else{
316  /*
317  if(startrg==0 && fcstrg==1){
318  for(u_int i=0; i<n; i++) {
319  u_int tb = dd->adc[i].tb;
320  u_int data = dd->adc[i].adc;
321  tmp[i*2 ]=data;
322  tmp[i*2+1]=tb + 8;
323  //printf("AAA %4d : %4d %4d : %4d %4d\n",i,data&0xfff,d16[i*2]&0xfff,tb,d16[i*2+1]);
324  }
325  hit = new StFcsHit(1,detid,id,ns,ehp,dep,ch,2*n,tmp);
326  */
327  //}else{
328  hit = new StFcsHit(1,detid,id,ns,ehp,dep,ch,2*n,d16);
329  //}
330  }
331  mFcsCollectionPtr->addHit(detid,hit);
332  ndata++;
333  if(detid<6) nvaliddata++;
334 
335  if(mDebug){
336  printf("FCS %3s : S%2d:%2d [det%1d ns%1d dep%2d ch%2d] det=%d id=%3d size=%3d adc=",
337  mode[mReadMode].c_str(),sec,rdo,ehp,ns,dep,ch,detid,id,n) ;
338  for(unsigned int tb=0; tb<hit->nTimeBin(); tb++) printf("%4d ", hit->adc(tb));
339  //for(int tb=0; tb<3; tb++) printf("%4d ", hit->adc(tb));
340  printf("\n");
341  }
342  }
343  }
344  LOG_DEBUG <<Form("FCS found %d data lines, and %d valid data lines",
345  ndata,nvaliddata)<<endm;
346  if(mDebug>3) mFcsCollectionPtr->print(3);
347 
348  ndata=0;
349  dd = mRdr->det("stgc")->get("raw");
350  if(dd){
351  while(dd->iterate()){
352  if(mDebug) printf("STGC RAW: stgc%02d(sec) RDO=%1d size=%d(bytes)\n",dd->sec,dd->rdo,dd->ncontent);
353  }
354  }
355  for(int r=1; r<=6; r++) {
356  dd = mRdr->det("stgc")->get("altro",r) ;
357  while(dd && dd->iterate()) { //per xing and per RDO
358  if(mDebug) printf("STGC ALTRO: stgc%02d(sec) RDO=%1d ALTRO=%03d(row) Ch=%02d(pad)\n",dd->sec,r,dd->row,dd->pad);
359  for(u_int i=0; i<dd->ncontent; i++) {
360  if(mDebug) printf(" TB=%3d ADC=%4d",dd->adc[i].tb,dd->adc[i].adc) ;
361  ndata++;
362  }
363  if(mDebug) printf("\n");
364  }
365  }
366  LOG_DEBUG <<Form("STGC found %d data lines",ndata)<<endm;
367 
368  return kStOK;
369 };
370 
371 void StFcsRawDaqReader::Clear( Option_t *opts ){
372  //StTriggerData2019* d=(StTriggerData2019*)mTrg;
373  //if(d) delete d;
374  //mFcsCollectionPtr->print();
375  //StMaker::Clear(opts);
376  //mFcsCollectionPtr->print();
377  //if(mFcsCollectionPtr){
378  // for(int d=0; d<kFcsNDet+1; d++) { mFcsCollectionPtr->hits(d).clear(); }
379  //}
380  //mFcsCollectionPtr->print();
381 };
382 
383 ClassImp(StFcsRawDaqReader);
384 
385 /*
386  * $Id: StFcsRawDaqReader.cxx,v 1.9 2021/05/30 21:31:30 akio Exp $
387  * $Log: StFcsRawDaqReader.cxx,v $
388  * Revision 1.9 2021/05/30 21:31:30 akio
389  * Adding StTriggerData to StEvent, not dataset
390  *
391  * Revision 1.8 2021/05/27 13:10:38 akio
392  * Many updates for trigger bits and around Clear()
393  *
394  * Revision 1.7 2021/03/30 13:30:11 akio
395  * StFcsDbMAker->StFcsDB
396  *
397  * Revision 1.6 2021/02/13 21:39:17 akio
398  * debug printouts
399  *
400  * Revision 1.5 2021/01/11 14:39:12 akio
401  * Change logic to skip over none standard events at the begining of files.
402  * Added function to get event# in a sector=file.
403  *
404  * Revision 1.4 2019/07/10 07:47:37 akio
405  * minor fix for compilation warnings
406  *
407  * Revision 1.3 2019/07/10 03:09:57 akio
408  * fix trigger data version for run19
409  *
410  * Revision 1.2 2019/06/07 18:22:33 akio
411  * *** empty log message ***
412  *
413  * Revision 1.1 2019/03/14 14:45:35 akio
414  * FCS raw daq reader for online monitoring
415  *
416  * Revision 1.1 2019/03/13 20:31:34 akio
417  * FCS daqfile/evp reader for online
418  *
419  *
420  */
virtual void Clear(Option_t *opts="")
User defined functions.
virtual void AddData(TDataSet *data, const char *dir=".data")
User methods.
Definition: StMaker.cxx:332
void getIdfromDep(int ehp, int ns, int dep, int ch, int &detectorId, int &id, int &crt, int &slt) const
Get DEP map.
Definition: StFcsDb.cxx:1031
Definition: Stypes.h:43
virtual Int_t Make()
Definition: Stypes.h:40