StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EEqaSorter.cxx
1 // $Id: EEqaSorter.cxx,v 1.11 2010/04/09 04:48:33 ogrebeny Exp $
2 #include <string.h>
3 #include <stdlib.h>
4 //#include <iostream>
5 //using namespace std;
6 
7 #include <TObjArray.h>
8 
9 #include <TFile.h>
10 #include <TH1.h>
11 
12 #include "StMessMgr.h"
13 
14 #include "EEqaSorter.h"
15 
16 #include "StMuDSTMaker/EZTREE/EztEmcRawData.h"
17 #include "EEqaSorterA.h"
18 #include "EEqaSorterC.h" // needs DB
19 #include "EEdsmAna.h"
20 
21 #include "SpyCopyCat.h"
22 #include "SpyCorruption.h"
23 #include "SpyJPped.h"
24 #include "SpyJPfreq.h"
25 
26 ClassImp(EEqaSorter)
27 
28 //-------------------------------------------
30  : TObject()
31  , H4jpCor(0)
32  , sortA(0)
33  , sortC(0)
34  , dsm(0)
35  , eeDb(dbx)
36  , H1tot(0)
37  , pathInp(0)
38  , pathOut(0)
39  , timeStamp(0)
40  , mySpy(0)
41  , mySpyCC(0)
42  , nSpy(0)
43  , nSpyCC(0)
44  , minSecSpy(0)
45  , nEveSpy(0)
46  , spyMode(0)
47  , lastSpyRun(0)
48 {
49  memset(hCorT, 0, sizeof(hCorT));
50  memset(hCorS, 0, sizeof(hCorS));
51 }
52 
53 //--------------------------------------------------
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];
58  }
59  memset(hCorT, 0, sizeof(hCorT));
60  memset(hCorS, 0, sizeof(hCorS));
61  if (H4jpCor) delete H4jpCor;
62  if (sortA) delete sortA;
63  sortA = 0;
64  if (sortC) delete sortC;
65  sortC = 0;
66  if (dsm) delete dsm;
67  dsm = 0;
68  eeDb = 0;
69  if (H1tot) delete H1tot;
70  H1tot = 0;
71  pathInp = 0;
72  pathOut = 0;
73  timeStamp = 0;
74  for (int i = 0;i < nSpy;i++) if (mySpy && mySpy[i]) delete mySpy[i];
75  if (mySpy) delete mySpy;
76  mySpy = 0;
77  for (int i = 0;i < nSpyCC;i++) if (mySpyCC && mySpyCC[i]) delete mySpyCC[i];
78  if (mySpyCC) delete mySpyCC;
79  mySpyCC = 0;
80  nSpy = 0;
81  nSpyCC = 0;
82  minSecSpy = 0;
83  nEveSpy = 0;
84  spyMode = 0;
85  lastSpyRun = 0;
86 }
87 
88 //--------------------------------------------------
89 void EEqaSorter::initHisto(TObjArray *HList, int nBin, int mxADC) {
90  sortA=new EEqaSorterA();
91  sortC=new EEqaSorterC(eeDb);
92 
93  dsm = new EEdsmAna("allTrig");
94 
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);
99  //sortA->usePed4(Form("%s/eemcPed4.dat","/star/u/rfatemi/TestPPlots/home_local/eemc/defaultPanitkinSetup"));
100  sortA->usePed4(Form("%s/eemcPed4.dat", pathInp));
101 
102  sortC->initHisto(HList);
103 
104  if (dsm) dsm->initHisto(HList);
105 
106  //.................. my histo .............
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);
109 
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);
112 
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);
115 
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);
118 
119  hCorT[4]= new TH1F("ETowGhost","# ETOW nGhost/eve; chan / eve",50,.5,100.5);
120  hCorS[4]=0;
121 
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);
124 
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);
127 
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);
130 
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]);
134  }
135 
136  LOG_DEBUG << Form(" EEqaSorter::initHisto() total %d histos created\n", HList ? HList->GetEntries() : 0) << endl;
137 }
138 
139 //-------------------------------------------
140 void EEqaSorter::initRun() {
141  sortC->initRun();
142 }
143 
144 //-------------------------------------------
145 void EEqaSorter::clear() {
146  timeStamp=0;
147  if (dsm) dsm->clear();
148 }
149 
150 //-------------------------------------------
151 void EEqaSorter::saveHisto(TFile *f) const {
152  if (f) f->cd();
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();
161  }
162 }
163 
164 //-------------------------------------------
165 void EEqaSorter::sort(EztEmcRawData *t, EztEmcRawData *s, int runNo,
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)
169 {
170  H1tot->Fill(0.5);
171  xRayETOW(t, token); // detect corruption
172  xRayESMD(s, token,daqVer,runNo); // detect corruption
173 
174  crateHealth(t, hCorT, 1, daqVer); // histogram corruption
175  crateHealth(s, hCorS, 2, daqVer); // histogram corruption
176 
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);
180 }
181 
182 //-------------------------------------------
183 void EEqaSorter::xRayETOW(EztEmcRawData *t, int token) const {
184  // Goal:verify content of each header + tower data
185 
186  if (!t) return; // no ETOW data
187  int lenCount=0xa4;
188  int errFlag=0;
189  int trigComm=0x4; // physics, 9=laser/LED, 8=??
190 
191  const int mxN256=10; //per crate
192  const int mxGhost=2; //per crate
193  int totN256=0, totGhost=0;
194 
195  int icr;
196  for(icr=0;icr<t->getNBlocks();icr++) {
197  if(t->isCrateVoid(icr)) continue;
198  if(t->purgeCrateOFF(icr)) continue;
199 
200  // eETow->print(icr,0);
201  int crID=icr+1;
202  //........... hader..........
203  t->tagHeadValid(icr,token, crID,lenCount,trigComm,errFlag);
204  //............. ghost, N256 ...............
205  int i;
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++;
211  }
212  totGhost+=nGhost;
213  totN256+=n256;
214 
215  // ..... replace sanity bits
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);
220  //t->print(icr,0);
221  }
222  hCorT[2]->Fill(totN256);
223  hCorT[4]->Fill(totGhost);
224 }
225 
226 //-------------------------------------------
227 void EEqaSorter::xRayESMD(EztEmcRawData *s, int token, int ver, int runNo) const {
228  // Goal:verify content of each header
229 
230  if (!s) return; // no ESMD data
231  int icr;
232  int lenCount=0xc4;
233  int errFlag=0x28;
234  if(runNo>6001001) errFlag=0; // Gerard has reporogrammed FEE
235  int trigComm=0x4; // physics, 9=laser/LED, 8=??
236  const int mxN256=10; //per crate
237  int totN256=0;
238 
239  for(icr=0;icr<s->getNBlocks();icr++) {
240  if(s->isCrateVoid(icr)) continue;
241  if(s->purgeCrateOFF(icr)) continue;
242  // eESmd->print(icr,0);
243 
244  int crateID=icr+64;
245  // in 2004 there was only 16 MAPMT crates for sectors 5-8
246  if(ver<0x22) {
247  if(icr>=16) break;
248  crateID=icr+84;
249  }
250  //........... hader..........
251  s->tagHeadValid(icr,token, crateID,lenCount,trigComm,errFlag);
252  //............. N256 ...............
253  int i;
254  int n256=0;
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++;
258  }
259  totN256+=n256;
260 
261  UShort_t sanity=s->getCorruption(icr);
262  if(n256>mxN256) sanity|= EztEmcRawData::bitN256;
263  s->setCorruption(icr,sanity); // replace sanity bits
264  //s->print(icr,0);
265  }
266  hCorS[2]->Fill(totN256);
267 }
268 
269 //-------------------------------------------
270 void EEqaSorter::crateHealth(EztEmcRawData *eRaw, TH1F **h, int es, int ver) const {
271  if (!eRaw || !(es==1 || es==2)) return; // ETOW or ESMD
272 
273  int nN256=0, nGhost=0,nHeadCorr=0, nOFF=0;
274  int icr;
275  for(icr=0;icr<eRaw->getNBlocks();icr++) {
276  if(eRaw->sizeHeader(icr)<=0) continue; // crates not existing, only here scan also carte which are OFF==> isCrateVoid(icr)==true
277  // eETow->print(icr,0);
278  int crID=icr+1;
279  if(es==2) { // ESMD
280  crID=icr+64;
281  if(ver<0x22) {// in 2004 there was only 16 MAPMT crates for sectors 5-8
282  if(icr>=16) break;
283  crID=icr+84;
284  }
285  }
286  const UShort_t sanity=eRaw->getCorruption(icr);
287  if(eRaw->isCrateVoid(icr)){// case of 'crateOFF'
288  nOFF++;
289  h[6]->Fill(crID);
290  continue;
291  }
292 
293  if(es==1) { // ETOW
294  if(sanity & EztEmcRawData::bitGhost) nGhost++;
295  }
296 
297  if(sanity & 0x1f) nHeadCorr++; // any header bit
298  if(sanity & EztEmcRawData::bitN256) nN256++;
299 
300  // printf("aa es=%d icr=%d %d %d %d san=0x%x\n",es,icr,nHeadCorr,nN256,nGhost,sanity);
301  int i;
302  for(i=0;i<=6;i++) {// examin & histo all ETOW sanity bits
303  if(!(sanity&(1<<i))) continue; // bit not set
304  float x=crID+i/10.;
305  h[5]->Fill(x);
306  if(es==1)H4jpCor->Fill(crID); // any corruption per crate
307  }
308  } // end of loop over blocks
309 
310  if(nOFF>0) h[0]->Fill(0);
311 
312  int errKey=((nHeadCorr>0)<<0) | ((nN256>0)<<1)| ((nGhost>0)<<2);
313 
314  if(errKey>0) h[0]->Fill(errKey);
315  h[1]->Fill(nHeadCorr);
316  h[3]->Fill(nOFF);
317 
318 }
319 
320 //-------------------------------------------
321 void EEqaSorter::Finish() {
322  if (sortA) sortA->Finish();
323  //if (sortC) sortC->Finish();
324 }
325 
326 //-------------------------------------------
327 void EEqaSorter::saveHisto(const Char_t *filename) const {
328  TFile f(filename,"recreate");
329  if (f.IsOpen()) {
330  LOG_DEBUG << "Saving histograms to " << filename<< endm;
331  saveHisto(&f);
332  f.Close();
333  LOG_DEBUG << " , save Ok" << endm;
334  } else {
335  LOG_ERROR << "Cannot write to file " << filename << endm;
336  }
337 }
338 
339 //-------------------------------------------
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();
349  }
350 }
351 
352 //-------------------------------------------
353 void EEqaSorter::initSpy(const TObjArray *HList, int minSec, int mode) { // must be called after histos initialized
354  minSecSpy=minSec;
355  spyMode=mode;
356  int mxSpy=100;
357  mySpy=new SpyGeneric*[mxSpy];
358  mySpyCC=new SpyCopyCat*[mxSpy];
359  memset(mySpy, 0, sizeof(mySpy[0])*mxSpy);
360  memset(mySpyCC, 0, sizeof(mySpyCC[0])*mxSpy);
361  nSpy=nSpyCC=0;
362  int i;
363  char txt[100];
364  for(i=1;i<=6;i++) {
365  sprintf(txt,"JP%d_sum",i);
366  if (HList) mySpy[nSpy++]=(new SpyJPped)->set((TH1*)HList->FindObject(txt));
367  }
368 
369  if (HList) mySpy[nSpy++]=(new SpyJPfreq)->set((TH1*)HList->FindObject("JPtotFreq"));
370 
371  mySpy[nSpy++]=(new SpyCorruption)->set (hCorT[0],"ETOW corruption ");
372  mySpy[nSpy++]=(new SpyCorruption)->set (hCorS[0],"ESMD corruption ");
373 
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);
376 
377  for(i=0;i<nSpyCC;i++) {
378  mySpy[nSpy++]= mySpyCC[i];
379  }
380 
381  nEveSpy=0;
382  lastSpyRun=0;
383  LOG_DEBUG << Form("EEqaSorter::initSpy() nSpy=%d nSpyCC=%d, timeDelay=%d mode=%d\n",nSpy,nSpyCC,minSecSpy,spyMode) << endm;
384 }
385 
386 //-------------------------------------------
387 void EEqaSorter::spy(EztEmcRawData *t, EztEmcRawData *s, int runNo, int eveId) {
388  nEveSpy++;
389  static int lastTm=0;
390  static int lastNbad=0;
391  static int nMail=0; // # if mails per run
392  int uTm=time(0);
393  if( lastTm==0)lastTm=uTm;
394 
395  int i;
396  for(i=0;i<nSpyCC;i++) {
397  mySpyCC[i]->accumulate(t, s);
398  }
399 
400  //test hCorT[0]->Fill( 1+(nEveSpy%5));
401  //test hCorS[6]->Fill( 71+(nEveSpy%5));
402 
403  if(uTm-lastTm<minSecSpy && nEveSpy<200) return; //tmp, was 100
404 
405  //...................evaluate spy criteria
406  printf(" ACTING spy IN delT=%d\n",uTm-lastTm);
407  TArrayF a0;// accumulated
408  char txt[200];
409  char fname[200];
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");
415 
416  if(flog==0)return;
417  // flog=stdout; //test
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);
422 
423  lastTm=uTm;
424  int nBad=0;
425  if(lastSpyRun!=runNo) {
426  fprintf(flog,"run has changed from old=R%d, spy tests postponed\n",lastSpyRun);
427  lastSpyRun=runNo;
428  lastNbad=0;
429  nMail=0;
430  // printf(" ACTING spy new run, DO not replace array\n");
431  } else {
432  // printf(" ACTING spy doJob\n");
433  int i;
434  for(i=0;i<nSpy;i++) {
435  // printf("i=%d p=%p\n",i,mySpy[i]);
436  nBad+=mySpy[i]->sense(flog);
437  }
438  fprintf(flog,"\n grand total of %d ALARMS \n",nBad);
439  }
440 
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());
445  system(txt);
446 
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 ) {
451  nMail++;
452  system(txt);
453  lastNbad=nBad ;
454  }
455  nEveSpy=0; // clear # of sampled events
456 }
457 
458 
459 // $Log: EEqaSorter.cxx,v $
460 // Revision 1.11 2010/04/09 04:48:33 ogrebeny
461 // Added more protection against out-of-boundary errors. See bug report 1903.
462 //
463 // Revision 1.10 2009/03/05 02:37:53 ogrebeny
464 // Updates from Renee and Grant
465 //
466 // Revision 1.9 2009/02/24 04:07:45 ogrebeny
467 // Fixed part of the trigger histograms
468 //
469 // Revision 1.8 2009/02/14 03:16:52 ogrebeny
470 // Updated some histo titles. Removed unnecessary histo Clone() and Delete().
471 //
472 // Revision 1.7 2009/02/04 20:33:26 ogrebeny
473 // Moved the EEMC database functionality from StEEmcDbMaker to StEEmcUtil/database. See ticket http://www.star.bnl.gov/rt2/Ticket/Display.html?id=1388
474 //
475 // Revision 1.6 2009/01/24 03:47:43 ogrebeny
476 // Fuxed bug - can see ETOW data now
477 //
478 // Revision 1.5 2009/01/23 00:14:50 ogrebeny
479 // Inherited EEmcDb from StEEmcDbMaker to fix run-time bug http://www.star.bnl.gov/rt2/Ticket/Display.html?id=1378
480 //
481 // Revision 1.4 2009/01/18 01:01:28 ogrebeny
482 // Better separate EMC histogramming from OnlinePlots infrastructure
483 //
484 // Revision 1.3 2008/12/19 17:54:34 fine
485 // Disable the dummy class
486 //
487 // Revision 1.2 2005/05/05 22:22:08 balewski
488 // added spy for JP
489 //
490 // Revision 1.1 2005/04/28 20:54:46 balewski
491 // start
492 //
493