1 #include "StEmcPedestalMaker.h"
10 #include <StEventTypes.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>
33 setPedDiffSaveDB(2.0);
35 setPedDiffSaveMinTime(60 * 60 * 24);
36 setCompareLastTableDB(
false);
37 setPedCrateFilenameFormat(
"pedestal_crate0x%02x.dat");
38 setBemcStatusFilename(
"bemcStatus.txt");
39 setUseBemcStatus(
true);
42 StEmcPedestalMaker::~StEmcPedestalMaker()
46 Int_t StEmcPedestalMaker::Init()
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);
53 mSpecName =
"mSpecPed";
54 mAcceptName =
"mAcceptPed";
58 return StEmcCalibMaker::Init();
67 if(!accept())
return kStOk;
71 if(getTimeInterval(mLastPedDate,mLastPedTime)>mPedInterval)
73 mLastPedDate = getDate();
74 mLastPedTime = getTime();
80 if(isDebug()) cout <<
"Time remaining for a new pedestal run is "
81 <<mPedInterval-getTimeInterval(mLastPedDate,mLastPedTime)
82 <<
" hours for detector number "<<getDetector()<<endl;
86 if(!mStarted)
return kStOk;
88 for(
int i=0;i<mNChannel;i++)
91 float adc = (float) getCalib()->getADC(mDetector,
id);
93 if(adc!=0) fill(
id,adc);
96 if(getNEvents()>getNPedEvents())
99 savePedestals(mLastPedDate,mLastPedTime,isAutoSaveDB());
100 if(isAutoSaveDB() || getSaveTables()) saveToDb(mLastPedDate,mLastPedTime);
112 void StEmcPedestalMaker::calcPedestals()
114 cout <<
"***** Calculating pedestals for detector "<<getDetector()<<
" ...\n";
122 int ngood=0,nped=0,nrms=0,nchi=0,nbad=0,nodata=0;
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();
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)");
135 func.SetParameter(0,max);
136 func.SetParameter(1,avg);
137 func.SetParameter(2,rms);
138 func.SetParLimits(2,0,100000);
140 float fitleft = avg-left*rms;
141 if(fitleft<0) fitleft = 0;
142 float fitright = avg+right*rms;
143 func.SetRange(fitleft,fitright);
145 int npt = (Int_t)((left+right+1.0)*rms);
146 int ndg = (Int_t)((
float)npt-3.0);
149 max = func.GetParameter(0);
150 avg = func.GetParameter(1);
151 rms = func.GetParameter(2);
153 float chi = func.GetChisquare()/(float)ndg;
154 float res = avg-seed;
156 if(avg<0) {status+= 2; nped++; avg = 0;}
157 if(rms<0 || rms >7*rmsInit) {status+= 4; nrms++;}
158 if(fabs(res)>1.5*rms) {status+= 8; nchi++;}
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
166 <<
" ped = "<<avg <<
" res = " <<res<<
" rms = "<<rms
167 <<
" chi = "<<chi<<
" status = "<<status<<endl;
169 mPedestal->Fill((
float)
id,0);
170 mRms->Fill((
float)
id,0);
171 mChi->Fill((
float)
id,0);
172 mStatus->Fill((
float)
id,0);
175 cout <<
"det = " << getDetector() <<
" id = " <<
id <<
" peakY = "<< max
176 <<
" ped = " << avg <<
" rms = " << rms
177 <<
" int = " << integral << endl;
179 if(h) {
delete h; h = 0;}
181 cout <<
"nGood = "<<ngood<<
"; nBad = "<<nbad<<
": neg Ped = "<<nped<<
", bad rms = "<<nrms<<
", large res = "<<nchi <<
", no data = " << nodata <<endl;
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;
189 TString n[] = {
"bemcPed",
"bprsPed",
"bsmdePed",
"bsmdpPed"};
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";
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;
222 saveThisTable =
true;
223 cout <<
"Saving this table because cannot open last timestamp file " << lastTableTimestampFilename << endl;
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;
231 const St_smdPed *pedT = (
const St_smdPed *)lastTableFile->Get(n[getDetector() - 1]);
232 ped_s_st = pedT ? pedT->GetTable() : 0;
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);
241 cout <<
"Reading BEMC trigger status file " << bemcStatusFilename << endl;
243 cout <<
"Cannot open BEMC trigger status file! " << bemcStatusFilename << endl;
246 bemcStatus =
new Int_t[4800];
248 for (Int_t i = 4800;i;bemcStatus[--i] = 1);
249 while (ifstr.good()) {
254 ifstr.getline(dummy,
sizeof(dummy));
257 }
while (ifstr.good() && (token !=
"SoftId"));
259 if (token ==
"SoftId") {
260 int softId, crate, crateSeq, unmaskTower, unmaskHT, unmaskPA;
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) {
276 for (Int_t i = 0;i < getNChannel();i++) {
278 Float_t pedNew = getPedestal(
id);
279 Float_t rmsNew = getRms(
id);
281 Int_t statusNew = (int)getStatus(
id);
285 Int_t statusLast = 0;
286 if (getDetector() < 3) {
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];
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];
300 Float_t pedDiff = pedNew - pedLast;
301 if (maxPedDiff < TMath::Abs(pedDiff)) maxPedDiff = TMath::Abs(pedDiff);
302 if ((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))) {
308 statusLabel =
" (bad in trigger)";
310 cout <<
"det " << getDetector() <<
", id " <<
id << statusLabel <<
": ped diff " << pedDiff <<
", last " << pedLast <<
" " << rmsLast <<
" " << (Int_t)statusLast <<
", new " << pedNew <<
" " << rmsNew <<
" " << (Int_t)statusNew << endl;
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;
321 if (lastTableFile) delete lastTableFile; lastTableFile = 0;
324 St_emcPed *pedT_t = new St_emcPed(n[getDetector() - 1].Data(), 1);
325 emcPed_st *tnew = pedT_t ? pedT_t->GetTable() : 0;
327 St_smdPed *pedS_t = new St_smdPed(n[getDetector() - 1].Data(), 1);
328 smdPed_st *snew = pedS_t ? pedS_t->GetTable() : 0;
330 for (
int i = 0;i < getNChannel();i++) {
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) {
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;
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;
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;
365 table->setFlavor(
"ofl");
366 if (getDetector() < 3) {
372 cout <<
"table set" << endl;
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;
381 if (getSaveTables() && (!getCompareLastTableDB() || saveThisTable) && tableFilename) {
382 cout <<
"Saving DB table into " << tableFilename << endl;
383 TFile *f =
new TFile(tableFilename,
"RECREATE");
385 if (getDetector() < 3) {
386 pedT_t->AddAt(tnew, 0);
389 pedS_t->AddAt(snew, 0);
396 if (getCompareLastTableDB() && saveThisTable) {
397 cout <<
"Saving last DB table into " << lastTableFilename << endl;
398 TFile *f =
new TFile(lastTableFilename,
"RECREATE");
400 if (getDetector() < 3) {
401 pedT_t->AddAt(tnew, 0);
404 pedS_t->AddAt(snew, 0);
409 ofstream ofstr_timestamp(lastTableTimestampFilename);
410 ofstr_timestamp << timeStamp << endl;
412 if (getDetector() == 1) {
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++) {
424 if ((softId >= 1) && (softId <= 4800)) {
425 TString line = Form(
"%i %.2f %.2f", ch, getPedestal(softId), getRms(softId));
426 ofstr << line.Data() << endl;
429 ofstream ofstr_timestamp(pedCrateTimestampFilename);
430 ofstr_timestamp << timeStamp << endl;
432 if (d)
delete d; d = 0;
437 void StEmcPedestalMaker::saveToDb(Int_t date, Int_t time)
const
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);
451 sprintf(tf,
"%s/y3%s/%s.%08d.%06d.root", getTablesPath(), d[getDetector() - 1].Data(), n[getDetector() - 1].Data(), date, time);
455 void StEmcPedestalMaker::savePedestals(Int_t date, Int_t time, Bool_t DB)
const
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();
472 void StEmcPedestalMaker::loadPedestals(
const Char_t* file)
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);
virtual void Clear(Option_t *option="")
User defined functions.
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc'd version of data for StRoot
int GetTowerIdFromCrate(int crate, int sequence, int &softId) const
Get Software Id from Crate number and position in crate for towers.
static StDbManager * Instance()
strdup(..) is not ANSI