StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFgtRawDaqReader.cxx
1 /*
2  *
3  * \class StFgtRawMaker
4  * \author S. Gliske (sgliske@anl.gov) based on StFgtComsicReader,
5  * v1.15 written by A. Vossen (avossen@indiana.edu)
6  *
7  * See header for description.
8  *
9  */
10 
11 #include "StFgtRawDaqReader.h"
12 #include "StRoot/StEvent/StEvent.h"
13 
14 #include "StRoot/StFgtPool/StFgtCosmicTestStandGeom/StFgtCosmicTestStandGeom.h"
15 #include "StRoot/St_base/StMessMgr.h"
16 #include "RTS/src/DAQ_FGT/daq_fgt.h"
17 #include "RTS/src/DAQ_READER/daq_dta.h"
18 #include "StRoot/StEvent/StFgtCollection.h"
19 #include "StRoot/StEvent/StFgtStripCollection.h"
20 #include "StRoot/StEvent/StFgtStrip.h"
21 #include "RTS/src/DAQ_READER/daqReader.h"
22 #include "StRoot/StFgtDbMaker/StFgtDbMaker.h"
23 #include "StRoot/StFgtUtil/StFgtConsts.h"
24 
25 #include <string.h>
26 #include <time.h>
27 
28 StFgtRawDaqReader::StFgtRawDaqReader( const Char_t* name, const Char_t *daqFileName, const Char_t* dbMkrName ) :
29  StMaker( name ), mCutShortEvents(0), mIsCosmic(0), mFgtCollectionPtr(0), mDaqFileName( daqFileName ), mDbMkrName( dbMkrName ),
30  mRdr(0), mFgtDbMkr(0), mDataType(0), mStartTbin(0), mNumTbin(15) {
31 
32  // set to being cosmic if filename ends in .sfs
33  // otherwise, assume is not
34  std::string daqFileNameS( daqFileName );
35  std::string::size_type pos = daqFileNameS.find_last_of(".");
36 
37  if( pos != std::string::npos && daqFileNameS.substr( pos ) == ".sfs" )
38  mIsCosmic = 1;
39 };
40 
41 StFgtRawDaqReader::~StFgtRawDaqReader(){
42  if( mRdr )
43  delete mRdr;
44 };
45 
46 
47 //in the cosmic maker the prepareEnvironment should only be called once (in init), so everything is constructed
48 Int_t StFgtRawDaqReader::prepareEnvironment(){
49  StEvent* eventPtr=0;
50  eventPtr= (StEvent*)GetInputDS("StEvent");
51 
52  mFgtCollectionPtr=NULL;
53  if(eventPtr) {
54  mFgtCollectionPtr=eventPtr->fgtCollection();
55  } else {
56  eventPtr=new StEvent();
57  AddData(eventPtr);
58  mFgtCollectionPtr=eventPtr->fgtCollection();
59  };
60  if(!mFgtCollectionPtr) {
61  mFgtCollectionPtr=new StFgtCollection();
62  eventPtr->setFgtCollection(mFgtCollectionPtr);
63  LOG_DEBUG <<"::prepareEnvironment() has added a non existing StFgtCollection()"<<endm;
64  } else {
65  //this should be unncessary if the member clear function is called
66  mFgtCollectionPtr->Clear();
67  };
68  return kStOK;
69 };
70 
71 Int_t StFgtRawDaqReader::Init(){
72  //LOG_INFO << "initializing" << endm;
73 
74  GetEvtHddr()->SetEventNumber(1);
75 
76  Int_t ierr = prepareEnvironment();
77 
78  //LOG_INFO << "event constructed" << endm;
79 
80  if( ierr || !mFgtCollectionPtr ) {
81  LOG_FATAL << "Error constructing FgtCollection" << endm;
82  ierr = kStFatal;
83  };
84 
85  if( !ierr ){
86  //LOG_INFO << "constructing daqReader" << endm;
87 
88  // unfortunately, the daqReader has some constness issues to be
89  // fixed. Until they are, must remove constness of the filename.
90  mRdr = new daqReader( const_cast< Char_t* >( mDaqFileName.data() ) );
91  };
92  mRdr->get(0,EVP_TYPE_ANY);
93  int unixtime=mRdr->evt_time;
94  struct tm* local = localtime((const time_t*)&unixtime);
95  int date=(local->tm_year+1900)*10000 + (local->tm_mon+1)*100 + local->tm_mday;
96  int time=local->tm_hour*10000 + local->tm_min*100 + local->tm_sec;
97  printf("Event Unix Time = %d %08d %06d\n",mRdr->evt_time,date,time);
98 
99  if( !ierr && !mIsCosmic ){
100  if( !mDbMkrName.empty() ){
101  // get the maker pointer
102  mFgtDbMkr = static_cast< StFgtDbMaker* >( GetMaker( mDbMkrName.data() ) );
103 
104  if( !ierr && !mFgtDbMkr ){
105  LOG_FATAL << "Error finding FgtDbMkr named '" << mDbMkrName << "'" << endm;
106  ierr = kStFatal;
107  };
108 
109  if( !mFgtDbMkr->InheritsFrom("StFgtDbMaker") ){
110  LOG_FATAL << "StFgtDbMkr does not inherit from StFgtDbMaker" << endm;
111  LOG_FATAL << "Name is '" << mFgtDbMkr->GetName() << "', class is '" << mFgtDbMkr->ClassName() << endm;
112  ierr = kStFatal;
113  };
114  };
115 
116  if( !mFgtDbMkr ){
117  mFgtDbMkr = static_cast< StFgtDbMaker* >( GetMakerInheritsFrom( "StFgtDbMaker" ) );
118  if( !mFgtDbMkr ){
119  LOG_FATAL << "StFgtDbMaker name not provided and error finding StFgtDbMaker" << endm;
120  ierr = kStFatal;
121  };
122  };
123 
124  if(date<21000000 && date>19990000) mFgtDbMkr->SetDateTime(date,time);
125  LOG_INFO << "Using date and time " << mFgtDbMkr->GetDateTime().GetDate() << ", "
126  << mFgtDbMkr->GetDateTime().GetTime() << endm;
127  };
128 
129  //LOG_INFO << "done with init" << endm;
130 
131  return ierr;
132 };
133 
134 //read next event from daq file and fill the fgtevent
136  Int_t ierr = kStOk;
137 
138 
139  StFgtDb *fgtTables = 0;
140  if( !mIsCosmic ){
141  if( !mFgtDbMkr ){
142  LOG_FATAL << "Pointer to Fgt DB Maker is null" << endm;
143  ierr = kStFatal;
144  };
145  if( !ierr ){
146  fgtTables = mFgtDbMkr->getDbTables();
147 
148  if( !fgtTables ){
149  LOG_FATAL << "Pointer to Fgt DB Tables is null" << endm;
150  ierr = kStFatal;
151  };
152  };
153  };
154 
155  if( !ierr ){
157  if(mFgtCollectionPtr)
158  mFgtCollectionPtr->Clear();
159 
160  //Short_t quadrant=0;
161  //Char_t layer=0;
162  //Double_t ordinate=0;
163  //Double_t lowerSpan=0;
164  //Double_t upperSpan=0;
165  Int_t rdo=0;
166  Int_t arm=0;
167  Int_t apv=0;
168  Int_t channel=0;
169  Short_t adc=0;
170  Short_t timebin=0;
171 
172  //char *ret =
173  mRdr->get(0,EVP_TYPE_ANY);
174  if(mRdr->status == EVP_STAT_EOR) {
175  LOG_DEBUG <<"End of File reached..."<<endm;
176  return kStEOF;
177  }
178 
179  daq_dta *dd = 0;
180  if(mDataType==0){
181  dd = mRdr->det("fgt")->get("adc");
182  if(!dd) dd = mRdr->det("fgt")->get("zs");
183  }else if(mDataType==1){
184  dd = mRdr->det("fgt")->get("adc");
185  }else if(mDataType==2){
186  dd = mRdr->det("fgt")->get("zs");
187  }else if(mDataType==3){
188  dd = mRdr->det("fgt")->get("zs");
189  if(!dd) dd = mRdr->det("fgt")->get("adc");
190  }
191 
192  int ntimebin=0;
193  if(dd && dd->meta){
194  apv_meta_t *meta = (apv_meta_t *)dd->meta;
195  for(int r=1;r<=FGT_RDO_COU;r++) {
196  if(meta->arc[r].present == 0) continue ;
197  for(int arm=0;arm<FGT_ARM_COU;arm++) {
198  if(meta->arc[r].arm[arm].present == 0) continue ;
199  for(int apv=0;apv<FGT_APV_COU;apv++) {
200  if(meta->arc[r].arm[arm].apv[apv].present == 0) continue ;
201  int nt=meta->arc[r].arm[arm].apv[apv].ntim;
202  //printf("RDO=%1d ARM=%1d APV=%02d Number of time bin =%d\n",r,arm,apv,nt);
203  if(ntimebin!=0 && nt!=0 && ntimebin!=nt) printf("Different number of timebins in different APV!!! Taking larger one!!!\n");
204  if(ntimebin<nt) ntimebin=nt;
205  }
206  }
207  }
208  }
209  mFgtCollectionPtr->setNumTimeBins(ntimebin);
210  if(mNumTbin < ntimebin) mFgtCollectionPtr->setNumTimeBins(mNumTbin);
211  //printf("Max Number of Timebin=%d\n",ntimebin);
212 
213  while(dd && dd->iterate()) {
214  fgt_adc_t *f = (fgt_adc_t *) dd->Void ;
215 
216  for(uint32_t i=0;i<dd->ncontent;i++) {
217  timebin=f[i].tb - mStartTbin;
218  if(timebin<0 || timebin>=mNumTbin) continue;
219 
220  channel=f[i].ch;
221  adc=f[i].adc;
222  arm=dd->sec;
223  apv=dd->pad;
224  rdo=dd->rdo;
225 
226  int flag=0;
227  //corrupted data in non existing channels
228  if(rdo<1 || rdo > kFgtNumRdos) flag=1;
229  if(arm<0 || arm>=kFgtNumArms) flag=1;
230  if(apv>=22 || apv < 0 || apv ==10|| apv==11) flag=1;
231  if(channel<0 || channel>=kFgtNumChannels) flag=1;
232  if(timebin<0 || timebin>=kFgtNumTimeBins) flag=1;
233  //LOG_INFO<< "rdo: " << rdo <<" arm: " << arm <<" apv: " << apv <<" channel: " << channel <<" tbin: "<<timebin<<endm;
234  if(flag==1){
235  LOG_INFO<< "Corrupt data rdo: " << rdo <<" arm: " << arm <<" apv: " << apv <<" channel: " << channel <<" tbin: "<<timebin<<endm;
236  continue;
237  }
238 
239 #if 0
240  // year 2012 exclusions -- adjusted (bug fixed) 02/29/12 by sgliske
241  // if( ( (rdo==1 && arm==1) || (rdo==2 && arm==2) || (rdo==1 && arm==4) ) && apv>4 && apv<10 ) continue;
242  // if( ( (rdo==2 && arm==1) || (rdo==1 && arm==3) || (rdo==2 && arm==4) ) && apv<5 ) continue;
243  // if( ( (rdo==2 && arm==1) || (rdo==1 && arm==3) || (rdo==2 && arm==4) ) && apv>16 ) continue;
244  // if( ( (rdo==1 && arm==2) || (rdo==2 && arm==3) || (rdo==2 && arm==4) ) && apv>9 && apv<17) continue;
245  // end of 2012 exclusions
246 #endif
247  Short_t discIdx=0; // will be set with getNaivePhysCoordFromElecCoord
248  Short_t quad, strip;
249  Char_t layer;
250  Int_t geoId = ( mIsCosmic
251  ? StFgtCosmicTestStandGeom::getNaiveGeoIdFromElecCoord(rdo,arm,apv,channel)
252  : fgtTables->getGeoIdFromElecCoord(rdo, arm, apv, channel)
253  );
254  StFgtGeom::decodeGeoId( geoId, discIdx, quad, layer, strip );
255  if(discIdx < 0 || discIdx >= kFgtNumDiscs) flag=1;
256  if(quad <0 || quad >= kFgtNumQuads) flag=1;
257  if(layer != 'P' && layer != 'R') flag=1;
258  if(strip < 0 || strip >= kFgtNumStrips) flag=1;
259  if(flag==1){
260  LOG_INFO<<"Wrong geoId: " << geoId <<" discIdx: " << discIdx << " quad: " << quad << " layer: " << layer <<" strip: " << strip
261  <<" is cosmic: " << mIsCosmic << " rdo: " << rdo <<" arm: " << arm <<" apv: " << apv <<" channel: " << channel <<endm;
262  continue;
263  }
264 
265  // cout << "AAA " << GetEventNumber() << " | " << rdo << ' ' << arm << ' ' << apv << ' ' << channel << " | " << geoId << ' ' << discIdx << ' ' << quad << ' ' << layer << ' ' << strip << ' '<<timebin<<endl;
266  /* DEBUGGING
267  if( timebin == 1 ){
268 
269  // ( mIsCosmic
270  // ? StFgtCosmicTestStandGeom::getNaiveElecCoordFromGeoId(geoId, rdo,arm,apv,channel)
271  // : fgtTables->getElecCoordFromGeoId(geoId, rdo, arm, apv, channel)
272  // );
273  // cout << " | " << rdo << ' ' << arm << ' ' << apv << ' ' << channel << endl;
274  };
275  */
276 
277  StFgtStripCollection *stripCollectionPtr = mFgtCollectionPtr->getStripCollection( discIdx );
278  if( stripCollectionPtr ) {
279  Int_t elecId = StFgtGeom::getElectIdFromElecCoord( rdo, arm, apv, channel );
280  StFgtStrip* stripPtr = stripCollectionPtr->getStrip( elecId );
281  stripPtr->setAdc( adc, timebin );
282  stripPtr->setGeoId( geoId );
283  stripPtr->setElecCoords( rdo, arm, apv, channel );
284  } else {
285  LOG_WARN << "StFgtRawDaqReader::Make() -- Could not access stripCollection for disc " << discIdx << endm;
286  };
287  };
288  };
289  };
290 
291  if( mCutShortEvents ){
292  // clear events that do not have a complete set of channels
293  Bool_t eventOK = 1;
294  UInt_t numDiscs = mFgtCollectionPtr->getNumDiscs();
295 
296  for( UInt_t discIdx = 0; discIdx < numDiscs && eventOK; ++discIdx ){
297  StFgtStripCollection *stripCollectionPtr = mFgtCollectionPtr->getStripCollection( discIdx );
298  if( stripCollectionPtr ){
299  Int_t chanPerQuad = kFgtApvsPerQuad*kFgtNumChannels; // 1280
300  Int_t remainder = stripCollectionPtr->getNumStrips() % chanPerQuad;
301  eventOK = (remainder == 0);
302  };
303  };
304  if( !eventOK ){
305  for( UInt_t discIdx = 0; discIdx < numDiscs && eventOK; ++discIdx ){
306  StFgtStripCollection *stripCollectionPtr = mFgtCollectionPtr->getStripCollection( discIdx );
307  if( stripCollectionPtr )
308  stripCollectionPtr->Clear();
309  };
310  };
311  };
312 
313  return ierr;
314 };
315 
316 void StFgtRawDaqReader::Clear( Option_t *opts )
317 {
318  if( mFgtCollectionPtr )
319  {
320  // LOG_DEBUG<<"info" <<endl;
321  // cout <<"clearing collectons... " <<endl;
322 
323  mFgtCollectionPtr->Clear( opts );
324  // cout <<"done " <<endl;
325  }
326 };
327 
328 
329 ClassImp(StFgtRawDaqReader);
330 
331 /*
332  * $Id: StFgtRawDaqReader.cxx,v 1.18 2013/03/19 03:24:19 akio Exp $
333  * $Log: StFgtRawDaqReader.cxx,v $
334  * Revision 1.18 2013/03/19 03:24:19 akio
335  * remove static from numtimebin
336  *
337  * Revision 1.17 2013/03/10 05:45:29 akio
338  * added option to limit timebins to feed rest of makers
339  *
340  * Revision 1.16 2013/03/03 04:59:12 akio
341  * less printing
342  *
343  * Revision 1.15 2013/02/21 20:30:26 akio
344  * added ZS data first option
345  *
346  * Revision 1.14 2013/02/06 18:10:54 akio
347  * getting date & time from data
348  *
349  * Revision 1.13 2013/01/31 20:44:55 akio
350  * Adding StFgtCollection::setNumTimeBins()
351  *
352  * Revision 1.12 2013/01/31 20:00:32 akio
353  * adding obtaining number of timebins from meta data
354  * adding options for zero suppressed data
355  *
356  * Revision 1.11 2012/11/26 15:20:35 akio
357  * remove some hardcoded numbers, and use StEnumeration
358  *
359  * Revision 1.10 2012/08/23 20:05:35 avossen
360  * commented out year 12 exclusions
361  *
362  * Revision 1.9 2012/03/07 15:23:52 sgliske
363  * StFgtStrip no longer has a type field
364  *
365  * Revision 1.8 2012/02/29 18:23:46 sgliske
366  * fixed bug in 2012 exclusions
367  *
368  * Revision 1.7 2012/02/07 05:33:25 balewski
369  * *** empty log message ***
370  *
371  * Revision 1.6 2012/02/06 04:17:32 balewski
372  * added 2012 APV exclusions
373  *
374  * Revision 1.5 2012/02/05 21:19:22 avossen
375  * added check for invalid elec coordinates
376  *
377  * Revision 1.4 2012/01/31 16:48:56 wwitzke
378  * Changed for cosmic test stand.
379  *
380  * Revision 1.3 2012/01/31 11:23:34 sgliske
381  * No longer requires passing name of StFgtDbMaker
382  *
383  * Revision 1.2 2012/01/31 09:16:55 sgliske
384  * fixed cvs caption
385  *
386  * Revision 1.1 2012/01/31 09:15:34 sgliske
387  * Moved to StFgtPool
388  *
389  */
virtual void AddData(TDataSet *data, const char *dir=".data")
User methods.
Definition: StMaker.cxx:332
virtual Int_t Make()
virtual void Clear(Option_t *opts="")
User defined functions.
Definition: Stypes.h:43
virtual const char * GetName() const
special overload
Definition: StMaker.cxx:237
Definition: Stypes.h:40
Definition: Stypes.h:41