StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StPmdCalibConstMaker.cxx
1 /*************************************************
2  *
3  * StPmdCalibConstMaker.cxx$
4  * Author: Raghunath Sahoo, IOP, Bhubaneswar, Subhasis Chattopadhyay, VECC, Kolkata.
5  *************************************************
6  *
7  * Description: Base class for Pmd Calibration Constant Maker
8  *
9  *************************************************
10  *
11  * Initial version : 28th aug 2003
12  * The code is tested to work in simulation chain, and tested with
13  * data taken with PMD last year. Numbers are written to DB, and tested
14  * to read them back.
15  *
16  * Some changes are forseen in data storing once a large amount
17  * PMD data will be run to generate the numbers.
18  *
19  *************************************************/
20 
21 /* !
22 
23 This Maker obtains calibrations constants for all cells in PMD and CPV.
24 This collects MIP spectra for each cell and at the end it fits them with
25 Landau function and then generates the calibration constants. these calibration
26 constants are then stored in calibration database
27 */
28 
29 #include"Stsstream.h"
30 #include<assert.h>
31 #include<math.h>
32 #include"TROOT.h"
33 #include"TMath.h"
34 #include<TRandom.h>
35 #include<StMessMgr.h>
36 #include<TFile.h>
37 #include<TF1.h>
38 #include "StPmdUtil/StPmdGeom.h"
39 #include "StPmdCalibConstMaker.h"
40 #include "StPmdUtil/StPmdDetector.h"
41 #include "StPmdUtil/StPmdModule.h"
42 #include "StPmdUtil/StPmdCollection.h"
43 #include "StPmdUtil/StPmdHit.h"
44 //...........................................................
45 #include "TStopwatch.h"
46 #include "StarClassLibrary/SystemOfUnits.h"
47 #include "time.h"
48 #include "TDatime.h"
49 
50 #include "StDbLib/StDbManager.hh"
51 #include "StDbLib/StDbTable.h"
52 #include "StDbLib/StDbConfigNode.hh"
53 
54 #ifndef ST_NO_NAMESPACES
55 using units::tesla;
56 #endif
57 
58 
59 
60 ClassImp(StPmdCalibConstMaker)
61 
62 
69  Double_t MipArray[PMD_CRAMS_MAX*2][PMD_ROW_MAX][PMD_COL_MAX][MIP_CH_MAX];
70 
71  //___________________------------
73 Int_t StPmdCalibConstMaker::neibx[PMD_CELL_NEIGHBOUR] = {1,0,-1,-1,0,1};
74 
75 Int_t StPmdCalibConstMaker::neiby[PMD_CELL_NEIGHBOUR] = {0,1,1,0,-1,-1};
76 
77 Int_t StPmdCalibConstMaker::imax[2*PMD_CRAMS_MAX] = {48,72,48,72,72,72,48,48,48,72,72,96,48,72,48,72,72,72,48,48,48,72,72,96};
78 //for col
79 Int_t StPmdCalibConstMaker::jmax[2*PMD_CRAMS_MAX] = {48,48,72,72,48,48,48,72,48,72,48,72,48,48,72,72,48,48,48,72,48,72,48,72};
80 //for row
81 
82 //--------------------
83 StPmdCalibConstMaker::StPmdCalibConstMaker(const char *name):StMaker(name)
84 {
85  // mSaveCalibToDB = kFALSE; //To be checked
86  mSaveCalibToDB = kTRUE;
87  mOptHist = kTRUE;
88  mDate=0;
89  mTime=0;
90  InitMipParams();
91  mPmdGeom=new StPmdGeom();
92  mPmdDbUtil=new StPmdDBUtil();
93 }
94 //----------------------
95 
97 {
98  cout << "**** I am in StPmdCalibConstMaker::~StPmdCalibConstMaker()"<< endl;
99 
100  if(mPmdGeom){mPmdGeom=0;delete mPmdGeom;}
101  if(mPmdDbUtil){mPmdDbUtil=0;delete mPmdDbUtil;}
102  if(mOptHist)ClearHists();
103 }
104 
105 //---------------------
107 {
108  //Initialize date and time
109  if(mOptHist)BookHistograms();
110 
111  //---------------------------------------------------
112  // Newly included from this
113 
121  ClearMipArray();
122 
123  return StMaker::Init();
124 }
125 
126 //--------------------------------------
127 void StPmdCalibConstMaker::BookHistograms()
128 {
129 
130  for(Int_t sm=0;sm<2*PMD_CRAMS_MAX;sm++){
131  for(Int_t i =0;i<imax[sm];i++){
132  for(Int_t j =0;j<jmax[sm];j++){
133  char text[40],title[80];
134  sprintf(text,"calib_%02d_%02d_%02d",sm,i,j);
135  sprintf(title,"MIP plot for %02d_%02d_%2d",sm,i,j);
136  mMipEnergy[sm][i][j]=new TH1F(text,title,200,0.,150.);
137  }
138  }
139  }
140 }
141 
142 
143 //-------------------------------------------
144 
146 {
147  if (mDate==0) {mDate = GetDate();}
148  if (mTime==0) {mTime = GetTime();}
149 
150  TDataSet* CalibIn = GetDataSet("pmdReader");
151  if (!CalibIn){CalibIn = GetDataSet("PmdSimulator");}
152  if (CalibIn){
153  StPmdCollection *CalibHit = (StPmdCollection*)CalibIn->Find("PmdCollection");
154  if(CalibHit){
155  StPmdDetector * cpv_det = CalibHit->detector(Int_t(1));
156  StPmdDetector * pmd_det = CalibHit->detector(Int_t(0));
157  GetIsoHit(pmd_det,cpv_det);
158  }
159  }
160  else{cout<<"Could not find Simulator/Reader"<<endl; return kStOK;}
161  return kStOK;
162 }
163 
164 
165 void StPmdCalibConstMaker::GetIsoHit(StPmdDetector* pmd_det,
166  StPmdDetector* cpv_det)
167 {
170  Int_t xpad,ypad,gsuper,idet;
171  Float_t edep;
172  Float_t d0[PMD_ROW_MAX][PMD_COL_MAX];
173 
174  if(cpv_det){
175  for(Int_t id=0;id<PMD_CRAMS_MAX;id++){
176  for(Int_t j=0;j<PMD_ROW_MAX;j++){
178  for(Int_t k=0;k<PMD_COL_MAX;k++){
179  d0[j][k]=0;
180  }
181  }
182 
183 
184  StPmdModule * cpv_mod=cpv_det->module(id);
185  if(cpv_det->module_hit(id)>0){
186  Int_t nmh=cpv_det->module_hit(id);
187  TIter next(cpv_mod->Hits());
188  StPmdHit *spmhit0;
189  for(Int_t im=0; im<nmh; im++){
190  spmhit0 = (StPmdHit*)next();
191  if(spmhit0)
192  {
193  ypad=spmhit0->Row();
194  xpad=spmhit0->Column();
195  edep=spmhit0->Edep();
196  Int_t adc=spmhit0->Adc();
197  gsuper = spmhit0->Gsuper();
198  idet=spmhit0->SubDetector();
199  if(idet==2){
200 if((xpad>0 && xpad<=PMD_ROW_MAX) && (ypad>0 && ypad <=PMD_COL_MAX))d0[xpad-1][ypad-1]=adc;
201  }
202  }
203  }
204 
206 
207  for(Int_t i=0;i<imax[id];i++){
208  for(Int_t j=0;j<jmax[id];j++){
209  if(d0[i][j] > 0.){
210  Int_t count = 0;
211  for(Int_t ii=0;ii<PMD_CELL_NEIGHBOUR;ii++){
212  Int_t id1 = i + neibx[ii];
213  Int_t jd1 = j + neiby[ii];
214  if(d0[id1][jd1]==0.){
215  count++;
216  if(count == PMD_CELL_NEIGHBOUR){//finding isolated cell
217  gsuper = spmhit0->Gsuper();
218  gsuper = gsuper-1;
219  if(mOptHist)mMipEnergy[gsuper][i][j]->Fill(d0[i][j]);
220  Int_t channel=(Int_t)d0[i][j];
221  if(channel<MIP_CH_MAX)MipArray[gsuper][i][j][channel]++;
222 
223  }
224  }
225  }
226  }
227  }
228  }
229 
230  }// if-- module hit
231  }// for supermodule
232  }// if---CPV detector
233 
234 
235 
236  Float_t d1[PMD_ROW_MAX][PMD_COL_MAX];
237 
238  if(pmd_det){
239  for(Int_t id=0;id<PMD_CRAMS_MAX;id++){
240  for(Int_t j=0;j<PMD_ROW_MAX;j++){
242  for(Int_t k=0;k<PMD_COL_MAX;k++){
243  d1[j][k]=0;
244  }
245  }
246 
247  StPmdModule * pmd_mod=pmd_det->module(id);
248  if(pmd_det->module_hit(id)>0){
249  Int_t nmh=pmd_det->module_hit(id);
250  TIter next(pmd_mod->Hits());
251  StPmdHit *spmhit1;
252  for(Int_t im=0; im<nmh; im++){
253  spmhit1 = (StPmdHit*)next();
254  if(spmhit1)
255  {
256  ypad=spmhit1->Row();
257  xpad=spmhit1->Column();
258  edep=spmhit1->Edep();
259  Int_t adc=spmhit1->Adc();
260  gsuper = spmhit1->Gsuper();
261  idet=spmhit1->SubDetector();
262  if(idet==1){
263 if((xpad>0 && xpad<=PMD_ROW_MAX) && (ypad>0 && ypad <=PMD_COL_MAX))d1[xpad-1][ypad-1]=adc;
264  }
265  }
266  }
267 
269 
270  for(Int_t i=0;i<imax[id];i++){
271  for(Int_t j=0;j<jmax[id];j++){
272  if(d1[i][j] > 0.){
273  Int_t count = 0;
274  for(Int_t ii=0;ii<PMD_CELL_NEIGHBOUR;ii++){
275  Int_t id1 = i + neibx[ii];
276  Int_t jd1 = j + neiby[ii];
277  if(d1[id1][jd1]==0.){
278  count++;
279  if(count == PMD_CELL_NEIGHBOUR){//finding isolated cell
280  gsuper = spmhit1->Gsuper();
281  gsuper = gsuper + 11;
282  if(mOptHist)mMipEnergy[gsuper][i][j]->Fill(d1[i][j]);
283  Int_t channel=(Int_t)d1[i][j];
284  if(channel<MIP_CH_MAX)MipArray[gsuper][i][j][channel]++;;
285 
286  }
287  }
288  }
289  }
290  }
291  }
292 
293  }// if-- module hit
294  }// for supermodule
295  }// if---PMD detector
296 }//void
297 
299 {
300  Float_t MipFitParam[3];
301  Float_t MipFitChiSqr, MipPeakPosition, MipPeakWidth;
302  TF1 LandauFunction("LandauFunction","landau",0,100);
303  LandauFunction.SetParameters(1,1,1);
304  LandauFunction.SetParNames("Constant","MPV","Sigma");
305  Float_t MPV_Sum = 0;
306  Int_t MPV_Count = 0;
307  TH1F * miphist;
308  Stat_t entryFlag=0;
309  for(Int_t sm=0;sm<2*PMD_CRAMS_MAX;sm++){
310  for(Int_t i =0;i<imax[sm];i++){
311  for(Int_t j =0;j<jmax[sm];j++){
312  if(!mOptHist){
313  miphist=new TH1F("miphist","MipHist",200,0.,140.);
314  for(Int_t k =0;k<10;k++){
315  miphist->SetBinContent(k+1,(Stat_t)MipArray[sm][i][j][k]);
316  }
317  entryFlag = miphist->GetEntries();
318  }
319  if(mOptHist)entryFlag = mMipEnergy[sm][i][j]->GetEntries();
320 
321  //if(entryFlag > 5){
322  if(entryFlag > MIP_MIN_ENTRY){ // new
323  if(mOptHist)mMipEnergy[sm][i][j]->Fit("LandauFunction","q","r");
324  if(!mOptHist)miphist->Fit("LandauFunction","q","r");
325  for(Int_t m=0; m < 3; m++){
326  MipFitParam[m] = LandauFunction.GetParameter(m);
327  }
328 
329  if(MipFitParam[1] > 0.){
330  MPV_Sum = MPV_Sum + MipFitParam[1];
331  MPV_Entry[sm][i][j] = MipFitParam[1];
332  MPV_Count++;
333  }
334  MipFitChiSqr = LandauFunction.GetChisquare();
335  //Float_t ndf = func->GetNDF();
336  if(mOptHist){
337  MipPeakPosition = mMipEnergy[sm][i][j]->GetMean();
338  MipPeakWidth = mMipEnergy[sm][i][j]->GetRMS();
339  }
340  else{
341  MipPeakPosition = miphist->GetMean();
342  MipPeakWidth = miphist->GetRMS();
343  }
344 
345  Int_t BrdNo=0;
346  mPmdDbUtil->BoardNumber(sm,i,j,BrdNo);
347  Int_t BrdCh=0;
348  mPmdDbUtil->ChannelInBoard(sm,i,j,BrdCh);
349 
350  //mMipPeak[BrdNo][BrdCh]=MipPeakPosition;//!new
351  mMipWidth[BrdNo][BrdCh]=MipPeakWidth;
352 
353  }
354  if(!mOptHist)delete miphist;
355  } // ! col
356  }
357  }
358 
359  Float_t MPV_Av = MPV_Sum/MPV_Count;
360  // Float_t MPV_Av = 4.5;
361 
362  for(Int_t sm=0;sm<2*PMD_CRAMS_MAX;sm++){
363  for(Int_t i =0;i<imax[sm];i++){
364  for(Int_t j =0;j<jmax[sm];j++){
365  if(MPV_Entry[sm][i][j] > 0.)
366  normFactor[sm][i][j] = (Float_t) MPV_Av/(Float_t) MPV_Entry[sm][i][j];
367  Int_t BrdNo=0;
368  mPmdDbUtil->BoardNumber(sm,i,j,BrdNo);
369  Int_t BrdCh=0;
370  mPmdDbUtil->ChannelInBoard(sm,i,j,BrdCh);
371 
372  mMipPeak[BrdNo][BrdCh]=normFactor[sm][i][j];
373  }
374  }
375  }
376  return kStOK;
377 }
378 
380 {
381  cout << " *** I am in StPmdCalibConstMaker::Finish() " << endl;
382 
383  TStopwatch clock;
384  clock.Start();
385 
387  if(mSaveCalibToDB)SaveCalibration();
388 
389  gMessMgr->Info("StPmdCalibConstMaker::Finish()");
390  return StMaker::Finish();
391  //return kStOK;
392 }
393 
394 //_____________________________________________________________________________
395 void StPmdCalibConstMaker::SaveCalibration()
396 {
397 
398  cout <<" ***** I am now saving the Calibration to DB : Wait !!" << endl;
399  // TDatime *tt = new TDatime(date,time);
400  // TString timestamp = tt->AsSQLString();
401  // delete tt;
402 
403  // if(!mSaveCalibToDB) return;
404  /* Presently it is storing some numbers , coming from simulated files,
405  it is tested with daq file, and time stamp works., so once we have good
406  amount fo data , it should work*/
407 
408 
410  StDbConfigNode* node=mgr->initConfig("Calibrations_pmd");
411  StDbTable* tab1=node->addDbTable("pmdBrdMipCalib");
412  StDbTable* tab2=node->addDbTable("pmdCalSummary");
413  pmdBrdMipCalib_st pbd[PMD_BOARD_MAX];
414  memset(pbd,0,PMD_BOARD_MAX*sizeof(pmdBrdMipCalib_st));
415  for(Int_t ism=0;ism<2*PMD_CRAMS_MAX;ism++){
416  for(Int_t irow=0;irow<jmax[ism];irow++){
417  for(Int_t icol=0;icol<imax[ism];icol++){
418  Int_t brd=0;
419  Int_t brd_ch=0;
420  mPmdDbUtil->BoardNumber(ism,irow,icol,brd);
421  mPmdDbUtil->ChannelInBoard(ism,irow,icol,brd_ch);
422  pbd[brd].FEEBoardNumber = brd;
423  //filling peak and width from arrays
424 
425  pbd[brd].MipPeakPosition[brd_ch] = mMipPeak[brd][brd_ch];
426  pbd[brd].MipPeakWidth[brd_ch] = mMipWidth[brd][brd_ch];
427  }
428  }
429  }
430  tab1->SetTable((char*)pbd,PMD_BOARD_MAX);
431 
432  pmdCalSummary_st pcs[1];
433  memset(pcs,0,sizeof(pmdCalSummary_st));
434  // Summary table will be filled here.
435 
436  tab2->SetTable((char*)pcs,1);
437 
438  //storing timestamp from the first event in the chain
439  char timestamp[15];
440  sprintf(timestamp,"%08d%06d",mDate,mTime);
441  mgr->setStoreTime(timestamp);
442  //mgr->setStoreTime("2003-12-29 11:11:15");
443  cout<<"PMD Calib: storing in DB"<<endl;
444 
445  mgr->storeDbTable(tab1);
446  mgr->storeDbTable(tab2);
447  cout<<"PMD Calib: Stored in DB "<<endl;
448 
449 }
450 
451 
452 void StPmdCalibConstMaker::InitMipParams()
453 {
454  for(Int_t i=0;i<PMD_BOARD_MAX;i++){
455  for(Int_t j=0;j<PMD_BOARD_CH_MAX;j++){
456  mMipPeak[i][j]=0.;
457  mMipWidth[i][j]=0.;
458 
459  }
460  }
461 }
462 
463 void StPmdCalibConstMaker::ClearHists()
464 {
465  cout << " *** Let me to clear the Histograms in PmdCalib" << endl;
466 
467  for(Int_t sm=0;sm<2*PMD_CRAMS_MAX;sm++){
468  for(Int_t i =0;i<imax[sm];i++){
469  for(Int_t j =0;j<jmax[sm];j++){
470 
471  if(mMipEnergy[sm][i][j]){
472  mMipEnergy[sm][i][j]=0;
473  delete mMipEnergy[sm][i][j];
474  }
475  }
476  }
477  }
478 }
479 void StPmdCalibConstMaker::ClearMipArray()
480 {
481  for(Int_t sm=0;sm<2*PMD_CRAMS_MAX;sm++){
482  for(Int_t i =0;i<imax[sm];i++){
483  for(Int_t j =0;j<jmax[sm];j++){
484  for(Int_t k =0;k<MIP_CH_MAX;k++){
485  MipArray[sm][i][j][k]=0;
486  }
487  }
488  }
489  }
490 }
491 
492 
493 
494 
495 
496 
497 
498 
499 
500 
501 
Int_t module_hit(Int_t)
module number
StPmdDetector * detector(Int_t)
destructor
TH1F * mMipEnergy[2 *PMD_CRAMS_MAX][PMD_ROW_MAX][PMD_COL_MAX]
booking Pmd cluster histograms
virtual Int_t Init()
Init method.
virtual ~StPmdCalibConstMaker()
Default destructor.
Float_t normFactor[2 *PMD_CRAMS_MAX][PMD_ROW_MAX][PMD_COL_MAX]
deposited energy of isolated cells
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc&#39;d version of data for StRoot
Definition: StDbTable.cc:550
virtual Int_t Make()
Make mathod - process each event.
Definition: Stypes.h:40
virtual Int_t Finish()
Finish method - save final numbers.
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155
virtual Int_t Finish()
Definition: StMaker.cxx:776
StPmdModule * module(unsigned int)
number of hits
virtual Int_t FindMipParameters()
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362