StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StEmcPedestalMaker.cxx
1 #include "StEmcPedestalMaker.h"
2 
3 #include <fstream>
4 using namespace std;
5 
6 #include <TFile.h>
7 #include <TROOT.h>
8 #include <TF1.h>
9 
10 #include <StEventTypes.h>
11 #include <StEvent.h>
12 #include <StEmcUtil/geometry/StEmcGeom.h>
13 #include <StDbLib/StDbManager.hh>
14 #include <StDbLib/StDbTable.h>
15 #include <StDbLib/StDbConfigNode.hh>
16 #include <tables/St_emcPed_Table.h>
17 #include <tables/St_smdPed_Table.h>
18 #include <StDaqLib/EMC/StEmcDecoder.h>
19 
20 ClassImp(StEmcPedestalMaker)
21 
22 //_____________________________________________________________________________
23 StEmcPedestalMaker::StEmcPedestalMaker(const Char_t *name)
24  : StEmcCalibMaker(name)
25  {
26  setRange(300);
27  setMaxTracks(100);
28  mLastPedDate = 2000;
29  mLastPedTime = 0;
30 
31  setNPedEvents(2000);
32  setSaveTables(false);
33  setPedDiffSaveDB(2.0);
34  setPedDiffSaveNum(3);
35  setPedDiffSaveMinTime(60 * 60 * 24);
36  setCompareLastTableDB(false);
37  setPedCrateFilenameFormat("pedestal_crate0x%02x.dat");
38  setBemcStatusFilename("bemcStatus.txt");
39  setUseBemcStatus(true);
40 }
41 //_____________________________________________________________________________
42 StEmcPedestalMaker::~StEmcPedestalMaker()
43 {
44 }
45 //_____________________________________________________________________________
46 Int_t StEmcPedestalMaker::Init()
47 {
48  mPedestal = new TH1F("mPed","",getNChannel(),0.5,getNChannel()+0.5);
49  mRms = new TH1F("mRms","",getNChannel(),0.5,getNChannel()+0.5);
50  mChi = new TH1F("mChi","",getNChannel(),0.5,getNChannel()+0.5);
51  mStatus = new TH1F("mStatus","",getNChannel(),0.5,getNChannel()+0.5);
52 
53  mSpecName = "mSpecPed";
54  mAcceptName = "mAcceptPed";
55 
56  mStarted = false;
57 
58  return StEmcCalibMaker::Init();
59 }
60 //_____________________________________________________________________________
61 void StEmcPedestalMaker::Clear(Option_t *option)
62 {
63 }
64 //_____________________________________________________________________________
66 {
67  if(!accept()) return kStOk;
68 
69  if(!mStarted)
70  {
71  if(getTimeInterval(mLastPedDate,mLastPedTime)>mPedInterval)
72  {
73  mLastPedDate = getDate();
74  mLastPedTime = getTime();
75  mStarted = true;
76  reset();
77  }
78  else
79  {
80  if(isDebug()) cout <<"Time remaining for a new pedestal run is "
81  <<mPedInterval-getTimeInterval(mLastPedDate,mLastPedTime)
82  <<" hours for detector number "<<getDetector()<<endl;
83  }
84  }
85 
86  if(!mStarted) return kStOk;
87  //cout <<"Started\n";
88  for(int i=0;i<mNChannel;i++)
89  {
90  int id = i+1;
91  float adc = (float) getCalib()->getADC(mDetector,id);
92  //cout <<"Detector = "<<mDetector<<" id = "<<id<<" adc = "<<adc<<endl;
93  if(adc!=0) fill(id,adc);
94  }
95 
96  if(getNEvents()>getNPedEvents())
97  {
98  calcPedestals();
99  savePedestals(mLastPedDate,mLastPedTime,isAutoSaveDB());
100  if(isAutoSaveDB() || getSaveTables()) saveToDb(mLastPedDate,mLastPedTime);
101  mStarted = false;
102  }
103 
104  return kStOK;
105 }
106 //_____________________________________________________________________________
108 {
109  return StMaker::Finish();
110 }
111 //_____________________________________________________________________________
112 void StEmcPedestalMaker::calcPedestals()
113 {
114  cout <<"***** Calculating pedestals for detector "<<getDetector()<<" ...\n";
115  mPedestal->Reset();
116  mRms->Reset();
117  mChi->Reset();
118  mStatus->Reset();
119  float left = 3;
120  float right= 2;
121 
122  int ngood=0,nped=0,nrms=0,nchi=0,nbad=0,nodata=0;
123 
124  for(int id = 1;id<=getNChannel();id++) {
125  TH1D *h = getSpec(id);
126  int ibin = h->GetMaximumBin();
127  float avg = (float)h->GetBinCenter(ibin);
128  float max = (float)h->GetMaximum();
129  float rms = 1.5;
130  if(getDetector()>2) rms = h->GetRMS();
131  float integral = (float)h->Integral();
132  if((max!=0) && (integral > (getNPedEvents() / 2.0))) {
133  TF1 func("ped","gaus(0)");
134  float rmsInit = rms;
135  func.SetParameter(0,max);
136  func.SetParameter(1,avg);
137  func.SetParameter(2,rms);
138  func.SetParLimits(2,0,100000);
139  float seed = avg;
140  float fitleft = avg-left*rms;
141  if(fitleft<0) fitleft = 0;
142  float fitright = avg+right*rms;
143  func.SetRange(fitleft,fitright);
144 
145  int npt = (Int_t)((left+right+1.0)*rms);
146  int ndg = (Int_t)((float)npt-3.0);
147 
148  h->Fit(&func,"RQN"); // pre fit
149  max = func.GetParameter(0);
150  avg = func.GetParameter(1);
151  rms = func.GetParameter(2);
152  int status = 1; // data present
153  float chi = func.GetChisquare()/(float)ndg;
154  float res = avg-seed;
155 
156  if(avg<0) {status+= 2; nped++; avg = 0;}// negative pedestal
157  if(rms<0 || rms >7*rmsInit) {status+= 4; nrms++;}// bad rms
158  if(fabs(res)>1.5*rms) {status+= 8; nchi++;}// large distance to seed
159  if(status==1) ngood++; else nbad++;
160  mPedestal->Fill((float)id,avg);
161  mRms->Fill((float)id,rms);
162  mChi->Fill((float)id,chi);
163  mStatus->Fill((float)id,(float)status);
164  if(status>1) cout <<"det = "<<getDetector()<<" id = "<<id <<" max = "<<seed<<" initRms = "<<rmsInit
165  <<" peakY = "<<max
166  <<" ped = "<<avg <<" res = " <<res<<" rms = "<<rms
167  <<" chi = "<<chi<<" status = "<<status<<endl;
168  } else {
169  mPedestal->Fill((float)id,0);
170  mRms->Fill((float)id,0);
171  mChi->Fill((float)id,0);
172  mStatus->Fill((float)id,0);
173  nodata++;
174  nbad++;
175  cout << "det = " << getDetector() << " id = " << id << " peakY = "<< max
176  << " ped = " << avg << " rms = " << rms
177  << " int = " << integral << endl;
178  }
179  if(h) {delete h; h = 0;}
180  }
181  cout <<"nGood = "<<ngood<<"; nBad = "<<nbad<<": neg Ped = "<<nped<<", bad rms = "<<nrms<<", large res = "<<nchi << ", no data = " << nodata <<endl;
182 }
183 //_____________________________________________________________________________
184 void StEmcPedestalMaker::saveToDb(const Char_t *timeStamp, const Char_t *tableFilename) const {
185  cout << "=================================================" << endl;
186  cout << "Saving pedestal table for detector " << getDetector() << endl;
187  cout << "TimeStamp = " << timeStamp << endl;
188 
189  TString n[] = {"bemcPed", "bprsPed", "bsmdePed", "bsmdpPed"};
190 
191  Bool_t saveThisTable = true;
192  TString lastTableFilename = getLastTablePath();
193  lastTableFilename += "/";
194  lastTableFilename += n[getDetector() - 1];
195  lastTableFilename += ".last";
196  TString lastTableTimestampFilename = lastTableFilename + ".timestamp";
197  lastTableFilename += ".root";
198 
199  if (getCompareLastTableDB()) {
200  cout << "Comparing to the last table file " << lastTableFilename << endl;
201  TFile *lastTableFile = new TFile(lastTableFilename);
202  if (lastTableFile && lastTableFile->IsOpen()) {
203  cout << "Opened last table file " << lastTableFilename << endl;
204  saveThisTable = false;
205  ifstream ifstr_timestamp(lastTableTimestampFilename);
206  if (ifstr_timestamp.good()) {
207  Char_t lastTableTimestampStr[2048];
208  ifstr_timestamp.getline(lastTableTimestampStr, 2048);
209  ifstr_timestamp.close();
210  TDatime tsLast(&lastTableTimestampStr[0]);
211  TDatime tsCurrent(timeStamp);
212  UInt_t timeLast = tsLast.Convert();
213  UInt_t timeCurrent = tsCurrent.Convert();
214  Double_t diffTime = difftime(timeCurrent, timeLast);
215  cout << "Timestamps: Last: " << lastTableTimestampStr << ", " << tsLast.AsSQLString() << endl;
216  cout << "Timestamps: Current: " << timeStamp << ", " << tsCurrent.AsSQLString() << "; Diff: " << diffTime << endl;
217  if (diffTime > this->getPedDiffSaveMinTime()) {
218  saveThisTable = true;
219  cout << "Saving this table because min time passed" << endl;
220  }
221  } else {
222  saveThisTable = true;
223  cout << "Saving this table because cannot open last timestamp file " << lastTableTimestampFilename << endl;
224  }
225  const emcPed_st *ped_t_st = 0;
226  const smdPed_st *ped_s_st = 0;
227  if (getDetector() < 3) {
228  const St_emcPed *pedT = (const St_emcPed *)lastTableFile->Get(n[getDetector() - 1]);
229  ped_t_st = pedT ? pedT->GetTable() : 0;
230  } else {
231  const St_smdPed *pedT = (const St_smdPed *)lastTableFile->Get(n[getDetector() - 1]);
232  ped_s_st = pedT ? pedT->GetTable() : 0;
233  }
234  Float_t maxPedDiff = 0;
235  Float_t numPedDiff = 0;
236  Int_t *bemcStatus = 0;
237  if ((getDetector() == 1) && this->getUseBemcStatus() && this->getBemcStatusFilename()) {
238  TString bemcStatusFilename = this->getBemcStatusFilename();
239  ifstream ifstr(bemcStatusFilename);
240  if (ifstr.good()) {
241  cout << "Reading BEMC trigger status file " << bemcStatusFilename << endl;
242  } else {
243  cout << "Cannot open BEMC trigger status file! " << bemcStatusFilename << endl;
244  }
245  if (ifstr.good()) {
246  bemcStatus = new Int_t[4800];
247  if (bemcStatus) {
248  for (Int_t i = 4800;i;bemcStatus[--i] = 1);
249  while (ifstr.good()) {
250  string token;
251  do {
252  if (token == "#") {
253  char dummy[4096];
254  ifstr.getline(dummy, sizeof(dummy));
255  }
256  ifstr >> token;
257  } while (ifstr.good() && (token != "SoftId"));
258  if (ifstr.good()) {
259  if (token == "SoftId") {
260  int softId, crate, crateSeq, unmaskTower, unmaskHT, unmaskPA;
261  float ped;
262  ifstr >> softId >> crate >> crateSeq >> unmaskTower >> unmaskHT >> unmaskPA >> ped;
263  if ((softId >= 1) && (softId <= 4800)) {
264  bemcStatus[softId - 1] = (unmaskTower && (unmaskHT || unmaskPA)) ? 1 : 0;
265  if (bemcStatus[softId - 1] != 1) {
266  //cout << "tower " << softId << " masked out: " << unmaskTower << " " << unmaskHT << " " << unmaskPA << endl;
267  }
268  }
269  }
270  }
271  }
272  }
273  ifstr.close();
274  }
275  }
276  for (Int_t i = 0;i < getNChannel();i++) {
277  Int_t id = i + 1;
278  Float_t pedNew = getPedestal(id);
279  Float_t rmsNew = getRms(id);
280  //Float_t chiNew = getChi(id);
281  Int_t statusNew = (int)getStatus(id);
282  Float_t pedLast = 0;
283  Float_t rmsLast = 0;
284  Float_t chiLast = 0;
285  Int_t statusLast = 0;
286  if (getDetector() < 3) {
287  if (ped_t_st) {
288  pedLast = (short)ped_t_st->AdcPedestal[i] / 100.0;
289  rmsLast = (short)ped_t_st->AdcPedestalRMS[i] / 100.0;
290  chiLast = ped_t_st->ChiSquare[i];
291  statusLast = ped_t_st->Status[i];
292  }
293  } else {
294  if (ped_s_st) {
295  pedLast = (short)ped_s_st->AdcPedestal[i][0] / 100.0;
296  rmsLast = (short)ped_s_st->AdcPedestalRMS[i][0] / 100.0;
297  statusLast = ped_s_st->Status[i];
298  }
299  }
300  Float_t pedDiff = pedNew - pedLast;
301  if (maxPedDiff < TMath::Abs(pedDiff)) maxPedDiff = TMath::Abs(pedDiff);
302  if (/*(statusLast == 1) || */(statusNew == 1)) {
303  if (TMath::Abs(pedDiff) >= TMath::Abs(getPedDiffSaveDB() * rmsNew)) {
304  TString statusLabel = "";
305  if (!bemcStatus || ((getDetector() == 1) && bemcStatus && (id >= 1) && (id <= 4800) && (bemcStatus[id-1] == 1))) {
306  numPedDiff++;
307  } else {
308  statusLabel = " (bad in trigger)";
309  }
310  cout << "det " << getDetector() << ", id " << id << statusLabel << ": ped diff " << pedDiff << ", last " << pedLast << " " << rmsLast << " " << (Int_t)statusLast << ", new " << pedNew << " " << rmsNew << " " << (Int_t)statusNew << endl;
311  }
312  }
313  }
314  if (bemcStatus) delete [] bemcStatus; bemcStatus = 0;
315  cout << "max ped diff " << maxPedDiff << endl;
316  cout << "num ped diff " << numPedDiff << endl;
317  saveThisTable |= (numPedDiff >= this->getPedDiffSaveNum());
318  lastTableFile->Close();
319  if (!saveThisTable) cout << "This table does not need to be saved" << endl;
320  }
321  if (lastTableFile) delete lastTableFile; lastTableFile = 0;
322  }
323 
324  St_emcPed *pedT_t = new St_emcPed(n[getDetector() - 1].Data(), 1);
325  emcPed_st *tnew = pedT_t ? pedT_t->GetTable() : 0;
326 
327  St_smdPed *pedS_t = new St_smdPed(n[getDetector() - 1].Data(), 1);
328  smdPed_st *snew = pedS_t ? pedS_t->GetTable() : 0;
329 
330  for (int i = 0;i < getNChannel();i++) {
331  int id = i + 1;
332  float ped = getPedestal(id);
333  float rms = getRms(id);
334  float chi = getChi(id);
335  int status = (int)getStatus(id);
336  if (getDetector() < 3) {
337  if (tnew) {
338  tnew->Status[i] = (char)status;
339  tnew->AdcPedestal[i] = (short)(ped * 100.0);
340  tnew->AdcPedestalRMS[i] = (short)(rms * 100.0);
341  tnew->ChiSquare[i] = chi;
342  //cout << "tnew i = " << i << ": Status " << (Int_t)tnew->Status[i] << ", ped " << tnew->AdcPedestal[i] << ", rms " << tnew->AdcPedestalRMS[i] << ", chi " << tnew->ChiSquare[i] << endl;
343  }
344  } else {
345  if (snew) {
346  snew->Status[i] = (char)status;
347  snew->AdcPedestal[i][0] = (short)(ped * 100.0);
348  snew->AdcPedestalRMS[i][0] = (short)(rms * 100.0);
349  snew->AdcPedestal[i][1] = 0;
350  snew->AdcPedestalRMS[i][1] = 0;
351  snew->AdcPedestal[i][2] = 0;
352  snew->AdcPedestalRMS[i][2] = 0;
353  //cout << "snew i = " << i << ": Status " << (Int_t)snew->Status[i] << ", ped " << snew->AdcPedestal[i][0] << ", rms " << snew->AdcPedestalRMS[i][0] << endl;
354  }
355  }
356  }
357  if (isAutoSaveDB() && (!getCompareLastTableDB() || saveThisTable)) {
359  cout << "mgr = " << mgr << endl;
360  StDbConfigNode* node = mgr ? mgr->initConfig(dbCalibrations, dbEmc) : 0;
361  cout << "node = " << node << endl;
362  StDbTable* table = node ? node->addDbTable(n[getDetector() - 1].Data()) : 0;
363  cout << "table = " << table << endl;
364  if (table) {
365  table->setFlavor("ofl");
366  if (getDetector() < 3) {
367  table->SetTable((char*)tnew, 1);
368  } else {
369  table->SetTable((char*)snew, 1);
370  }
371  }
372  cout << "table set" << endl;
373  if (mgr && table) {
374  cout << "setStoreTime " << timeStamp << endl;
375  mgr->setStoreTime(timeStamp);
376  cout << "Storing " << n[getDetector() - 1] << " " << timeStamp << endl;
377  mgr->storeDbTable(table);
378  cout << "Stored." << endl;
379  }
380  }
381  if (getSaveTables() && (!getCompareLastTableDB() || saveThisTable) && tableFilename) {
382  cout << "Saving DB table into " << tableFilename << endl;
383  TFile *f = new TFile(tableFilename, "RECREATE");
384  if (f) {
385  if (getDetector() < 3) {
386  pedT_t->AddAt(tnew, 0);
387  pedT_t->Write();
388  } else {
389  pedS_t->AddAt(snew, 0);
390  pedS_t->Write();
391  }
392  f->Close();
393  delete f; f = 0;
394  }
395  }
396  if (getCompareLastTableDB() && saveThisTable) {
397  cout << "Saving last DB table into " << lastTableFilename << endl;
398  TFile *f = new TFile(lastTableFilename, "RECREATE");
399  if (f) {
400  if (getDetector() < 3) {
401  pedT_t->AddAt(tnew, 0);
402  pedT_t->Write();
403  } else {
404  pedS_t->AddAt(snew, 0);
405  pedS_t->Write();
406  }
407  f->Close();
408  delete f; f = 0;
409  ofstream ofstr_timestamp(lastTableTimestampFilename);
410  ofstr_timestamp << timeStamp << endl;
411  }
412  if (getDetector() == 1) {
413  StEmcDecoder *d = new StEmcDecoder();
414  for (Int_t crate = 1;crate <= 30;crate++) {
415  TString pedCrateFilename = getLastTablePath();
416  pedCrateFilename += "/";
417  pedCrateFilename += Form(getPedCrateFilenameFormat(), crate);
418  TString pedCrateTimestampFilename = pedCrateFilename + ".timestamp";
419  cout << "Saving pedestals for crate " << crate << ": " << pedCrateFilename << endl;
420  ofstream ofstr(pedCrateFilename);
421  for (Int_t ch = 0;ch < 160;ch++) {
422  Int_t softId = -1;
423  if (d) d->GetTowerIdFromCrate(crate, ch, softId);
424  if ((softId >= 1) && (softId <= 4800)) {
425  TString line = Form("%i %.2f %.2f", ch, getPedestal(softId), getRms(softId));
426  ofstr << line.Data() << endl;
427  }
428  }
429  ofstream ofstr_timestamp(pedCrateTimestampFilename);
430  ofstr_timestamp << timeStamp << endl;
431  }
432  if (d) delete d; d = 0;
433  }
434  }
435 }
436 //_____________________________________________________________________________
437 void StEmcPedestalMaker::saveToDb(Int_t date, Int_t time) const
438 {
439  Int_t year = (Int_t)(date/10000);
440  Int_t month = (Int_t)(date-year*10000)/100;
441  Int_t day = (Int_t)(date-year*10000-month*100);
442  Int_t hour = (Int_t)(time/10000);
443  Int_t minute= (Int_t)(time-hour*10000)/100;
444  Int_t second= (Int_t)(time-hour*10000-minute*100);
445  Char_t ts[1024]; ts[0] = 0;
446  Char_t tf[1024]; tf[0] = 0;
447  TString d[] = {"bemc","bprs","bsmde","bsmdp"};
448  TString n[] = {"bemcPed","bprsPed","bsmdePed","bsmdpPed"};
449  sprintf(ts,"%04d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second);
450  //sprintf(ts,"%04d%02d%02d%02d%02d%02d",year,month,day,hour,minute,second);
451  sprintf(tf, "%s/y3%s/%s.%08d.%06d.root", getTablesPath(), d[getDetector() - 1].Data(), n[getDetector() - 1].Data(), date, time);
452  saveToDb(ts, tf);
453 }
454 //_____________________________________________________________________________
455 void StEmcPedestalMaker::savePedestals(Int_t date, Int_t time, Bool_t DB) const
456 {
457  Char_t ts[1024]; ts[0] = 0;
458  TString n[] = {"bemcPed","bprsPed","bsmdePed","bsmdpPed"};
459  if (DB) sprintf(ts,"%s/%s.%08d.%06d.root", getSavePath(), n[getDetector()-1].Data(), date, time);
460  else sprintf(ts,"%s/%s.%08d.%06d.NO_DB.root", getSavePath(), n[getDetector()-1].Data(), date, time);
461  cout << "Saving pedestal histograms to " << ts << endl;
462  TFile *f = new TFile(ts,"RECREATE");
463  if(getSpec()) getSpec()->Write();
464  if(mPedestal) mPedestal->Write();
465  if(mRms) mRms->Write();
466  if(mChi) mChi->Write();
467  if(mStatus) mStatus->Write();
468  f->Close();
469  delete f;
470 }
471 //_____________________________________________________________________________
472 void StEmcPedestalMaker::loadPedestals(const Char_t* file)
473 {
474  TFile *f = new TFile(file);
475  if(getSpec()) getSpec()->Reset();
476  if(mPedestal) mPedestal->Reset();
477  if(mRms) mRms->Reset();
478  if(mChi) mChi->Reset();
479  if(mStatus) mStatus->Reset();
480  TH2F* h = (TH2F*)f->Get("mSpec;1");
481  if(h && getSpec()) getSpec()->Add(h,1);
482  TH1F *g=(TH1F*)f->Get("mPed;1");
483  if(g && mPedestal) mPedestal->Add(g,1);
484  g=(TH1F*)f->Get("mRms;1");
485  if(g && mRms) mRms->Add(g,1);
486  g=(TH1F*)f->Get("mChi;1");
487  if(g && mChi) mChi->Add(g,1);
488  g=(TH1F*)f->Get("mStatus;1");
489  if(g && mStatus) mStatus->Add(g,1);
490  f->Close();
491  delete f;
492  return;
493 }
494 
virtual void Clear(Option_t *option="")
User defined functions.
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc&#39;d version of data for StRoot
Definition: StDbTable.cc:550
int GetTowerIdFromCrate(int crate, int sequence, int &softId) const
Get Software Id from Crate number and position in crate for towers.
Definition: Stypes.h:40
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155
virtual Int_t Finish()
Definition: StMaker.cxx:776
Definition: Stypes.h:41