12 #include "StMessMgr.h"
14 #include "EEqaSorter.h"
16 #include "StMuDSTMaker/EZTREE/EztEmcRawData.h"
17 #include "EEqaSorterA.h"
18 #include "EEqaSorterC.h"
21 #include "SpyCopyCat.h"
22 #include "SpyCorruption.h"
24 #include "SpyJPfreq.h"
49 memset(hCorT, 0,
sizeof(hCorT));
50 memset(hCorS, 0,
sizeof(hCorS));
54 EEqaSorter::~EEqaSorter() {
55 for (
int i = 0;i <= 6;i++) {
56 if(hCorT[i])
delete hCorT[i];
57 if(hCorS[i])
delete hCorS[i];
59 memset(hCorT, 0,
sizeof(hCorT));
60 memset(hCorS, 0,
sizeof(hCorS));
61 if (H4jpCor)
delete H4jpCor;
62 if (sortA)
delete sortA;
64 if (sortC)
delete sortC;
69 if (H1tot)
delete H1tot;
74 for (
int i = 0;i < nSpy;i++)
if (mySpy && mySpy[i])
delete mySpy[i];
75 if (mySpy)
delete mySpy;
77 for (
int i = 0;i < nSpyCC;i++)
if (mySpyCC && mySpyCC[i])
delete mySpyCC[i];
78 if (mySpyCC)
delete mySpyCC;
89 void EEqaSorter::initHisto(TObjArray *HList,
int nBin,
int mxADC) {
95 LOG_DEBUG << Form(
" EEqaSorter::initHisto(nb=%d, max=%d), pathInp=%s=\n", nBin,mxADC, pathInp) << endm;
96 H1tot=
new TH1F(
"eeTot",
"EEMC total counter",10,0,10);
97 if (HList) HList->Add(H1tot);
98 sortA->initCrateHisto(HList, nBin,mxADC);
100 sortA->usePed4(Form(
"%s/eemcPed4.dat", pathInp));
102 sortC->initHisto(HList);
104 if (dsm) dsm->initHisto(HList);
107 hCorT[0]=
new TH1F(
"ETowHealth",
"ETOW HEALTH; X=corr. type; events",9,-0.5,8.5);
108 hCorS[0]=
new TH1F(
"ESmdHealth",
"ESMD HEALTH; X=corr. type; events",9,-0.5,8.5);
110 hCorT[1]=
new TH1F(
"ETowHeadCorr",
"# ETOW head corr; crates / eve",6,.5,6.5);
111 hCorS[1]=
new TH1F(
"ESmdHeadCorr",
"# ESMD head corr; crates / eve",48,.5,48.5);
113 hCorT[2]=
new TH1F(
"ETowN256",
"# ETOW n256/eve; chan / eve",50, .5,100.5);
114 hCorS[2]=
new TH1F(
"ESmdN256",
"# ESMD n256/eve; chan / eve",50, .5,100.5);
116 hCorT[3]=
new TH1F(
"ETowOFF",
"# ETOW OFF; crates / eve",6,.5,6.5);
117 hCorS[3]=
new TH1F(
"ESmdOFF",
"# ESMD OFF; crates / eve",48,.5,48.5);
119 hCorT[4]=
new TH1F(
"ETowGhost",
"# ETOW nGhost/eve; chan / eve",50,.5,100.5);
122 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);
123 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);
125 hCorT[6]=
new TH1F(
"ETowOFFid",
"ETOW OFF ID; crate ID",6,.5,6.5);
126 hCorS[6]=
new TH1F(
"ESmdOFFid",
"ESMD OFF ID; crate ID",48,63.5,111.5);
128 H4jpCor =
new TH1F(
"JPtotCor",
"ETOW Corruption per JP; JP ID",EEnJetPatch,0.5,EEnJetPatch+0.5); H4jpCor->SetFillColor(kRed);
129 if (HList) HList->Add(H4jpCor);
131 for(
int i=0;i<=6;i++) {
132 if(hCorT[i] && HList) HList->Add(hCorT[i]);
133 if(hCorS[i] && HList) HList->Add(hCorS[i]);
136 LOG_DEBUG << Form(
" EEqaSorter::initHisto() total %d histos created\n", HList ? HList->GetEntries() : 0) << endl;
140 void EEqaSorter::initRun() {
145 void EEqaSorter::clear() {
147 if (dsm) dsm->clear();
151 void EEqaSorter::saveHisto(TFile *f)
const {
153 if (H1tot) H1tot->Write();
154 if (sortA) sortA->saveHisto(f);
155 if (sortC) sortC->saveHisto(f);
156 if (dsm) dsm->saveHisto(f);
157 if (H4jpCor) H4jpCor->Write();
158 for(
int i=0;i<=6;i++) {
159 if(hCorT[i]) hCorT[i]->Write();
160 if(hCorS[i]) hCorS[i]->Write();
166 int token,
int daqVer,
167 const unsigned char *dsm0inp,
const unsigned short int *dsm1inp,
168 const unsigned short int *dsm2inp,
const unsigned short int *dsm3inp)
172 xRayESMD(s, token,daqVer,runNo);
174 crateHealth(t, hCorT, 1, daqVer);
175 crateHealth(s, hCorS, 2, daqVer);
177 if (sortA) sortA->sort(t, s, daqVer);
178 if (sortC) sortC->sort(t, s, daqVer);
179 if (dsm) dsm->sort(dsm0inp, dsm1inp, dsm2inp, dsm3inp);
183 void EEqaSorter::xRayETOW(
EztEmcRawData *t,
int token)
const {
193 int totN256=0, totGhost=0;
196 for(icr=0;icr<t->getNBlocks();icr++) {
197 if(t->isCrateVoid(icr))
continue;
198 if(t->purgeCrateOFF(icr))
continue;
203 t->tagHeadValid(icr,token, crID,lenCount,trigComm,errFlag);
206 int nGhost=0, n256=0;
207 const UShort_t*
data=t->data(icr);
208 for(i=0;i<t->sizeData(icr) && i<MaxTwCrateCh;i++) {
209 if(data[i]>0 && (data[i] &0xff)==0 ) n256++;
210 if(i>=121 && data[i]>50) nGhost++;
216 UShort_t sanity=t->getCorruption(icr);
217 if(nGhost>mxGhost) sanity |=EztEmcRawData::bitGhost;
218 if(n256>mxN256) sanity |=EztEmcRawData::bitN256;
219 t->setCorruption(icr,sanity);
222 hCorT[2]->Fill(totN256);
223 hCorT[4]->Fill(totGhost);
227 void EEqaSorter::xRayESMD(
EztEmcRawData *s,
int token,
int ver,
int runNo)
const {
234 if(runNo>6001001) errFlag=0;
239 for(icr=0;icr<s->getNBlocks();icr++) {
240 if(s->isCrateVoid(icr))
continue;
241 if(s->purgeCrateOFF(icr))
continue;
251 s->tagHeadValid(icr,token, crateID,lenCount,trigComm,errFlag);
255 const UShort_t* data=s->data(icr);
256 for(i=0;i<s->sizeData(icr) && i<MaxMapmtCrateCh;i++) {
257 if(data[i]>0 && (data[i] &0xff)==0 ) n256++;
261 UShort_t sanity=s->getCorruption(icr);
262 if(n256>mxN256) sanity|= EztEmcRawData::bitN256;
263 s->setCorruption(icr,sanity);
266 hCorS[2]->Fill(totN256);
270 void EEqaSorter::crateHealth(
EztEmcRawData *eRaw, TH1F **h,
int es,
int ver)
const {
271 if (!eRaw || !(es==1 || es==2))
return;
273 int nN256=0, nGhost=0,nHeadCorr=0, nOFF=0;
275 for(icr=0;icr<eRaw->getNBlocks();icr++) {
276 if(eRaw->sizeHeader(icr)<=0)
continue;
286 const UShort_t sanity=eRaw->getCorruption(icr);
287 if(eRaw->isCrateVoid(icr)){
294 if(sanity & EztEmcRawData::bitGhost) nGhost++;
297 if(sanity & 0x1f) nHeadCorr++;
298 if(sanity & EztEmcRawData::bitN256) nN256++;
303 if(!(sanity&(1<<i)))
continue;
306 if(es==1)H4jpCor->Fill(crID);
310 if(nOFF>0) h[0]->Fill(0);
312 int errKey=((nHeadCorr>0)<<0) | ((nN256>0)<<1)| ((nGhost>0)<<2);
314 if(errKey>0) h[0]->Fill(errKey);
315 h[1]->Fill(nHeadCorr);
321 void EEqaSorter::Finish() {
322 if (sortA) sortA->Finish();
327 void EEqaSorter::saveHisto(
const Char_t *filename)
const {
328 TFile f(filename,
"recreate");
330 LOG_DEBUG <<
"Saving histograms to " << filename<< endm;
333 LOG_DEBUG <<
" , save Ok" << endm;
335 LOG_ERROR <<
"Cannot write to file " << filename << endm;
340 void EEqaSorter::resetHisto() {
341 if (H1tot) H1tot->Reset();
342 if (sortA) sortA->resetHisto();
343 if (sortC) sortC->resetHisto();
344 if (dsm) dsm->resetHisto();
345 if (H4jpCor) H4jpCor->Reset();
346 for(
int i=0;i<=6;i++) {
347 if(hCorT[i]) hCorT[i]->Reset();
348 if(hCorS[i]) hCorS[i]->Reset();
353 void EEqaSorter::initSpy(
const TObjArray *HList,
int minSec,
int mode) {
359 memset(mySpy, 0,
sizeof(mySpy[0])*mxSpy);
360 memset(mySpyCC, 0,
sizeof(mySpyCC[0])*mxSpy);
365 sprintf(txt,
"JP%d_sum",i);
366 if (HList) mySpy[nSpy++]=(
new SpyJPped)->set((TH1*)HList->FindObject(txt));
369 if (HList) mySpy[nSpy++]=(
new SpyJPfreq)->set((TH1*)HList->FindObject(
"JPtotFreq"));
371 mySpy[nSpy++]=(
new SpyCorruption)->set (hCorT[0],
"ETOW corruption ");
372 mySpy[nSpy++]=(
new SpyCorruption)->set (hCorS[0],
"ESMD corruption ");
374 for(i=0;i<6;i++) mySpyCC[nSpyCC++]=
new SpyCopyCat(
'T',i);
375 for(i=0;i<48;i++) mySpyCC[nSpyCC++]=
new SpyCopyCat(
'M',i);
377 for(i=0;i<nSpyCC;i++) {
378 mySpy[nSpy++]= mySpyCC[i];
383 LOG_DEBUG << Form(
"EEqaSorter::initSpy() nSpy=%d nSpyCC=%d, timeDelay=%d mode=%d\n",nSpy,nSpyCC,minSecSpy,spyMode) << endm;
390 static int lastNbad=0;
393 if( lastTm==0)lastTm=uTm;
396 for(i=0;i<nSpyCC;i++) {
397 mySpyCC[i]->accumulate(t, s);
403 if(uTm-lastTm<minSecSpy && nEveSpy<200)
return;
406 printf(
" ACTING spy IN delT=%d\n",uTm-lastTm);
410 sprintf(fname,
"eeSpy.%d-%d",runNo,uTm);
411 printf(
"EEqaSorter::spy() spyLog=%s=\n",fname);
412 TString fullName=TString(pathOut)+fname;
413 printf(
"fullName=%s=\n",fullName.Data());
414 FILE *flog=fopen(fullName.Data(),
"w");
418 if(spyMode!=3)fprintf(flog,
"\n\n THIS IS TEST - IGNORE \n\n Jan\n\n");
419 fprintf(flog,
"BNL=%s" ,ctime((
const time_t *)&uTm));
420 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());
421 fprintf(flog,
"unix time=%d\n" ,uTm);
425 if(lastSpyRun!=runNo) {
426 fprintf(flog,
"run has changed from old=R%d, spy tests postponed\n",lastSpyRun);
434 for(i=0;i<nSpy;i++) {
436 nBad+=mySpy[i]->sense(flog);
438 fprintf(flog,
"\n grand total of %d ALARMS \n",nBad);
441 if(flog!=stdout) fclose(flog);
442 TString fullName0=fullName;
443 fullName+=
"-"; fullName+=nBad;
444 sprintf(txt,
"mv %s %s\n",fullName0.Data(),fullName.Data());
447 if(nBad>0) printf(
" EE-SPY ALARMING , logFile=%s-%d nBad=%d\n",fname,nBad,nBad);
448 if(nBad>0 &&spyMode!=3 ) printf(
"\7\7n");
449 sprintf(txt,
"%s/macros/eeSpy.sh %s-%d %d %d&",pathInp,fname,nBad,nBad,spyMode);
450 if(nBad>0 && nMail<3 && lastNbad!=nBad ) {