00001
00002
00003
00004
00005 #include "EmcFEM.h"
00006 #include "EmcDynamicData.h"
00007 #include "Eventiterator.h"
00008 #include "Event.h"
00009 #include "packetConstants.h"
00010
00011 #define LONG_FORMAT 808 // long format - 5 words for each of 192 channels per FEM
00012 #define SHORT_FORMAT 908 // short format - 3 words for each of 144 channels per FEM
00013
00014 #define OK 0x0 // no any problems.
00015 #define NO_PACKET 0x1 // current event has no packet from this FEM
00016 #define WRONG_CELL_NUMBER 0x2 // the delay between TAC and Pre samples or Post and Pre does
00017
00018 #define HG_PRE_TOO_LOW 0x4 // high gain pre-sample value is less then minimal acceptable.
00019 #define HG_POST_TOO_LOW 0x8 // high gain post-sample value is less then minimal acceptable.
00020 #define HG_PRE_TOO_HIGH 0x10 // high gain pre-sample value is higher then maximal acceptable.
00021 #define HG_POST_TOO_HIGH 0x20 // high gain post-sample value is higher then maximal acceptable.
00022
00023 #define LG_PRE_TOO_LOW 0x40 // low gain pre-sample value is less then minimal acceptable.
00024 #define LG_POST_TOO_LOW 0x80 // low gain post-sample value is less then minimal acceptable.
00025 #define LG_PRE_TOO_HIGH 0x100 // low gain pre-sample value is higher then maximal acceptable.
00026 #define LG_POST_TOO_HIGH 0x200 // low gain post-sample value is higher then maximal acceptable.
00027 #define TAC_TOO_HIGH 0x400 // TAC value is higher then maximal acceptable.
00028
00029
00030 EMCalFEE::EMCalFEE(EmcDynamicData* d, FEMlimits * l, int& status)
00031 {
00032 lim=l;
00033 dd=d;
00034 FEMstatus=NULL;
00035 DataErrors=NULL;
00036 RefErrors=NULL;
00037 EvtCells=NULL;
00038
00039 evtIn.evtLength=0;
00040 evtIn.evtType=0;
00041 evtIn.evtSequence=0;
00042 evtIn.evtRunNumber=0;
00043 event =0;
00044 FEMstatus=new int[dd->getnSM()];
00045 if(!FEMstatus)
00046 {
00047 status=1;
00048 return;
00049 }
00050 DataErrors=new int[dd->getEmcSize()];
00051 if(!DataErrors)
00052 {
00053 status=2;
00054 return;
00055 }
00056
00057 RefErrors=new int[dd->getRefSize()];
00058 if(!RefErrors)
00059 {
00060 status=3;
00061 return;
00062 }
00063 EvtCells=new cells[dd->getnSM()];
00064 if(!EvtCells)
00065 {
00066 status=4;
00067 return;
00068 }
00069 status=0;
00070 }
00071 EMCalFEE::~EMCalFEE()
00072 {
00073 delete [] FEMstatus;
00074 delete [] DataErrors;
00075 delete [] RefErrors;
00076 delete [] EvtCells;
00077 }
00078 int EMCalFEE::getNextEvent(Eventiterator *it)
00079 {
00080 if(event) delete event;
00081 int rv = 0 ;
00082 event = it->getNextEvent();
00083 if (event) {
00084 rv = processEvent(event) ;
00085 delete event ;
00086 event = 0;
00087 }
00088 return rv ;
00089 }
00090
00091 int EMCalFEE::readNextEvent(Eventiterator *it)
00092 {
00093 event = it->getNextEvent();
00094 return ((event)? event->getEvtSequence() : 0);
00095 }
00096
00097 int EMCalFEE::convertNextEvent()
00098 {
00099 if(!event) return 0;
00100 int rv = processEvent(event);
00101 delete event;
00102 event = 0;
00103 return rv;
00104 }
00105
00106 int EMCalFEE::processEvent(Event* evt)
00107 {
00108 int pre,post,i,j;
00109 Packet *p;
00110 int npack=0;
00111 int ok=0;
00112 int emcCh;
00113 int refCh;
00114 int iEmc=0;
00115 int iRef=0;
00116 int hitFormat=0;
00117 float** rEmc=dd->getEmcRaw();
00118 float** rRef=dd->getRefRaw();
00119
00120 evtIn.evtLength=evt->getEvtLength();
00121 evtIn.evtType=evt->getEvtType();
00122 evtIn.evtSequence=evt->getEvtSequence();
00123 evtIn.evtRunNumber=evt->getRunNumber();
00124 for(i=0;i<dd->getnSM();i++)
00125 {
00126 FEMstatus[i] = 0;
00127
00128 p=evt->getPacket((dd->getSmMap())[i].packet,IDEMC_OLDSTYLE);
00129
00130 if(p)
00131 {
00132 hitFormat=p->getHitFormat();
00133 npack++;
00134 EvtCells[i].tac = p->iValue(0,"AMU");
00135 EvtCells[i].pre = p->iValue(1,"AMU");
00136 EvtCells[i].post= p->iValue(2,"AMU");
00137
00138 if(((EvtCells[i].pre+(dd->getSmMap())[i].tac_pre)&0x3F)!=EvtCells[i].tac ||
00139 ((EvtCells[i].pre+(dd->getSmMap())[i].post_pre)&0x3F)!=EvtCells[i].post)
00140 {
00141 ok=2;
00142 FEMstatus[i] = ok;
00143 }
00144 else
00145 ok=0;
00146 }
00147 else
00148 {
00149 ok=1;
00150 FEMstatus[i] = ok;
00151 }
00152
00153 for(j=0;j<(dd->getSmMap())[i].nch;j++)
00154 {
00155 iEmc=(dd->getSmMap())[i].startTad+j;
00156 DataErrors[iEmc]=0;
00157 emcCh=(dd->getSmMap())[i].femCh[j];
00158 if(hitFormat==SHORT_FORMAT)
00159 {
00160 if((emcCh%16)>11)emcCh=144;
00161 else emcCh=emcCh-emcCh/16*4;
00162 }
00163
00164 if(ok!=1)
00165 {
00166 if(rEmc)
00167 {
00168
00169 for(int ra=0;ra<5;ra++)
00170 {
00171 rEmc[ra][iEmc]=p->iValue(emcCh,ra);
00172
00173 }
00174
00175 }
00176
00177
00178 pre=p->iValue(emcCh,3);
00179 post=p->iValue(emcCh,1);
00180 if(pre<lim->minAmp){DataErrors[iEmc]|=HG_PRE_TOO_LOW;}
00181 if(post<lim->minAmp){DataErrors[iEmc]|=HG_POST_TOO_LOW;}
00182 if(pre>lim->maxAmp){DataErrors[iEmc]|=HG_PRE_TOO_HIGH;}
00183 if(post>lim->maxAmp){DataErrors[iEmc]|=HG_POST_TOO_HIGH;}
00184 (dd->getEmcHG())[iEmc]=float(pre-post);
00185
00186
00187 pre=p->iValue(emcCh,4);
00188 post=p->iValue(emcCh,2);
00189 if(pre<lim->minAmp)(DataErrors[iEmc])|=LG_PRE_TOO_LOW;
00190 if(post<lim->minAmp)(DataErrors[iEmc])|=LG_POST_TOO_LOW;
00191 if(pre>lim->maxAmp)(DataErrors[iEmc])|=LG_PRE_TOO_HIGH;
00192 if(post>lim->maxAmp)(DataErrors[iEmc])|=LG_POST_TOO_HIGH;
00193 (dd->getEmcLG())[iEmc]=float(pre-post);
00194
00195
00196 (dd->getEmcTAC())[iEmc]=float(p->iValue(emcCh,0));
00197 if(p->iValue(emcCh,0)>lim->maxAmp) (DataErrors[iEmc])|= TAC_TOO_HIGH;
00198 }
00199 else
00200 {
00201 (DataErrors[iEmc])|=ok;
00202 (dd->getEmcHG())[iEmc]=0.;
00203 (dd->getEmcLG())[iEmc]=0.;
00204 (dd->getEmcTAC())[iEmc]=0.;
00205 }
00206
00207 }
00208
00209 for(j=0;j<(dd->getSmMap())[i].nrefCh;j++)
00210 {
00211 iRef=(dd->getSmMap())[i].startRad+j;
00212 refCh=(dd->getSmMap())[i].refCh[j];
00213 if(hitFormat==SHORT_FORMAT)
00214 {
00215 refCh=refCh-refCh/16*4;
00216 if((refCh%16)>11)refCh=-1;
00217 }
00218
00219 if(ok!=1)
00220 {
00221 if(rRef)
00222 {
00223 for(int rf=0;rf<5;rf++)
00224 rRef[rf][iRef]=p->iValue(refCh,rf);
00225 }
00226 pre= p->iValue(refCh,3);
00227 post=p->iValue(refCh,1);
00228 if(pre<lim->minAmp)(RefErrors[iRef])|=HG_PRE_TOO_LOW;
00229 if(post<lim->minAmp)(RefErrors[iRef])|=HG_POST_TOO_LOW;
00230 if(pre>lim->maxAmp)(RefErrors[iRef])|=HG_PRE_TOO_HIGH;
00231 if(post>lim->maxAmp)(RefErrors[iRef])|=HG_POST_TOO_HIGH;
00232 (dd->getRefHG())[iRef]=float(pre-post);
00233
00234 pre=p->iValue(refCh,4);
00235 post=p->iValue(refCh,2);
00236 if(pre<lim->minAmp)(RefErrors[iRef])|=LG_PRE_TOO_LOW;
00237 if(post<lim->minAmp)(RefErrors[iRef])|=LG_POST_TOO_LOW;
00238 if(pre>lim->maxAmp)(RefErrors[iRef])|=LG_PRE_TOO_HIGH;
00239 if(post>lim->maxAmp)(RefErrors[iRef])|=LG_POST_TOO_HIGH;
00240 (dd->getRefLG())[iRef]=float(pre-post);
00241
00242 (dd->getRefTAC())[iRef]=float(p->iValue(refCh,0));
00243 if(p->iValue(refCh,0)>lim->maxAmp) (RefErrors[iRef])|= TAC_TOO_HIGH;
00244 }
00245 else
00246 {
00247 (RefErrors[iRef])|=ok;
00248 (dd->getRefHG())[iRef]=0.;
00249 (dd->getRefLG())[iRef]=0.;
00250 (dd->getRefTAC())[iRef]=0.;
00251 }
00252 }
00253
00254 if(p)delete p;
00255 }
00256
00257
00258 if(!npack)return -1;
00259 return 1;
00260 }
00261