00001
00002 #include <string.h>
00003 #include <stdlib.h>
00004
00005
00006
00007 #include <TObjArray.h>
00008
00009 #include <TFile.h>
00010 #include <TH1.h>
00011
00012 #include "StMessMgr.h"
00013
00014 #include "EEqaSorter.h"
00015
00016 #include "StMuDSTMaker/EZTREE/EztEmcRawData.h"
00017 #include "EEqaSorterA.h"
00018 #include "EEqaSorterC.h"
00019 #include "EEdsmAna.h"
00020
00021 #include "SpyCopyCat.h"
00022 #include "SpyCorruption.h"
00023 #include "SpyJPped.h"
00024 #include "SpyJPfreq.h"
00025
00026 ClassImp(EEqaSorter)
00027
00028
00029 EEqaSorter::EEqaSorter(StEEmcDb *dbx)
00030 : TObject()
00031 , H4jpCor(0)
00032 , sortA(0)
00033 , sortC(0)
00034 , dsm(0)
00035 , eeDb(dbx)
00036 , H1tot(0)
00037 , pathInp(0)
00038 , pathOut(0)
00039 , timeStamp(0)
00040 , mySpy(0)
00041 , mySpyCC(0)
00042 , nSpy(0)
00043 , nSpyCC(0)
00044 , minSecSpy(0)
00045 , nEveSpy(0)
00046 , spyMode(0)
00047 , lastSpyRun(0)
00048 {
00049 memset(hCorT, 0, sizeof(hCorT));
00050 memset(hCorS, 0, sizeof(hCorS));
00051 }
00052
00053
00054 EEqaSorter::~EEqaSorter() {
00055 for (int i = 0;i <= 6;i++) {
00056 if(hCorT[i]) delete hCorT[i];
00057 if(hCorS[i]) delete hCorS[i];
00058 }
00059 memset(hCorT, 0, sizeof(hCorT));
00060 memset(hCorS, 0, sizeof(hCorS));
00061 if (H4jpCor) delete H4jpCor;
00062 if (sortA) delete sortA;
00063 sortA = 0;
00064 if (sortC) delete sortC;
00065 sortC = 0;
00066 if (dsm) delete dsm;
00067 dsm = 0;
00068 eeDb = 0;
00069 if (H1tot) delete H1tot;
00070 H1tot = 0;
00071 pathInp = 0;
00072 pathOut = 0;
00073 timeStamp = 0;
00074 for (int i = 0;i < nSpy;i++) if (mySpy && mySpy[i]) delete mySpy[i];
00075 if (mySpy) delete mySpy;
00076 mySpy = 0;
00077 for (int i = 0;i < nSpyCC;i++) if (mySpyCC && mySpyCC[i]) delete mySpyCC[i];
00078 if (mySpyCC) delete mySpyCC;
00079 mySpyCC = 0;
00080 nSpy = 0;
00081 nSpyCC = 0;
00082 minSecSpy = 0;
00083 nEveSpy = 0;
00084 spyMode = 0;
00085 lastSpyRun = 0;
00086 }
00087
00088
00089 void EEqaSorter::initHisto(TObjArray *HList, int nBin, int mxADC) {
00090 sortA=new EEqaSorterA();
00091 sortC=new EEqaSorterC(eeDb);
00092
00093 dsm = new EEdsmAna("allTrig");
00094
00095 LOG_DEBUG << Form(" EEqaSorter::initHisto(nb=%d, max=%d), pathInp=%s=\n", nBin,mxADC, pathInp) << endm;
00096 H1tot=new TH1F("eeTot","EEMC total counter",10,0,10);
00097 if (HList) HList->Add(H1tot);
00098 sortA->initCrateHisto(HList, nBin,mxADC);
00099
00100 sortA->usePed4(Form("%s/eemcPed4.dat", pathInp));
00101
00102 sortC->initHisto(HList);
00103
00104 if (dsm) dsm->initHisto(HList);
00105
00106
00107 hCorT[0]= new TH1F("ETowHealth","ETOW HEALTH; X=corr. type; events",9,-0.5,8.5);
00108 hCorS[0]= new TH1F("ESmdHealth","ESMD HEALTH; X=corr. type; events",9,-0.5,8.5);
00109
00110 hCorT[1]=new TH1F("ETowHeadCorr","# ETOW head corr; crates / eve",6,.5,6.5);
00111 hCorS[1]=new TH1F("ESmdHeadCorr","# ESMD head corr; crates / eve",48,.5,48.5);
00112
00113 hCorT[2]=new TH1F("ETowN256","# ETOW n256/eve; chan / eve",50, .5,100.5);
00114 hCorS[2]=new TH1F("ESmdN256","# ESMD n256/eve; chan / eve",50, .5,100.5);
00115
00116 hCorT[3]=new TH1F("ETowOFF","# ETOW OFF; crates / eve",6,.5,6.5);
00117 hCorS[3]=new TH1F("ESmdOFF","# ESMD OFF; crates / eve",48,.5,48.5);
00118
00119 hCorT[4]= new TH1F("ETowGhost","# ETOW nGhost/eve; chan / eve",50,.5,100.5);
00120 hCorS[4]=0;
00121
00122 hCorT[5]=new TH1F("ETowCorrBit","ETOW sanity; X= crID + bits/10: 0=crID, 1=token,2=len,3=trgCom,4=ErrFlg,5=Ghost,6=n256; events ",60,0.85,6.85);
00123 hCorS[5]=new TH1F("ESmdCorrBit","ESMD sanity; X= crID + bits/10: 0=crID, 1=token,2=len,3=trgCom,4=ErrFlg,5=Ghost,6=n256;events ",480,63.85,111.85);
00124
00125 hCorT[6]=new TH1F("ETowOFFid","ETOW OFF ID; crate ID",6,.5,6.5);
00126 hCorS[6]=new TH1F("ESmdOFFid","ESMD OFF ID; crate ID",48,63.5,111.5);
00127
00128 H4jpCor =new TH1F("JPtotCor","ETOW Corruption per JP; JP ID",EEnJetPatch,0.5,EEnJetPatch+0.5); H4jpCor->SetFillColor(kRed);
00129 if (HList) HList->Add(H4jpCor);
00130
00131 for(int i=0;i<=6;i++) {
00132 if(hCorT[i] && HList) HList->Add(hCorT[i]);
00133 if(hCorS[i] && HList) HList->Add(hCorS[i]);
00134 }
00135
00136 LOG_DEBUG << Form(" EEqaSorter::initHisto() total %d histos created\n", HList ? HList->GetEntries() : 0) << endl;
00137 }
00138
00139
00140 void EEqaSorter::initRun() {
00141 sortC->initRun();
00142 }
00143
00144
00145 void EEqaSorter::clear() {
00146 timeStamp=0;
00147 if (dsm) dsm->clear();
00148 }
00149
00150
00151 void EEqaSorter::saveHisto(TFile *f) const {
00152 if (f) f->cd();
00153 if (H1tot) H1tot->Write();
00154 if (sortA) sortA->saveHisto(f);
00155 if (sortC) sortC->saveHisto(f);
00156 if (dsm) dsm->saveHisto(f);
00157 if (H4jpCor) H4jpCor->Write();
00158 for(int i=0;i<=6;i++) {
00159 if(hCorT[i]) hCorT[i]->Write();
00160 if(hCorS[i]) hCorS[i]->Write();
00161 }
00162 }
00163
00164
00165 void EEqaSorter::sort(EztEmcRawData *t, EztEmcRawData *s, int runNo,
00166 int token, int daqVer,
00167 const unsigned char *dsm0inp, const unsigned short int *dsm1inp,
00168 const unsigned short int *dsm2inp, const unsigned short int *dsm3inp)
00169 {
00170 H1tot->Fill(0.5);
00171 xRayETOW(t, token);
00172 xRayESMD(s, token,daqVer,runNo);
00173
00174 crateHealth(t, hCorT, 1, daqVer);
00175 crateHealth(s, hCorS, 2, daqVer);
00176
00177 if (sortA) sortA->sort(t, s, daqVer);
00178 if (sortC) sortC->sort(t, s, daqVer);
00179 if (dsm) dsm->sort(dsm0inp, dsm1inp, dsm2inp, dsm3inp);
00180 }
00181
00182
00183 void EEqaSorter::xRayETOW(EztEmcRawData *t, int token) const {
00184
00185
00186 if (!t) return;
00187 int lenCount=0xa4;
00188 int errFlag=0;
00189 int trigComm=0x4;
00190
00191 const int mxN256=10;
00192 const int mxGhost=2;
00193 int totN256=0, totGhost=0;
00194
00195 int icr;
00196 for(icr=0;icr<t->getNBlocks();icr++) {
00197 if(t->isCrateVoid(icr)) continue;
00198 if(t->purgeCrateOFF(icr)) continue;
00199
00200
00201 int crID=icr+1;
00202
00203 t->tagHeadValid(icr,token, crID,lenCount,trigComm,errFlag);
00204
00205 int i;
00206 int nGhost=0, n256=0;
00207 const UShort_t* data=t->data(icr);
00208 for(i=0;i<t->sizeData(icr) && i<MaxTwCrateCh;i++) {
00209 if(data[i]>0 && (data[i] &0xff)==0 ) n256++;
00210 if(i>=121 && data[i]>50) nGhost++;
00211 }
00212 totGhost+=nGhost;
00213 totN256+=n256;
00214
00215
00216 UShort_t sanity=t->getCorruption(icr);
00217 if(nGhost>mxGhost) sanity |=EztEmcRawData::bitGhost;
00218 if(n256>mxN256) sanity |=EztEmcRawData::bitN256;
00219 t->setCorruption(icr,sanity);
00220
00221 }
00222 hCorT[2]->Fill(totN256);
00223 hCorT[4]->Fill(totGhost);
00224 }
00225
00226
00227 void EEqaSorter::xRayESMD(EztEmcRawData *s, int token, int ver, int runNo) const {
00228
00229
00230 if (!s) return;
00231 int icr;
00232 int lenCount=0xc4;
00233 int errFlag=0x28;
00234 if(runNo>6001001) errFlag=0;
00235 int trigComm=0x4;
00236 const int mxN256=10;
00237 int totN256=0;
00238
00239 for(icr=0;icr<s->getNBlocks();icr++) {
00240 if(s->isCrateVoid(icr)) continue;
00241 if(s->purgeCrateOFF(icr)) continue;
00242
00243
00244 int crateID=icr+64;
00245
00246 if(ver<0x22) {
00247 if(icr>=16) break;
00248 crateID=icr+84;
00249 }
00250
00251 s->tagHeadValid(icr,token, crateID,lenCount,trigComm,errFlag);
00252
00253 int i;
00254 int n256=0;
00255 const UShort_t* data=s->data(icr);
00256 for(i=0;i<s->sizeData(icr) && i<MaxMapmtCrateCh;i++) {
00257 if(data[i]>0 && (data[i] &0xff)==0 ) n256++;
00258 }
00259 totN256+=n256;
00260
00261 UShort_t sanity=s->getCorruption(icr);
00262 if(n256>mxN256) sanity|= EztEmcRawData::bitN256;
00263 s->setCorruption(icr,sanity);
00264
00265 }
00266 hCorS[2]->Fill(totN256);
00267 }
00268
00269
00270 void EEqaSorter::crateHealth(EztEmcRawData *eRaw, TH1F **h, int es, int ver) const {
00271 if (!eRaw || !(es==1 || es==2)) return;
00272
00273 int nN256=0, nGhost=0,nHeadCorr=0, nOFF=0;
00274 int icr;
00275 for(icr=0;icr<eRaw->getNBlocks();icr++) {
00276 if(eRaw->sizeHeader(icr)<=0) continue;
00277
00278 int crID=icr+1;
00279 if(es==2) {
00280 crID=icr+64;
00281 if(ver<0x22) {
00282 if(icr>=16) break;
00283 crID=icr+84;
00284 }
00285 }
00286 const UShort_t sanity=eRaw->getCorruption(icr);
00287 if(eRaw->isCrateVoid(icr)){
00288 nOFF++;
00289 h[6]->Fill(crID);
00290 continue;
00291 }
00292
00293 if(es==1) {
00294 if(sanity & EztEmcRawData::bitGhost) nGhost++;
00295 }
00296
00297 if(sanity & 0x1f) nHeadCorr++;
00298 if(sanity & EztEmcRawData::bitN256) nN256++;
00299
00300
00301 int i;
00302 for(i=0;i<=6;i++) {
00303 if(!(sanity&(1<<i))) continue;
00304 float x=crID+i/10.;
00305 h[5]->Fill(x);
00306 if(es==1)H4jpCor->Fill(crID);
00307 }
00308 }
00309
00310 if(nOFF>0) h[0]->Fill(0);
00311
00312 int errKey=((nHeadCorr>0)<<0) | ((nN256>0)<<1)| ((nGhost>0)<<2);
00313
00314 if(errKey>0) h[0]->Fill(errKey);
00315 h[1]->Fill(nHeadCorr);
00316 h[3]->Fill(nOFF);
00317
00318 }
00319
00320
00321 void EEqaSorter::Finish() {
00322 if (sortA) sortA->Finish();
00323
00324 }
00325
00326
00327 void EEqaSorter::saveHisto(const Char_t *filename) const {
00328 TFile f(filename,"recreate");
00329 if (f.IsOpen()) {
00330 LOG_DEBUG << "Saving histograms to " << filename<< endm;
00331 saveHisto(&f);
00332 f.Close();
00333 LOG_DEBUG << " , save Ok" << endm;
00334 } else {
00335 LOG_ERROR << "Cannot write to file " << filename << endm;
00336 }
00337 }
00338
00339
00340 void EEqaSorter::resetHisto() {
00341 if (H1tot) H1tot->Reset();
00342 if (sortA) sortA->resetHisto();
00343 if (sortC) sortC->resetHisto();
00344 if (dsm) dsm->resetHisto();
00345 if (H4jpCor) H4jpCor->Reset();
00346 for(int i=0;i<=6;i++) {
00347 if(hCorT[i]) hCorT[i]->Reset();
00348 if(hCorS[i]) hCorS[i]->Reset();
00349 }
00350 }
00351
00352
00353 void EEqaSorter::initSpy(const TObjArray *HList, int minSec, int mode) {
00354 minSecSpy=minSec;
00355 spyMode=mode;
00356 int mxSpy=100;
00357 mySpy=new SpyGeneric*[mxSpy];
00358 mySpyCC=new SpyCopyCat*[mxSpy];
00359 memset(mySpy, 0, sizeof(mySpy[0])*mxSpy);
00360 memset(mySpyCC, 0, sizeof(mySpyCC[0])*mxSpy);
00361 nSpy=nSpyCC=0;
00362 int i;
00363 char txt[100];
00364 for(i=1;i<=6;i++) {
00365 sprintf(txt,"JP%d_sum",i);
00366 if (HList) mySpy[nSpy++]=(new SpyJPped)->set((TH1*)HList->FindObject(txt));
00367 }
00368
00369 if (HList) mySpy[nSpy++]=(new SpyJPfreq)->set((TH1*)HList->FindObject("JPtotFreq"));
00370
00371 mySpy[nSpy++]=(new SpyCorruption)->set (hCorT[0],"ETOW corruption ");
00372 mySpy[nSpy++]=(new SpyCorruption)->set (hCorS[0],"ESMD corruption ");
00373
00374 for(i=0;i<6;i++) mySpyCC[nSpyCC++]=new SpyCopyCat('T',i);
00375 for(i=0;i<48;i++) mySpyCC[nSpyCC++]=new SpyCopyCat('M',i);
00376
00377 for(i=0;i<nSpyCC;i++) {
00378 mySpy[nSpy++]= mySpyCC[i];
00379 }
00380
00381 nEveSpy=0;
00382 lastSpyRun=0;
00383 LOG_DEBUG << Form("EEqaSorter::initSpy() nSpy=%d nSpyCC=%d, timeDelay=%d mode=%d\n",nSpy,nSpyCC,minSecSpy,spyMode) << endm;
00384 }
00385
00386
00387 void EEqaSorter::spy(EztEmcRawData *t, EztEmcRawData *s, int runNo, int eveId) {
00388 nEveSpy++;
00389 static int lastTm=0;
00390 static int lastNbad=0;
00391 static int nMail=0;
00392 int uTm=time(0);
00393 if( lastTm==0)lastTm=uTm;
00394
00395 int i;
00396 for(i=0;i<nSpyCC;i++) {
00397 mySpyCC[i]->accumulate(t, s);
00398 }
00399
00400
00401
00402
00403 if(uTm-lastTm<minSecSpy && nEveSpy<200) return;
00404
00405
00406 printf(" ACTING spy IN delT=%d\n",uTm-lastTm);
00407 TArrayF a0;
00408 char txt[200];
00409 char fname[200];
00410 sprintf(fname,"eeSpy.%d-%d",runNo,uTm);
00411 printf("EEqaSorter::spy() spyLog=%s=\n",fname);
00412 TString fullName=TString(pathOut)+fname;
00413 printf("fullName=%s=\n",fullName.Data());
00414 FILE *flog=fopen(fullName.Data(),"w");
00415
00416 if(flog==0)return;
00417
00418 if(spyMode!=3)fprintf(flog,"\n\n THIS IS TEST - IGNORE \n\n Jan\n\n");
00419 fprintf(flog,"BNL=%s" ,ctime((const time_t *)&uTm));
00420 fprintf(flog,"run R%d, last eveID=%d, \nsampled: %d eve in last %d seconds, total=%d eve\n",runNo,eveId,nEveSpy,uTm-lastTm, (int)H1tot->GetEntries());
00421 fprintf(flog,"unix time=%d\n" ,uTm);
00422
00423 lastTm=uTm;
00424 int nBad=0;
00425 if(lastSpyRun!=runNo) {
00426 fprintf(flog,"run has changed from old=R%d, spy tests postponed\n",lastSpyRun);
00427 lastSpyRun=runNo;
00428 lastNbad=0;
00429 nMail=0;
00430
00431 } else {
00432
00433 int i;
00434 for(i=0;i<nSpy;i++) {
00435
00436 nBad+=mySpy[i]->sense(flog);
00437 }
00438 fprintf(flog,"\n grand total of %d ALARMS \n",nBad);
00439 }
00440
00441 if(flog!=stdout) fclose(flog);
00442 TString fullName0=fullName;
00443 fullName+="-"; fullName+=nBad;
00444 sprintf(txt,"mv %s %s\n",fullName0.Data(),fullName.Data());
00445 system(txt);
00446
00447 if(nBad>0) printf(" EE-SPY ALARMING , logFile=%s-%d nBad=%d\n",fname,nBad,nBad);
00448 if(nBad>0 &&spyMode!=3 ) printf("\7\7n");
00449 sprintf(txt,"%s/macros/eeSpy.sh %s-%d %d %d&",pathInp,fname,nBad,nBad,spyMode);
00450 if(nBad>0 && nMail<3 && lastNbad!=nBad ) {
00451 nMail++;
00452 system(txt);
00453 lastNbad=nBad ;
00454 }
00455 nEveSpy=0;
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493