StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFgtTimeShapeMaker.cxx
1  /***************************************************************************
2  *
3  *
4  * Author: Len K. Eun, Jan 2012
5  *
6  ***************************************************************************
7  *
8  * Description: Time shape fitter for the raw daq files. Output is a TTree that
9  * contain the original spectrum, mapping information, and the fit results.
10  *
11  ***************************************************************************
12  * No version history yet
13  *
14  *
15  *
16  *
17  **************************************************************************/
18 
19 #include <string>
20 #include "StFgtTimeShapeMaker.h"
21 #include "StRoot/StEvent/StFgtCollection.h"
22 #include "StRoot/StEvent/StFgtStrip.h"
23 #include "StRoot/StEvent/StEvent.h"
24 #include "StRoot/StFgtUtil/geometry/StFgtGeom.h"
25 #include "StRoot/StFgtDbMaker/StFgtDbMaker.h"
26 
27 // Copied from StFgtPedMaker.cxx
28 
29 // constructor
30 StFgtTimeShapeMaker::StFgtTimeShapeMaker( const Char_t* name , const Char_t* dbMkrName ) : StMaker( name ), mDbMkrName( dbMkrName ), mFgtDbMkr(0) {
31  fname="hFgt";
32  fitThresh=350;
33  plotThresh=700;
34  fixTau=false;
35  Ntimebin=7;
36 };
37 
38 // initialize
39 Int_t StFgtTimeShapeMaker::Init(){
40  Int_t ierr = kStOk;
41 
42  ierr=InitTree();
43 
44  mFgtDbMkr = static_cast< StFgtDbMaker* >( GetMaker( mDbMkrName.data() ) );
45 
46  if( !ierr && !mFgtDbMkr ){
47  LOG_FATAL << "Error finding mFgtDbMkr named '" << mDbMkrName << "'" << endm;
48  ierr = kStFatal;
49  };
50 
51  if( !mFgtDbMkr->InheritsFrom("StFgtDbMaker") ){
52  LOG_FATAL << "StFgtDbMkr does not inherit from StFgtDbMaker" << endm;
53  LOG_FATAL << "Name is '" << mFgtDbMkr->GetName() << "', class is '" << mFgtDbMkr->ClassName() << endm;
54  ierr = kStFatal;
55  };
56 
57  if( !mFgtDbMkr ){
58  mFgtDbMkr = static_cast< StFgtDbMaker* >( GetMakerInheritsFrom( "StFgtDbMaker" ) );
59  if( !mFgtDbMkr ){
60  LOG_FATAL << "StFgtDbMaker name not provided and error finding StFgtDbMaker" << endm;
61  ierr = kStFatal;
62  };
63  };
64 
65  if(fixTau)
66  {
67  TFile ftau("htau.root","read");
68  if(ftau.IsOpen()){
69  htau=(TH1F*)ftau.Get("htau");
70  }
71  else{
72  LOG_FATAL << "Tau parameter fix requested, but htau.root not found" << endm;
73  ierr = kStFatal;
74  };
75  };
76 
77  LOG_INFO << "Using date and time " << mFgtDbMkr->GetDateTime().GetDate() << ", "
78  << mFgtDbMkr->GetDateTime().GetTime() << endm;
79 
80  igoodCnt=0;
81  ibadCnt=0;
82  iEvt=-1;
83  hh=new TH1F("hh","hh",Ntimebin,0,Ntimebin);
84 
85  return ierr;
86 };
87 
88 StFgtTimeShapeMaker::~StFgtTimeShapeMaker(){
89 };
90 
91 Int_t StFgtTimeShapeMaker::InitTree(){
92 
93  Int_t ierr = kStOk;
94  if(fname.CompareTo("")==0){
95  LOG_ERROR << "No output file name given for the TTree" << endm;
96  ierr = kStErr;
97  }
98  else{
99  TString outName=fname+".tree.root";
100  fFgt = new TFile(outName,"recreate");
101  tFgt = new TTree("tFgt","TTree for FGT time shape analysis");
102 
103  tFgt->Branch("iEvt",&iEvt,"iEvt/I");
104  tFgt->Branch("rdo",&rdo,"rdo/I");
105  tFgt->Branch("arm",&arm,"arm/I");
106  tFgt->Branch("apv",&apv,"apv/I");
107  tFgt->Branch("chn",&chn,"chn/I");
108  tFgt->Branch("disk",&disk,"disk/S");
109  tFgt->Branch("quad",&quad,"quad/S");
110  tFgt->Branch("strip",&strip,"strip/S");
111  tFgt->Branch("stat",&stat,"stat/S");
112  tFgt->Branch("ordinate",&ordinate,"ordinate/D");
113  tFgt->Branch("lowerSpan",&lowerSpan,"lowerSpan/D");
114  tFgt->Branch("upperSpan",&upperSpan,"upperSpan/D");
115  tFgt->Branch("layer",&layer,"layer/B");
116  tFgt->Branch("adc",adc,"adc[7]/I");
117  tFgt->Branch("ped",&ped,"ped/D");
118  tFgt->Branch("pedSig",&pedSig,"pedSig/D");
119  tFgt->Branch("adcmax",&adcmax,"adcmax/I");
120  tFgt->Branch("mmin",&mmin,"mmin/I");
121  tFgt->Branch("mmax",&mmax,"mmax/I");
122  tFgt->Branch("chi2",&chi2,"chi2/F");
123  tFgt->Branch("fmax",&fmax,"fmax/F");
124  tFgt->Branch("norm",&norm,"norm/F");
125  tFgt->Branch("tau",&tau,"tau/F");
126  tFgt->Branch("t0",&t0,"t0/F");
127  tFgt->Branch("beta",&beta,"beta/F");
128  tFgt->Branch("offset",&offset,"offset/F");
129  tFgt->Branch("errCode",&errCode,"errCode/I");
130  };
131  return ierr;
132 };
133 
135  iEvt++;
136  Int_t ierr = kStOk;
137  if(iEvt%10==0)cout << "iEvt = " << iEvt << endl;
138  StFgtDb *fgtTables = 0;
139  if( !mFgtDbMkr ){
140  LOG_FATAL << "Pointer to Fgt DB Maker is null" << endm;
141  ierr = kStFatal;
142  return ierr;
143  };
144  if( !ierr ){
145  fgtTables = mFgtDbMkr->getDbTables();
146 
147  if( !fgtTables ){
148  LOG_FATAL << "Pointer to Fgt DB Tables is null" << endm;
149  ierr = kStFatal;
150  return ierr;
151  };
152  };
153 
154  StEvent* eventPtr = 0;
155  eventPtr = (StEvent*)GetInputDS("StEvent");
156 
157  if( !eventPtr ) {
158  LOG_ERROR << "Error getting pointer to StEvent from '" << ClassName() << "'" << endm;
159  ierr = kStErr;
160  };
161 
162  mFgtCollectionPtr = 0;
163 
164  if( eventPtr ) {
165  mFgtCollectionPtr=eventPtr->fgtCollection();
166  };
167 
168  if( !mFgtCollectionPtr) {
169  LOG_ERROR << "Error getting pointer to StFgtCollection from '" << ClassName() << "'" << endm;
170  ierr = kStErr;
171  };
172 
173  if( pedSelect<0 || pedSelect>2 ) {
174  LOG_ERROR << "Error no pedestal type selected " << pedSelect << endm;
175  ierr = kStErr;
176  };
177 
178  FitFunc();
179  if( !ierr ){
180  for( UInt_t discIdx=0; discIdx<mFgtCollectionPtr->getNumDiscs(); ++discIdx ){
181  StFgtStripCollection *stripCollectionPtr = mFgtCollectionPtr->getStripCollection( discIdx );
182  if( stripCollectionPtr ){
183  StSPtrVecFgtStrip& stripVec = stripCollectionPtr->getStripVec();
184  StSPtrVecFgtStripIterator stripIter;
185  rdo=0;arm=-1;apv=-1;chn=-1;
186  char hname[100];
187  for( stripIter = stripVec.begin(); stripIter != stripVec.end(); ++stripIter ){
188  rdo=0;arm=apv=chn=stat=-1;
189  disk=quad=strip=-1.;layer=' ';
190  ordinate=lowerSpan=upperSpan=-1.;
191  Int_t dof=Ntimebin-4;
192 
193  (*stripIter)->getElecCoords( rdo, arm, apv, chn );
194  stat=fgtTables->getStatusFromElecCoord(rdo,arm,apv,chn);
195  if(stat>0)continue;//stat=0 is good. Otherwise, skip.
196 
197  int geoId=fgtTables->getGeoIdFromElecCoord(rdo, arm, apv, chn);
198  if (geoId>0){
199  StFgtGeom::decodeGeoId(geoId,disk,quad,layer,strip);
200  StFgtGeom::getPhysicalCoordinate(geoId,disk,quad,layer,ordinate,lowerSpan,upperSpan);
201  }
202  else{continue;};//strip was not mapped , we readout in the daq file some '0' form non-existing APVs.
203  //printf("%d %d %d %d %d %d \n",iEvt,rdo,arm,apv,chn,geoId);
204 
205  ped=99999.;pedSig=0.;
206  switch(pedSelect){
207  case 0:{
208  ped=(*stripIter)->getAdc(0);
209  pedSig=35.*ped/745.;
210  } break;
211  case 1:{
212  for(Int_t is=0;is<Ntimebin;is++){
213  if(ped>(*stripIter)->getAdc(is)){ped=(*stripIter)->getAdc(is);};
214  };
215  pedSig=35.*ped/745.;
216  //dof++;//when using adcmin as pedestal, we fix the offset parameter.
217  } break;
218  case 2:{
219  ped=fgtTables->getPedestalFromElecCoord(rdo,arm,apv,chn);
220  pedSig=fgtTables->getPedestalSigmaFromElecCoord(rdo,arm,apv,chn);
221  } break;
222  default:cout << "O_o This should never print" << endl;
223  }
224  if(ped<=0.)continue;
225  for(Int_t is=0;is<7;is++){adc[is]=0.;};
226  for(Int_t is=0;is<Ntimebin;is++){
227  adc[is]=(*stripIter)->getAdc(is);
228  adc[is]-=ped;
229  };
230  Bool_t pass=true;
231  //if(rdo==1 && arm==3 && apv==9)pass=false;
232  //if(rdo==1 && arm==4 && apv==21)pass=false;
233  if(rdo==1 && arm==0 && apv>-1 && apv<10){}
234  else if(rdo==2 && arm==0 && apv>-1 && apv<10){}
235  else{pass=false;};
236 
237  if(pass){
238  chi2=-1.;tau=0.;t0=0.;beta=0.;offset=0.;errCode=0;
239  hh->Reset();
240  for(Int_t is=0;is<Ntimebin;is++){
241  hh->SetBinContent(is+1,adc[is]);
242  hh->SetBinError(is+1,pedSig);
243  }
244  sprintf(hname,"rdo%d_arm%d_apv%d_chn%d_%d",rdo,arm,apv,chn,iEvt);
245  hh->SetTitle(hname);
246  mmax=hh->GetMaximumBin()-1;
247  mmin=hh->GetMinimumBin()-1;
248  adcmax=hh->GetBinContent(mmax+1);
249  if(0){
250  for(Int_t is=0;is<Ntimebin;is++){
251  printf("%d ",(*stripIter)->getAdc(is));
252  };
253  printf("ped=%f \n",ped);
254  };
255  if(adcmax>fitThresh){
256  if(abs(mmax-mmin)==2 && mmin>0 && mmax>0 && mmin<Ntimebin-1 && mmax<Ntimebin-1){
257  Float_t middle1=(hh->GetBinContent(mmin)+hh->GetBinContent(mmin+2))/2.;
258  Float_t middle2=(hh->GetBinContent(mmax)+hh->GetBinContent(mmax+2))/2.;
259  if((middle1-hh->GetBinContent(mmin+1))/hh->GetBinError(mmin+1)>3. && (hh->GetBinContent(mmax+1)-middle2)/hh->GetBinError(mmax+1)>3.){errCode=1;}
260  }
261  Int_t highcnt=0;
262  for(Int_t is=0;is<Ntimebin;is++){
263  if(adc[is]>adcmax*0.95 && adcmax>20.*pedSig)highcnt++;
264  };
265  if(highcnt>2){errCode=2;}
266 
267  if(!errCode){
268  InitFX();
269  if(fixTau){
270  Int_t binAPV = disk*40 + quad*10 + (apv%12);
271  Float_t tau0=htau->GetBinContent(binAPV+1);
272  if(tau0>0){InitFX(tau0);}
273  else{InitFX();}
274  dof++;//for fixing the tau parameter
275  };
276 
277  hh->Fit(FX,"Q","",0.,Ntimebin);
278  chi2=FX->GetChisquare()/(Float_t)dof;
279  fmax=FX->GetMaximumX();
280  norm=FX->GetParameter(0);
281  tau=FX->GetParameter(1);
282  beta=FX->GetParameter(2);
283  offset=FX->GetParameter(3);
284  t0=FX->GetParameter(4);
285  if(chi2<1. && igoodCnt<120 && adcmax>plotThresh){
286  hGood[igoodCnt]=new TH1F(*hh);
287  fGood[igoodCnt]=new TF1(*FX);
288  sprintf(hname,"fgood%d",igoodCnt);
289  fGood[igoodCnt]->SetTitle(hname);
290  igoodCnt++;
291  };
292  if(chi2>20. && ibadCnt<120 && adcmax>plotThresh){
293  hBad[ibadCnt]=new TH1F(*hh);
294  fBad[ibadCnt]=new TF1(*FX);
295  sprintf(hname,"fbad%d",ibadCnt);
296  fBad[ibadCnt]->SetTitle(hname);
297  ibadCnt++;
298  };
299  };
300  tFgt->Fill();//save only fitted events
301  };
302  };
303  };
304  };
305  };
306  };
307  return ierr;
308 };
309 
310 
312 
313  TCanvas* tcv1=new TCanvas("tcv1","tcv1",850,1100);
314  tcv1->Divide(5,8);
315  TCanvas* tcv2=new TCanvas("tcv2","tcv2",850,1100);
316  tcv2->Divide(5,8);
317 
318  tcv1->Print("fits_good.pdf(","pdf");
319  tcv2->Print("fits_bad.pdf(","pdf");
320 
321  for(Int_t i=0;i<igoodCnt;i++){
322  tcv1->GetPad(i%40+1)->SetGridx(0);
323  tcv1->GetPad(i%40+1)->SetGridy(0);
324  tcv1->cd(i%40+1);
325  hGood[i]->Draw();
326  fGood[i]->Draw("same");
327  if(i%40==39){
328  tcv1->Print("fits_good.pdf","pdf");
329  tcv1->Clear();
330  tcv1->Divide(5,8);
331  };
332  }
333  for(Int_t i=0;i<ibadCnt;i++){
334  tcv2->GetPad(i%40+1)->SetGridx(0);
335  tcv2->GetPad(i%40+1)->SetGridy(0);
336  tcv2->cd(i%40+1);
337  hBad[i]->Draw();
338  fBad[i]->Draw("same");
339  if(i%40==39){
340  tcv2->Print("fits_bad.pdf","pdf");
341  tcv2->Clear();
342  tcv2->Divide(5,8);
343  };
344  }
345 
346  tcv1->Print("fits_good.pdf)","pdf");
347  tcv2->Print("fits_bad.pdf)","pdf");
348 
349  tFgt->Print();
350  fFgt->cd();
351  tFgt->Write();
352  fFgt->Close();
353  cout << "StFgtTimeShapeMaker::Finish()" << endl;
354 
355  return StMaker::Finish();
356 };
357 
358 
360  //par[0]=normalization
361  //par[1]=tau
362  //par[2]=exponent: fixed at 2
363  //par[3]=y-offset: fixed at 0 for pedSelect==1
364  //par[4]=t-offset
365  MyFunc(TF1 * f): fFunc(f) {}
366  double Evaluate (double *x, double * par) const {
367  fFunc->SetParameter(0,par[0]);
368  fFunc->SetParameter(1,par[1]);
369  fFunc->SetParameter(2,par[2]);
370  fFunc->SetParameter(3,par[3]);
371  Float_t val=0;
372  if(x[0]-par[4]<0.){val=par[3];}
373  else{val=fFunc->Eval(x[0]-par[4]);};
374  return val;
375  }
376  TF1 * fFunc;
377 };
378 
379 
380 void StFgtTimeShapeMaker::FitFunc()
381 {
382  fs = new TF1("fs","[0]*x**[2]*exp(-x/[1])+[3]",-100,100);
383  MyFunc * mf = new MyFunc(fs);
384  FX = new TF1("FX",mf,&MyFunc::Evaluate,-10.,10.,5);
385  FX->SetLineWidth(1);
386  FX->SetLineColor(2);
387  InitFX();
388 };
389 
390 void StFgtTimeShapeMaker::InitFX()
391 {
392  FX->SetParameter(0,200.);
393  FX->SetParLimits(0,0.,100000.);
394  FX->SetParameter(1,1.);
395  FX->SetParLimits(1,0.1,10.);
396  FX->FixParameter(2,2.);
397  //FX->SetParameter(2,2.);
398  //FX->SetParLimits(2,1.,3.);
399  //if(pedSelect==1)FX->FixParameter(3,0.);
400  FX->SetParameter(4,0.);
401  FX->SetParLimits(4,-10.,17.);
402 };
403 
404 void StFgtTimeShapeMaker::InitFX(Float_t ftau)
405 {
406  FX->SetParameter(0,200.);
407  FX->SetParLimits(0,0.,100000.);
408  FX->SetParameter(1,ftau);
409  FX->SetParLimits(1,ftau,ftau);
410  FX->FixParameter(2,2.);
411  //if(pedSelect==1)FX->FixParameter(3,0.);
412  FX->SetParameter(4,0.);
413  FX->SetParLimits(4,-10.,17.);
414 };
415 
416 
417 ClassImp( StFgtTimeShapeMaker );
virtual const char * GetName() const
special overload
Definition: StMaker.cxx:237
virtual Int_t Finish()
Definition: StMaker.cxx:776
Definition: Stypes.h:44
Definition: Stypes.h:41