1 #include "StEmcMipMaker.h"
5 #include "StEventTypes.h"
7 #include "StEmcUtil/geometry/StEmcGeom.h"
8 #include "StDbLib/StDbManager.hh"
9 #include "StDbLib/StDbTable.h"
10 #include "StDbLib/StDbConfigNode.hh"
11 #include "tables/St_emcCalib_Table.h"
12 #include "StEmcEqualMaker.h"
23 for(
int i=0;i<10;i++) mNFailed[i]=0;
39 StEmcMipMaker::~StEmcMipMaker()
56 Int_t StEmcMipMaker::Init()
58 mMipPos =
new TH1F(
"mMipPos",
"",getNChannel(),0.5,getNChannel()+0.5);
59 mMipPosErr =
new TH1F(
"mMipPosErr",
"",getNChannel(),0.5,getNChannel()+0.5);
60 mMipWid =
new TH1F(
"mMipWid",
"",getNChannel(),0.5,getNChannel()+0.5);
61 mMipWidErr =
new TH1F(
"mMipWidErr",
"",getNChannel(),0.5,getNChannel()+0.5);
62 mGain =
new TH1F(
"mGain",
"",getNChannel(),0.5,getNChannel()+0.5);
63 mGainDistr =
new TH1F(
"mGainDistr",
"",100,0,0.1);
64 mChi2 =
new TH1F(
"mChi2",
"",getNChannel(),0.5,getNChannel()+0.5);
65 mIntegral =
new TH1F(
"mIntegral",
"",getNChannel(),0.5,getNChannel()+0.5);
68 mAcceptName =
"mAcceptMip";
70 return StEmcCalibMaker::Init();
79 if(!accept())
return kStOk;
81 cout <<
"Number of tracks = "<<getCalib()->getNTracks()<<endl;
82 for(
int i=0;i<getCalib()->getNTracks();i++)
84 int id = getCalib()->getTrackTower(i);
85 int id2 = getCalib()->getTrackTowerExit(i);
86 float p = getCalib()->getTrackP(i);
88 float adc = (float) getCalib()->getADCPedSub(1,
id);
90 if(p>mPmin &&
id!=0 && id2==
id )
92 int nt = getCalib()->getNTracksInTower(
id);
93 float rms = getCalib()->getPedRms(1,
id);
94 if(adc!=0 && adc>2*rms && nt==1)
96 cout <<
"Found MIP. p = "<<p<<
" Tower id = "<<
id<<
" ADC = "<<adc<<endl;
107 saveHist((
char*)mFileName.Data());
111 void StEmcMipMaker::fit(TH1F* h)
114 float max = h->GetBinCenter(h->GetMaximumBin());
116 if(max<15) WIDTH = 3;
118 float maxy = h->GetBinContent(h->GetMaximumBin());
119 float maxback = h->GetBinContent((
int)(h->GetMaximumBin()+6*WIDTH));
123 float xmax = (max*3>120) ? max*3 : 120;
125 float p[] = {maxy-maxback,max,WIDTH,maxback,0,50};
128 funcFitPeak =
new TF1(
"peak",
"gaus(0)",0,1000);
129 funcFitPeak->SetLineColor(2);
130 funcFitPeak->SetLineWidth(1);
132 funcFitBack =
new TF1(
"back",
"gaus(0)",0,1000);
133 funcFitBack->SetLineWidth(1);
134 funcFitBack->SetLineColor(3);
136 funcFit =
new TF1(
"func",
"gaus(0)+gaus(3)",0,1000);
137 funcFit->SetLineWidth(1);
138 funcFit->SetLineColor(4);
141 func->SetRange(xmin,xmax);
143 for(
int i=0;i<NPars;i++) func->SetParameter(i,p[i]);
144 func->SetParLimits(0,0,10000);
145 func->SetParLimits(3,0,10000);
146 func->SetParLimits(5,30,100);
150 int nb = h->FindBin(xmax);
151 for(
int i=h->FindBin(xmin);i<nb;i++)
154 float y = h->GetBinContent(i);
155 float stat = sqrt(y);
158 float sigma = sqrt(stat*stat);
159 h->SetBinError(i,sigma);
169 if(!equal)
return NULL;
170 TH1F* h = equal->getEtaBinSpec(eta0,eta1,getSpec());
172 sprintf(name,
"Mip.%d.%d",eta0,eta1);
174 sprintf(name,
"Mip for %d <= eta <= %d",eta0,eta1);
182 for(
int i=0;i<NPars;i++)
184 float a = func->GetParameter(i);
185 if(i<3) funcFitPeak->SetParameter(i,a);
else funcFitBack->SetParameter(i-3,a);
188 float chi=func->GetChisquare()/(func->GetNDF());
189 if(chi<0 || chi>1000) chi = 1000;
190 float peak = func->GetParameter(1);
191 if(peak<5) peak = 0 ;
192 float epeak = func->GetParError(1);
193 float w = func->GetParameter(2);
194 float ew = func->GetParError(2);
200 if(eta0<0 && eta1<0) {m1 = 61; m2 = 120;}
201 if(eta0*eta1<0) {m1 = 1; m2 = 120;}
206 int ns = geo->NSub();
208 for(
int m = m1;m<=m2;m++)
209 for(
int e = eta0; e<=eta1; e++)
210 for(
int s = 1; s<=ns; s++)
213 geo->getId(m,e,s,
id);
214 float a = equal->getA()->GetBinContent(
id);
215 float b = equal->getB()->GetBinContent(
id);
217 if(a>10) {a = 0; b = 0;}
218 if(a<0.1) {a = 0; b = 0;}
220 mMipPos->SetBinContent(
id,peak*a+b);
221 mMipPosErr->SetBinContent(
id,epeak*a);
222 mMipWid->SetBinContent(
id,w*a);
223 mMipWidErr->SetBinContent(
id,ew*a);
224 mChi2->SetBinContent(
id,chi);
225 mIntegral->SetBinContent(
id,0);
226 cout <<
"MIP peak = "<<peak<<
"+-"<<epeak<<
" width = "<<w<<
"+-"<<ew<<endl;
227 cout <<
"Final chi2 = "<<chi<<endl;
233 TH1F* StEmcMipMaker::findMip(
int id,
int rebin,
bool print)
238 delete funcFitPeak; funcFitPeak = NULL;
239 delete funcFitBack; funcFitBack = NULL;
240 delete funcFit ; funcFit = NULL;
241 if(!mSpec)
return NULL;
242 if(gROOT->FindObject(
"id"))
delete (TH1D*)gROOT->FindObject(
"id");
243 TH1F *h = (TH1F*)mSpec->ProjectionY(
"id",
id,
id);
244 if(!h) { mNFailed[0]++;
return NULL;}
247 float integral= h->Integral();
248 if(integral==0) { mNFailed[0]++; mNFailed[1]++;
return h;}
250 if(print) cout <<
"Fitting MIP for id = "<<
id<<endl;
251 if(print) cout <<
"Integral = "<<integral<<endl;
257 chi=func->GetChisquare()/(func->GetNDF());
258 if(chi<0 || chi>1000) chi = 1000;
262 for(
int i=0;i<NPars;i++)
264 float a = func->GetParameter(i);
265 if(i<3) funcFitPeak->SetParameter(i,a);
else funcFitBack->SetParameter(i-3,a);
268 float peak = func->GetParameter(1);
269 if(peak<5) peak = 0 ;
270 float epeak = func->GetParError(1);
271 float w = func->GetParameter(2);
272 float ew = func->GetParError(2);
273 float XMAX = func->GetMaximumX(7,200);
274 if(fabs(XMAX-peak)>2)
278 if(print) cout <<
"****************** FAILED fabs(XMAX-peak) *****************\n";
279 if(print) cout <<
"fabs(XMAX-peak) = "<<fabs(XMAX-peak)<<endl;
282 mMipPos->Fill(
id,peak);
283 mMipPosErr->Fill(
id,epeak);
285 mMipWidErr->Fill(
id,ew);
287 mIntegral->Fill(
id,integral);
288 if(print) cout <<
"MIP peak = "<<peak<<
"+-"<<epeak<<
" width = "<<w<<
"+-"<<ew<<endl;
289 if(print) cout <<
"Final chi2 = "<<chi<<endl;
295 float StEmcMipMaker::findGain(
int id,
bool print)
297 float mip = getMipPosition(
id);
299 StEmcGeom *geom = StEmcGeom::instance(
"bemc");
301 geom->getEtaPhi(
id,eta,phi);
302 float theta=2.*atan(exp(-eta));
303 float EoverMIP = 0.261;
304 float MipE = EoverMIP*(1.+0.056*eta*eta)/sin(theta);
305 float gain = MipE/mip;
306 float chi = getChi2(
id);
307 if(chi<0.001 || chi>30) gain = 0;
308 if(gain <0 || gain > 10000) gain = 0;
309 if(gain==0) mNFailed[4]++;
310 mGain->Fill(
id,gain);
311 mGainDistr->Fill(gain);
312 if(print) cout <<
"gain for id "<<
id<<
" = "<<gain<<endl;
313 if(print) cout <<
"-------------------------------------------------------\n";
317 void StEmcMipMaker::mipCalib()
319 for(
int id = 0;
id<4800;
id++)
321 if((
id-1)%100==0) cout <<
"Doing MIP calibration for id "<<
id<<endl;
322 delete findMip(
id,3,
false);
330 void StEmcMipMaker::mipCalib(
int eta0,
int eta1,
int etabin,
StEmcEqualMaker* equal,
bool draw)
335 for(
int e = eta0; e<=eta1;e++)
337 TH1F *h = findMip(e,e+etabin-1,equal);
340 if(pad==1) {c1 =
new TCanvas(); c1->Divide(2,2);}
343 funcFitBack->Draw(
"same");
344 funcFitPeak->Draw(
"same");
345 funcFit->Draw(
"same");
359 TH1F* StEmcMipMaker::compareToDb(
char* timeStamp,
int mode)
364 StDbTable* table = node->addDbTable(
"bemcCalib");
365 mgr->setRequestTime(timeStamp);
366 mgr->fetchDbTable(table);
367 emcCalib_st *calib = (emcCalib_st*) table->GetTable();
368 if(!calib)
return NULL;
371 sprintf(line,
"Comparison with DB - %s",timeStamp);
373 if(mode == 0) h =
new TH1F(
"h",line,4800,0.5,4800.5);
374 else h =
new TH1F(
"h",line,100,0,4);
375 for(
int i=0;i<4800;i++)
378 float gain = calib[0].AdcToE[i][1];
379 float gain2 = getGain(
id);
381 if(gain!=0) ratio = gain2/gain;
382 if(mode==0) h->Fill(
id,ratio);
else h->Fill(ratio);
387 void StEmcMipMaker::saveToDb(
char* timeStamp)
390 for(
int i=0;i<4800;i++)
393 float gain = getGain(
id);
394 if(gain==0) tnew.Status[i] = 0;
else tnew.Status[i] = 1;
395 tnew.AdcToE[i][0] = 0;
396 tnew.AdcToE[i][1] = gain;
397 tnew.AdcToE[i][2] = 0;
398 tnew.AdcToE[i][3] = 0;
399 tnew.AdcToE[i][4] = 0;
403 StDbTable* table = node->addDbTable(
"bemcCalib");
405 mgr->setStoreTime(timeStamp);
406 mgr->storeDbTable(table);
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
static StDbManager * Instance()
strdup(..) is not ANSI