StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFcsDb.cxx
1 /***************************************************************************
2  * $id: StFcsDb.cxx,v 1.22 2020/12/17 21:01:04 akio Exp $
3  * \author: akio ogawa
4  ***************************************************************************
5  *
6  * Description: This interface between FCS and the STAR database
7  *
8  ***************************************************************************
9  *
10  * $Log: StFcsDb.cxx,v $
11  * Revision 1.3 2021/05/27 14:02:22 akio
12  * clean up Clear and fixGain/corr
13  *
14  * Revision 1.2 2021/04/09 15:11:18 akio
15  * Adding projection of Hcal local position to Ecal local position
16  *
17  * Revision 1.1 2021/03/30 13:40:07 akio
18  * FCS code after peer review and moved from $CVSROOT/offline/upgrades/akio
19  *
20  * Revision 1.32 2021/02/25 21:53:50 akio
21  * Int_t -> int
22  *
23  * Revision 1.31 2021/02/24 22:56:19 akio
24  * Modified for STAR code review (Dmitry)
25  *
26  * Revision 1.30 2021/02/23 22:18:23 akio
27  * Modified for STAr code review (Jason)
28  *
29  * Revision 1.29 2021/02/12 20:09:50 akio
30  * Adding getIdfromSCmap()
31  *
32  * Revision 1.28 2021/02/09 21:54:23 akio
33  * Using StEnumeration
34  *
35  * Revision 1.27 2021/02/05 17:23:25 akio
36  * Adding access to STAR offline DB tables.
37  * Adding getFromName/getDetFromName from David.
38  *
39  * Revision 1.26 2021/01/05 18:15:01 akio
40  * added setPedestal()
41  *
42  * Revision 1.25 2020/12/30 20:45:20 akio
43  * fix format
44  *
45  * Revision 1.24 2020/12/30 20:34:38 akio
46  * also modify getName for DEP
47  *
48  * Revision 1.23 2020/12/30 20:17:55 akio
49  * adding SC map access
50  *
51  * Revision 1.22 2020/12/17 21:01:04 akio
52  * fix slt problem in sc map
53  *
54  * Revision 1.21 2020/09/03 19:43:20 akio
55  * Updating SC map and adding patchpanel & cable color map
56  *
57  * Revision 1.20 2020/07/24 17:23:31 akio
58  * EPD mip value from 1.6MeV to 2.0MeV
59  *
60  * Revision 1.19 2020/05/29 18:53:40 akio
61  * Adding EPD as PRES maps, STAR coordinate for 4x4 trigger patch, renming map files to be used for DAQ as Tonko specifies
62  *
63  * Revision 1.18 2020/05/04 15:49:39 akio
64  * adding gain for EPD as PRES
65  *
66  * Revision 1.17 2020/05/04 15:48:22 akio
67  * adding input file for DAQ
68  *
69  * Revision 1.16 2019/10/23 20:05:39 akio
70  * bug fixed for getOffset for det=1
71  *
72  * Revision 1.15 2019/10/23 19:20:10 akio
73  * fix det=0 bug for getGain/gaincorr
74  *
75  * Revision 1.14 2019/10/23 13:34:38 akio
76  * Adding getZDepth, and take out Ecal front space (for SiPM/Fee) from offsets
77  * so that x/z offsets are now pointing to actual ecal tower front & near beam corner.
78  *
79  * Revision 1.13 2019/08/01 18:36:06 akio
80  * Bug fix which was causing id=0 to get gain=1.0 always
81  *
82  * Revision 1.12 2019/07/10 06:13:34 akio
83  * Adding reading of gains from text files
84  *
85  * Revision 1.11 2019/07/08 15:53:28 akio
86  * updating sampling fraction number by Ting & Liu's study
87  *
88  * Revision 1.10 2019/06/27 16:10:32 akio
89  * adding getLocalXYinCell
90  *
91  * Revision 1.9 2019/06/25 16:38:59 akio
92  * Fixed y offset for run19
93  * Added setting run# and time dependent (for preshower yoffset only for now)
94  *
95  * Revision 1.8 2019/06/21 17:28:47 akio
96  * dealing with 5cm offsent when leakyHcal
97  *
98  * Revision 1.7 2019/06/07 18:16:54 akio
99  * *** empty log message ***
100  *
101  * Revision 1.6 2019/05/16 16:08:32 akio
102  * going back to 2019 to full gepmetry
103  *
104  * Revision 1.5 2019/03/22 14:28:35 akio
105  * adding map for 2019
106  *
107  * Revision 1.4 2019/03/13 20:46:19 akio
108  * formatting
109  *
110  * Revision 1.3 2019/03/13 20:29:30 akio
111  * update for run19
112  *
113  * Revision 1.2 2019/02/05 22:00:18 akio
114  * fix NorthSouth()
115  *
116  * Revision 1.1 2018/11/14 16:50:13 akio
117  * FCS codes in offline/upgrade/akio
118  *
119  *
120  **************************************************************************/
121 
122 #include "StFcsDb.h"
123 #include "StMaker.h"
124 #include "StMessMgr.h"
125 #include "StEvent/StFcsHit.h"
126 #include "StEvent/StFcsCluster.h"
127 #include "StEvent/StFcsPoint.h"
128 #include <math.h>
129 
130 namespace {
131  //Gain factors
132  float mEtGain[kFcsNDet][kFcsMaxId];
133 
134  //DEP map
135  short mMap_ehp[kFcsNDet][kFcsMaxId];
136  short mMap_ns [kFcsNDet][kFcsMaxId];
137  short mMap_crt[kFcsNDet][kFcsMaxId];
138  short mMap_slt[kFcsNDet][kFcsMaxId];
139  short mMap_dep[kFcsNDet][kFcsMaxId];
140  short mMap_ch [kFcsNDet][kFcsMaxId];
141  short mMap_ppb[kFcsNDet][kFcsMaxId];
142  short mMap_ppp[kFcsNDet][kFcsMaxId];
143  short mMap_pph[kFcsNDet][kFcsMaxId];
144  short mMap_wcol[kFcsNDet][kFcsMaxId];
145  short mMap_jcol[kFcsNDet][kFcsMaxId];
146 
147  //Reverse map
148  short mRMap_det[kFcsEHP][kFcsNorthSouth][kFcsMaxDepBd][kFcsMaxDepCh];
149  short mRMap_id [kFcsEHP][kFcsNorthSouth][kFcsMaxDepBd][kFcsMaxDepCh];
150  short mRMap_crt[kFcsEHP][kFcsNorthSouth][kFcsMaxDepBd][kFcsMaxDepCh];
151  short mRMap_slt[kFcsEHP][kFcsNorthSouth][kFcsMaxDepBd][kFcsMaxDepCh];
152 
153  //SC map
154  const unsigned short kFcsMaxBranch=2;
155  const unsigned short kFcsMaxAddr=16;
156  const unsigned short kFcsMaxSiPM=4;
157  short mScMap_ehp[kFcsNDet][kFcsMaxId];
158  short mScMap_ns[kFcsNDet][kFcsMaxId];
159  short mScMap_dep[kFcsNDet][kFcsMaxId];
160  short mScMap_bra[kFcsNDet][kFcsMaxId];
161  short mScMap_add[kFcsNDet][kFcsMaxId];
162  short mScMap_sipm[kFcsNDet][kFcsMaxId];
163  short mScMap_pp[kFcsNDet][kFcsMaxId];
164  short mScMap_j[kFcsNDet][kFcsMaxId];
165 
166  //Reverse SC map
167  short mRScMap_det[kFcsEHP][kFcsNorthSouth][kFcsMaxDepBd][kFcsMaxBranch][kFcsMaxAddr][kFcsMaxSiPM];
168  short mRScMap_id[kFcsEHP][kFcsNorthSouth][kFcsMaxDepBd][kFcsMaxBranch][kFcsMaxAddr][kFcsMaxSiPM];
169 
170  //PatchPanel Map
171  const short EPPMap[8][6][3]={ // {dep#,low_ch/high_ch,pwr&ctrl row#}
172  {{20, 0, 1},{20, 1,-1}, //PPB1 P2,P3
173  { 0, 0, 2},{ 0, 1,-1}, //PPB1 P4,P5
174  { 1, 0, 3},{ 1, 1,-1}}, //PPB1 P6,P7
175  {{ 2, 0,-2},{ 2, 1,-1}, //PPB2 P2,P3
176  { 3, 0,-2},{ 3, 1,-1}, //PPB2 P4,P5
177  { 4, 0,-2},{ 4, 1,-1}}, //PPB2 P6,P7
178  {{22, 0, 4},{23, 0,-1}, //PPB3 P2,P3
179  { 5, 0, 5},{ 5, 1,-1}, //PPB3 P4,P5
180  { 6, 0, 6},{ 6, 1,-1}}, //PPB3 P6,P7
181  {{ 7, 0, 7},{ 7, 1,-1}, //PPB4 P2,P3
182  { 8, 0, 8},{ 8, 1,-1}, //PPB4 P4,P5
183  { 9, 0, 9},{ 9, 1,-1}}, //PPB4 P6,P7
184  {{10, 0,10},{10, 1,-1}, //PPB5 P2,P3
185  {11, 0,11},{11, 1,-1}, //PPB5 P4,P5
186  {12, 0,12},{12, 1,-1}}, //PPB5 P6,P7
187  {{13, 0,13},{13, 1,-1}, //PPB6 P2,P3
188  {14, 0,14},{14, 1,-1}, //PPB6 P4,P5
189  {22, 1,15},{23, 1,-1}}, //PPB6 P6,P7
190  {{15, 0,-2},{15, 1,-1}, //PPB7 P2,P3
191  {16, 0,-2},{16, 1,-1}, //PPB7 P4,P5
192  {17, 0,-2},{17, 1,-1}}, //PPB7 P6,P7
193  {{18, 0,16},{18, 1,-1}, //PPB8 P2,P3
194  {19, 0,17},{19, 1,-1}, //PPB8 P4,P5
195  {21, 0,-1},{21, 1,-1}} //PPB8 P6,P7
196  };
197  const short HPPMap[4][6][3]={ // {dep#,low_ch/high_ch,pwr&ctrl row#}
198  {{ 6, 0, 1},{ 6, 1,-1}, //PPB1 P2,P3
199  { 0, 0, 2},{ 1, 0,-1}, //PPB1 P4,P5
200  {-1,-1,-1},{-1,-1,-1}}, //PPB1 P6,P7
201  {{ 2, 0, 3},{ 0, 1,-1}, //PPB2 P2,P3
202  { 1, 1, 4},{ 2, 1,-1}, //PPB2 P4,P5
203  { 8, 0, 5},{-1,-1,-1}}, //PPB2 P6,P7
204  {{ 8, 1, 6},{-1,-1,-1}, //PPB3 P2,P3
205  { 3, 0, 7},{ 4, 0,-1}, //PPB3 P4,P5
206  { 5, 0, 8},{ 3, 1,-1}}, //PPB3 P6,P7
207  {{ 4, 1, 9},{ 5, 1,-1}, //PPB4 P2,P3
208  { 7, 0,10},{ 7, 1,-1}, //PPB4 P4,P5
209  {-1,-1,-1},{-1,-1,-1}} //PPB4 P6,P7
210  };
211 
212  short EMapPPB[24][2];
213  short EMapPPP[24][2];
214  short EMapSCDEP[17];
215  short EMapSCBRA[17];
216  short EMapSCPP[17];
217  short EMapSCJ[17];
218  short HMapPPB[24][2];
219  short HMapPPP[24][2];
220  short HMapSCDEP[10];
221  short HMapSCBRA[10];
222  short HMapSCPP[10];
223  short HMapSCJ[10];
224 
225  const char* colW[4]={"Green ","Brown ","Orange","Blue "};
226  const char* colJ[8]={"Blue ","Orange","Violet","Black ",
227  "Yellow","Red ","Grey ","Blue "};
228  const float leng[8]={ 6.5, 6.5, 5.0, 5.0,
229  3.5, 3.5, 8.0, 8.0};
230  const char* colJH[8]={"Red ","Grey ","Orange","Yellow",
231  "Orange","Blue ","Red ","Yellow"};
232  const float lengH[8]={ 6.5, 5.0, 5.0, 5.0,
233  6.5, 5.0, 5.0, 5.0};
234 
235  const char* EHP[3]={"Ecal","Hcal","Pres"};
236  const char* CRT[5]={"EN","MN","MA","MS","ES"};
237  const char* DET[6]={"EN","ES","HN","HS","PN","PS"};
238 }
239 
240 ClassImp(StFcsDb)
241 
242 StFcsDb::StFcsDb(const char *name) : TDataSet(name) {};
243 
244 StFcsDb::~StFcsDb() {}
245 
246 int StFcsDb::Init(){
247  return kStOK;
248 }
249 
250 void StFcsDb::setDbAccess(int v) {mDbAccess = v;}
251 void StFcsDb::setRun(int run) {mRun = run;}
252 void StFcsDb::setRun19(int v) {mRun19=v;}
253 void StFcsDb::setLeakyHcal(int v) {mLeakyHcal=v;}
254 
255 void StFcsDb::setFcsDetectorPosition(fcsDetectorPosition_st* t){
256  if(!t) { memset(&mFcsDetectorPosition,0,sizeof(fcsDetectorPosition_st)); }
257  else { memcpy(&mFcsDetectorPosition,t,sizeof(fcsDetectorPosition_st)); }
258 }
259 
260 void StFcsDb::setFcsEcalGain(fcsEcalGain_st* t){
261  if(!t) { memset(&mFcsEcalGain,0,sizeof(fcsEcalGain_st)); }
262  else { memcpy(&mFcsEcalGain,t,sizeof(fcsEcalGain_st)); }
263 }
264 
265 void StFcsDb::setFcsHcalGain(fcsHcalGain_st* t){
266  if(!t) { memset(&mFcsHcalGain,0,sizeof(fcsHcalGain_st)); }
267  else { memcpy(&mFcsHcalGain,t,sizeof(fcsHcalGain_st)); }
268 }
269 
270 void StFcsDb::setFcsPresGain(fcsPresGain_st* t){
271  if(!t) { memset(&mFcsPresGain,0,sizeof(fcsPresGain_st)); }
272  else { memcpy(&mFcsPresGain,t,sizeof(fcsPresGain_st)); }
273 }
274 
275 void StFcsDb::setFcsEcalGainCorr(fcsEcalGainCorr_st* t){
276  if(!t) { memset(&mFcsEcalGainCorr,0,sizeof(fcsEcalGainCorr_st)); }
277  else { memcpy(&mFcsEcalGainCorr,t,sizeof(fcsEcalGainCorr_st)); }
278 }
279 
280 void StFcsDb::setFcsHcalGainCorr(fcsHcalGainCorr_st* t){
281  if(!t) { memset(&mFcsHcalGainCorr,0,sizeof(fcsHcalGainCorr_st)); }
282  else { memcpy(&mFcsHcalGainCorr,t,sizeof(fcsHcalGainCorr_st)); }
283 }
284 
285 void StFcsDb::setFcsPresValley(fcsPresValley_st* t){
286  if(!t) { memset(&mFcsPresValley,0,sizeof(fcsPresValley_st)); }
287  else { memcpy(&mFcsPresValley,t,sizeof(fcsPresValley_st)); }
288 }
289 
290 void StFcsDb::setFcsEcalGainOnline(fcsEcalGainOnline_st* t){
291  if(!t) { memset(&mFcsEcalGainOnline,0,sizeof(fcsEcalGainOnline_st)); }
292  else { memcpy(&mFcsEcalGainOnline,t,sizeof(fcsEcalGainOnline_st)); }
293 }
294 
295 void StFcsDb::setFcsHcalGainOnline(fcsHcalGainOnline_st* t){
296  if(!t) { memset(&mFcsHcalGainOnline,0,sizeof(fcsHcalGainOnline_st)); }
297  else { memcpy(&mFcsHcalGainOnline,t,sizeof(fcsHcalGainOnline_st)); }
298 }
299 
300 void StFcsDb::setFcsPresThreshold(fcsPresThreshold_st* t){
301  if(!t) { memset(&mFcsPresThreshold,0,sizeof(fcsPresThreshold_st)); }
302  else { memcpy(&mFcsPresThreshold,t,sizeof(fcsPresThreshold_st)); }
303 }
304 
305 int StFcsDb::InitRun(int runNumber) {
306  LOG_INFO << "StFcsDb::InitRun - run = " << runNumber << endm;
307  mRun=runNumber;
308  if(mEtGainMode==0){
309  if(20000000<mRun && mRun<23027048) mEtGainMode=1;
310  else mEtGainMode=2;
311  }
312 
313  if(mRun19>0){
314  makeMap2019();
315  }else{
316  makeMap();
317  printEtGain();
318  }
319  if(mDebug>0) {
320  printMap();
321  }
322 
323  //storing in DEP sorted table
324  if(mGainMode==GAINMODE::DB){
325  int ie=0, ih=0, ip=0, ehp, ns, crt, slt, dep, ch;
326  for(int ins=0; ins<kFcsNorthSouth; ins++){
327  int det=kFcsEcalNorthDetId+ins;
328  for(int id=0; id<maxId(det); id++){
329  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
330  mGain[ehp][ns][dep][ch]=mFcsEcalGain.gain[ie];
331  ie++;
332  }
333  det=kFcsHcalNorthDetId+ins;
334  for(int id=0; id<maxId(det); id++){
335  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
336  mGain[ehp][ns][dep][ch]=mFcsHcalGain.gain[ih];
337  ih++;
338  }
339  det=kFcsPresNorthDetId+ins;
340  for(int id=0; id<maxId(det); id++){
341  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
342  mGain[ehp][ns][dep][ch]=mFcsPresGain.gain[ip];
343  ip++;
344  }
345  }
346  }else if(mGainMode==GAINMODE::TXT){
347  readGainFromText();
348  }
349 
350  if(mGainCorrMode==GAINMODE::DB){
351  int ie=0, ih=0, ip=0, ehp, ns, crt, slt, dep, ch;
352  for(int ins=0; ins<kFcsNorthSouth; ins++){
353  int det=kFcsEcalNorthDetId+ins;
354  for(int id=0; id<maxId(det); id++){
355  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
356  mGainCorr[ehp][ns][dep][ch]=mFcsEcalGainCorr.gaincorr[ie];
357  ie++;
358  }
359  det=kFcsHcalNorthDetId+ins;
360  for(int id=0; id<maxId(det); id++){
361  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
362  mGainCorr[ehp][ns][dep][ch]=mFcsHcalGainCorr.gaincorr[ih];
363  ih++;
364  }
365  det=kFcsPresNorthDetId+ins;
366  for(int id=0; id<maxId(det); id++){
367  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
368  mGainCorr[ehp][ns][dep][ch]=mFcsPresValley.valley[ip];
369  ip++;
370  }
371  }
372  }else if(mGainCorrMode==GAINMODE::TXT){
373  readGainCorrFromText();
374  }
375 
376  int ie=0, ih=0, ip=0, ehp, ns, crt, slt, dep, ch;
377  for(int ins=0; ins<kFcsNorthSouth; ins++){
378  int det=kFcsEcalNorthDetId+ins;
379  for(int id=0; id<maxId(det); id++){
380  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
381  mGainOnline[ehp][ns][dep][ch]=mFcsEcalGainOnline.gainOnline[ie];
382  ie++;
383  }
384  det=kFcsHcalNorthDetId+ins;
385  for(int id=0; id<maxId(det); id++){
386  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
387  mGainOnline[ehp][ns][dep][ch]=mFcsHcalGainOnline.gainOnline[ih];
388  ih++;
389  }
390  det=kFcsPresNorthDetId+ins;
391  for(int id=0; id<maxId(det); id++){
392  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
393  mGainOnline[ehp][ns][dep][ch]=mFcsPresThreshold.threshold[ip];
394  ip++;
395  }
396  }
397 
398  // Get beamline
399  //TDataSet* dbDataSet = StMaker::GetChain()->GetDataBase("Calibrations/rhic/vertexSeed");
400  TDataSet* dbDataSet = 0;
401  if(dbDataSet){
402  vertexSeed_st* vSeed = ((St_vertexSeed*) (dbDataSet->FindObject("vertexSeed")))->GetTable();
403  if(vSeed){
404  mVx = vSeed->x0;
405  mVy = vSeed->y0;
406  mVdxdz = vSeed->dxdz;
407  mVdydz = vSeed->dydz;
408  mThetaX = TMath::ATan( mVdxdz );
409  mThetaY = TMath::ATan( mVdydz );
410  }
411  }
412 
413  return kStOK;
414 }
415 
416 int StFcsDb::maxDetectorId() const {return kFcsNDet;}
417 
418 int StFcsDb::detectorId(int eh, int ns) const {
419  if(eh>=0 && eh<kFcsEHP && ns>=0 && ns<kFcsNorthSouth) return eh*2 + ns;
420  return -1;
421 }
422 
423 int StFcsDb::ecalHcalPres(int det) const {
424  if(det==0 || det==1) return 0;
425  if(det==2 || det==3) return 1;
426  if(det==4 || det==5) return 2;
427  return -1;
428 }
429 
430 int StFcsDb::northSouth(int det) const{
431  return det%2;
432 }
433 
434 int StFcsDb::nRow(int det) const{
435  int ehp=ecalHcalPres(det);
436  if(mRun19>0){
437  int ns = northSouth(det);
438  if(ns==0) return 0;
439  if (ehp==0){return 8;}
440  else if(ehp==1){return 4;}
441  else if(ehp==2){return 9;}
442  return -1;
443  }else{
444  if (ehp==0){return kFcsEcalNRow;}
445  else if(ehp==1){return kFcsHcalNRow;}
446  else if(ehp==2){return kFcsPresNRow;}
447  return -1;
448  }
449 }
450 
451 int StFcsDb::nColumn(int det) const{
452  int ehp=ecalHcalPres(det);
453  if(mRun19>0){
454  int ns = northSouth(det);
455  if(ns==0) return 0;
456  if (ehp==0){return 8;}
457  else if(ehp==1){return 4;}
458  else if(ehp==2){return 1;}
459  return -1;
460  }else{
461  if (ehp==0){return kFcsEcalNCol;}
462  else if(ehp==1){return kFcsHcalNCol;}
463  else if(ehp==2){return kFcsPresNCol;}
464  return -1;
465  }
466 }
467 
468 int StFcsDb::maxId(int det) const{
469  int ehp=ecalHcalPres(det);
470  if(mRun19>0){
471  int ns = northSouth(det);
472  if(ns==0) return 0;
473  if (ehp==0){return 64;}
474  else if(ehp==1){return 16;}
475  else if(ehp==2){return 9;}
476  return -1;
477  }else{
478  if (ehp==0){return kFcsEcalMaxId;}
479  else if(ehp==1){return kFcsHcalMaxId;}
480  else if(ehp==2){return kFcsPresMaxId;}
481  return -1;
482  }
483 }
484 
485 int StFcsDb::getRowNumber(int det, int id) const{
486  if(id<0 || id>=maxId(det)) return -1;
487  return id/nColumn(det) + 1;
488 }
489 
490 int StFcsDb::getColumnNumber(int det, int id) const{
491  if(id<0 || id>=maxId(det)) return -1;
492  return id%nColumn(det) + 1;
493 }
494 
495 int StFcsDb::getId(int det, int row, int col) const{
496  if(row<=0 || row>nRow(det) || nRow(det)<0) return -1;
497  if(col<=0 || col>nColumn(det) || nRow(det)<0) return -1;
498  return col - 1 + nColumn(det)*(row-1);
499 }
500 
501 int StFcsDb::getDepCh(int dep, int ch) const{
502  return dep*kFcsMaxDepCh + ch;
503 }
504 
505 void StFcsDb::getName(int det, int id, char name[]) {
506  int ehp,ns,crt,slt,dep,ch;
507  int c=getColumnNumber(det,id);
508  int r=getRowNumber(det,id);
509  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
510  if(ehp<2){
511  int scehp,scns,scdep,br,i2c,sipm,pp,j;
512  getSCmap(det,id,scehp,scns,scdep,br,i2c,sipm,pp,j);
513  sprintf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_F%02d/%1d/%02d/%1d",
514  DET[det],id,r,c,dep,ch,scdep,br,i2c,sipm);
515  }else{
516  int pp,tt;
517  getEPDfromId(det,id,pp,tt);
518  sprintf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_PP%02d%1sTT%02d",
519  DET[det],id,r,c,dep,ch,pp,tt%2==0?"E":"O",tt);
520  }
521 }
522 
523 void StFcsDb::getName(int ehp, int ns, int dep, int ch, char name[]) {
524  int det,id,crt,slt;
525  getIdfromDep(ehp,ns,dep,ch,det,id,crt,slt);
526  if(id==-1){
527  det = detectorId(ehp, ns);
528  sprintf(name,"%2s---_r--c--_Dep%02dCh%02d_F--/-/--/-",
529  DET[det],dep,ch);
530  }else{
531  int c=getColumnNumber(det,id);
532  int r=getRowNumber(det,id);
533  if(ehp<2){
534  int scehp,scns,scdep,br,i2c,sipm,pp,j;
535  getSCmap(det,id,scehp,scns,scdep,br,i2c,sipm,pp,j);
536  sprintf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_F%02d/%1d/%02d/%1d",
537  DET[det],id,r,c,dep,ch,scdep,br,i2c,sipm);
538  }else{
539  int pp,tt;
540  getEPDfromId(det,id,pp,tt);
541  sprintf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_PP%02d%1s/TT%02d",
542  DET[det],id,r,c,dep,ch,pp,tt%2==0?"E":"O",tt);
543  }
544  }
545 }
546 
547 void StFcsDb::getFromName(const char name[], int &det, int &id){
548  char detname[5];
549  int r,c,dep,ch,scdep,br,i2c,sipm;
550  sscanf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_F%02d/%1d/%02d/%1d",
551  detname,&id,&r,&c,&dep,&ch,&scdep,&br,&i2c,&sipm);
552  det = getDetFromName(detname);
553  return;
554 }
555 
556 int StFcsDb::getDetFromName(const std::string& detname){
557  if ( detname=="EN") {return 0;}
558  else if( detname=="ES") {return 1;}
559  else if( detname=="HN") {return 2;}
560  else if( detname=="HS") {return 3;}
561  else if( detname=="PN") {return 4;}
562  else if( detname=="PS") {return 5;}
563  else {
564  LOG_ERROR << "ERROR:Invalid name for detector;Input:"<<detname<<endm;
565  return -1;
566  }
567 }
568 
569 unsigned short StFcsDb::getKey(unsigned short detid, unsigned short id){ return ( (detid & 0x7)<<12 | (id & 0xffff) ); }
570 void StFcsDb::getDetIdFromKey(unsigned short key, unsigned short& detid, unsigned short& id)
571 {
572  detid = (key >> 12) & 0x0007;
573  id = (key & 0x0fff);
574 }
575 unsigned short StFcsDb::getDetFromKey(unsigned short key){ return (key >> 12) & 0x0007; }
576 unsigned short StFcsDb::getIdFromKey(unsigned short key) { return (key & 0x0fff); }
577 
578 StThreeVectorD StFcsDb::getDetectorOffset(int det, double zdepth ) const{
579  if(mRun19>0){
580  const float bOffY=-(17.0*5.81); //40in=101.6cm and 17*5.81=98.76 so I will leave this unchanged
581  if(det==1) return StThreeVectorD( 25.25*2.54, bOffY + getYWidth(det)*nRow(det)/2.0, 710.16);
582  if(det==3) return StThreeVectorD( 27.50*2.54, bOffY + getYWidth(det)*nRow(det)/2.0, 782.63);
583  if(det==5){
584  //this is before June5
585  if(mRun<1105942) return StThreeVectorD( 14*2.54, bOffY + 2.875 + 2.54 + getYWidth(det)*nRow(det)/2.0, 700.00);
586  //this is afetr June5, moved up by 3.5inch for ecal cooling
587  if(mRun<1105951) return StThreeVectorD( 14*2.54, bOffY + 2.875 + 2.54 + getYWidth(det)*nRow(det)/2.0 + 3.5*2.54, 700.00);
588  //after June19, added 4 inches due to cooling & mounted on STGC supoort structure
589  return StThreeVectorD( 14*2.54, bOffY + 2.875 + 2.54 + getYWidth(det)*nRow(det)/2.0 + 4.0*2.54, 700.00);
590  }
591  return StThreeVectorD(0.0, 0.0, 0.0);
592  }else{
593  double xoff = 0;
594  double zoff = 0;
595  if( zdepth>0 ){
596  double detangle = getDetectorAngle(det)*M_PI/180.0;
597  if( det%2==0 ){ detangle *= -1.0; } //North side use negative angle
598  xoff = zdepth*sin(detangle);
599  zoff = zdepth*cos(detangle);
600  }
601  if(mDbAccess==0){ //no DB
602  if(det==0) return StThreeVectorD(-17.399+xoff, -5.26, 710.16+zoff);
603  if(det==1) return StThreeVectorD( 17.399+xoff, -5.26, 710.16+zoff);
604  if(det==2) return StThreeVectorD(-21.285+xoff, +1.80, 782.63+zoff);
605  if(det==3) return StThreeVectorD( 21.285+xoff, +1.80, 782.63+zoff);
606  return StThreeVectorD(0.0, 0.0, 0.0);
607  }else{ //from DB
608  if(det>=0 && det<4)
609  return StThreeVectorD(mFcsDetectorPosition.xoff[det]+xoff,
610  mFcsDetectorPosition.yoff[det],
611  mFcsDetectorPosition.zoff[det]+zoff);
612  return StThreeVectorD(0.0, 0.0, 0.0);
613  }
614  }
615 }
616 
617 float StFcsDb::getDetectorAngle(int det) const{
618  if(det==0) return 1.73;
619  if(det==1) return 1.73;
620  if(det==2) return 1.73;
621  if(det==3) return 1.73;
622  if(det==4) return 0.0;
623  if(det==5) return 0.0;
624  return 0.0;
625 }
626 
627 float StFcsDb::getXWidth(int det) const{
628  if(det==0) return 5.542+0.03;
629  if(det==1) return 5.542+0.03;
630  if(det==2) return 9.99+0.00;
631  if(det==3) return 9.99+0.00;
632  return 0.0;
633 }
634 
635 float StFcsDb::getYWidth(int det) const{
636  return getXWidth(det);
637 }
638 
639 float StFcsDb::getZDepth(int det) const{
640  if(det==0 || det==1) {return 30.97;} //66*(0.4+0.01+0.01)+(66-1)*0.05
641  if(det==2 || det==3) {return 84.24;} //36*2.34
642  else {return 1.0;}
643 }
644 
645 float StFcsDb::getShowerMaxZ(int det) const{
646  if(det==0 || det==1) return 15.0;
647  if(det==2 || det==3) return 25.0;
648  return 0.0;
649 }
650 
652 void StFcsDb::getLocalXYinCell(StFcsHit* hit, float &x, float &y) const{
653  getLocalXYinCell(hit->detectorId(),hit->id(),x,y);
654 }
655 
656 void StFcsDb::getLocalXYinCell(int det, int id, float &x, float &y) const{
657  getLocalXYinCell(det,getColumnNumber(det,id),getRowNumber(det,id),x,y);
658 }
659 
660 void StFcsDb::getLocalXYinCell(int det, int col, int row, float &x, float &y) const{
661  if(mLeakyHcal==1 && (det==kFcsHcalNorthDetId || det==kFcsHcalSouthDetId)){
662  if(col==1){
663  x=float(col)-0.4;
664  }else if(col==kFcsHcalNCol){
665  x=float(col)-0.6;
666  }else{
667  x=float(col);
668  }
669  }else{
670  x=float(col)-0.5;
671  }
672  y=float(row)-0.5;
673 }
674 
678 StThreeVectorD StFcsDb::getStarXYZ_4x4(int det,int col, int row) const{
679  int c1=0, c2=0, r1=0, r2=0;
680  if(det<=kFcsEcalSouthDetId){
681  c1=(col-1)*2 + 2;
682  c2=(col-1)*2 + 5;
683  r1=(row-1)*2 + 2;
684  r2=(row-1)*2 + 5;
685  }else if(det<=kFcsHcalSouthDetId){
686  c1=(col-1)*2 + 1;
687  c2=(col-1)*2 + 4;
688  r1=(row-1)*2 + 1;
689  r2=(row-1)*2 + 4;
690  }
691  return (getStarXYZ(det,c1,r1)+getStarXYZ(det,c2,r2))/2.0;
692 }
693 
695 double StFcsDb::getHcalProjectedToEcalX(int ns, double hcalLocalX, double zvtx){
696  if(zvtx==0.0){
697  StThreeVectorD ecalfront = getDetectorOffset(ns);
698  StThreeVectorD hcalfront = getDetectorOffset(ns+2);
699  double ecalD = sqrt(ecalfront.x()*ecalfront.x() + ecalfront.z()*ecalfront.z());
700  double hcalD = sqrt(hcalfront.x()*hcalfront.x() + ecalfront.z()*hcalfront.z());
701  double ecalSMD = ecalD + getShowerMaxZ(ns);
702  double hcalSMD = hcalD + getShowerMaxZ(ns+2);
703  double hcalLocalcm = hcalLocalX * getXWidth(ns+2); //convert to [cm]
704  return hcalLocalcm*ecalSMD/hcalSMD;
705  }else{
706  //to be impremented
707  LOG_INFO << "Need to be implememted!!!"<<endm;
708  return 1000;
709  }
710 };
711 
713 double StFcsDb::getHcalProjectedToEcalY(int ns, double hcalLocalY, double zvtx){
714  StThreeVectorD ecalfront = getDetectorOffset(ns);
715  StThreeVectorD hcalfront = getDetectorOffset(ns+2);
716  double ecalSMD = ecalfront.z() + getShowerMaxZ(ns) - zvtx;
717  double hcalSMD = hcalfront.z() + getShowerMaxZ(ns+2) - zvtx;
718  double hcalLocalcm = hcalLocalY * getYWidth(ns+2); //convert to [cm]
719  double hcalStar = double(nRow(ns+2))/2.0*getYWidth(ns+2)+hcalfront.y()-hcalLocalcm; //STAR Y
720  double hcalatEcal = hcalStar*ecalSMD/hcalSMD; //project assuming vtx_y=0
721  return double(nRow(ns))/2.0*getYWidth(ns) + ecalfront.y() - hcalatEcal; //put in Ecal local
722 };
723 
725 double StFcsDb::getProjectedDistance(StFcsCluster* ecal,StFcsCluster* hcal, double zvtx){
726  int eNS = northSouth(ecal->detectorId());
727  int hNS = northSouth(hcal->detectorId());
728  if(eNS!=hNS) return 1000;
729  double hX = getHcalProjectedToEcalX(hNS,hcal->x(),zvtx);
730  double hY = getHcalProjectedToEcalY(hNS,hcal->y(),zvtx);
731  double eX = ecal->x() * getXWidth(eNS);
732  double eY = ecal->y() * getYWidth(eNS);
733  double dX = eX-hX;
734  double dY = eY-hY;
735  return sqrt(dX*dX + dY*dY);
736 };
737 
739 double StFcsDb::getProjectedDistance(StFcsPoint* ecal, StFcsCluster* hcal, double zvtx){
740  int eNS = northSouth(ecal->detectorId());
741  int hNS = northSouth(hcal->detectorId());
742  if(eNS!=hNS) return 1000;
743  double hX = getHcalProjectedToEcalX(hNS,hcal->x(),zvtx);
744  double hY = getHcalProjectedToEcalY(hNS,hcal->y(),zvtx);
745  double eX = ecal->x() * getXWidth(eNS);
746  double eY = ecal->y() * getYWidth(eNS);
747  double dX = eX-hX;
748  double dY = eY-hY;
749  return sqrt(dX*dX + dY*dY);
750 };
751 
752 StThreeVectorD StFcsDb::getNormal(int det) const
753 {
754  double detangle = getDetectorAngle(det)*M_PI/180.0;
755  if( det%2==0 ){ detangle *= -1.0; } //North side use negative angle
756  return StThreeVectorD( sin(detangle), 0 ,cos(detangle) );
757  double planenormal[3] = {sin(detangle),0,cos(detangle)};
758 }
759 
760 StThreeVectorD StFcsDb::projectTrackToEcal(const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert) const
761 {
762  int det=0; // North side for negative px
763  // South side for positive px, since px==0 does not hit detector choose south side for that case
764  if( g2ttrk->p[2]>=0 && g2ttrk->p[0]>=0 ){ det=1; }
765  if( g2ttrk->p[2]<0 && g2ttrk->p[0]<0 ){ det=1; }
766  return projectTrack(det,g2ttrk,g2tvert,0);
767 }
768 
769 StThreeVectorD StFcsDb::projectTrackToHcal(const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert) const
770 {
771  int det = 2; // North side for negative px
772  // South side for positive px, since px==0 does not hit detector choose south side for that case
773  if( g2ttrk->p[2]>=0 && g2ttrk->p[0]>=0 ){ det=3; }
774  if( g2ttrk->p[2]<0 && g2ttrk->p[0]<0 ){ det=3; }
775  return projectTrack(det,g2ttrk,g2tvert,0);
776 }
777 
778 StThreeVectorD StFcsDb::projectTrackToEcalSMax(const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert) const
779 {
780  int det=0; // North side for negative px
781  // South side for positive px, since px==0 does not hit detector choose south side for that case
782  if( g2ttrk->p[2]>=0 && g2ttrk->p[0]>=0 ){ det=1; }
783  if( g2ttrk->p[2]<0 && g2ttrk->p[0]<0 ){ det=1; }
784  return projectTrack(det,g2ttrk,g2tvert,-1);
785 }
786 
787 StThreeVectorD StFcsDb::projectTrackToHcalSMax(const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert) const
788 {
789  int det = 2; // North side for negative px
790  // South side for positive px, since px==0 does not hit detector choose south side for that case
791  if( g2ttrk->p[2]>=0 && g2ttrk->p[0]>=0 ){ det=3; }
792  if( g2ttrk->p[2]<0 && g2ttrk->p[0]<0 ){ det=3; }
793  return projectTrack(det,g2ttrk,g2tvert,-1);
794 }
795 
796 StThreeVectorD StFcsDb::projectTrack(int det, const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert, double showermaxz) const
797 {
798  double linedir[3] = {g2ttrk->p[0],g2ttrk->p[1],g2ttrk->p[2]};
799  if( g2tvert!=0 ){
800  int vertind = g2ttrk->start_vertex_p - 1; //To correct for offset by one between fortran array and c array. 0 start index means it was generated at the starting vertex
801  double linestart[3] = {g2tvert[vertind].ge_x[0],g2tvert[vertind].ge_x[1],g2tvert[vertind].ge_x[2]};
802  if( vertind >= 0 ){//Since start index==0 means no start then vertind<0 will default to using origin
803  return projectLine(det, linedir, linestart, showermaxz);
804  }
805  }
806  double zero[3] = {0,0,0};
807  return projectLine(det,linedir,zero,showermaxz);
808 }
809 
810 StThreeVectorD StFcsDb::projectLine(int det, StThreeVectorD &linedirection, StThreeVectorD &lineorigin, double showermaxz) const
811 {
812  double linedir[3] = {linedirection.x(),linedirection.y(),linedirection.z()};
813  double linestart[3] = {lineorigin.x(),lineorigin.y(),lineorigin.z()};
814  return projectLine(det,linedir,linestart,showermaxz);
815 }
816 
817 StThreeVectorD StFcsDb::projectLine(int det, double* linedirection, double* lineorigin, double showermaxz) const
818 {
819  if( showermaxz<0 ){ showermaxz = getShowerMaxZ(det); } //when negative use default showermax
820  if( det%2==0 ){ //North side is negative x-axis
821  if( linedirection[2]>=0 && linedirection[0]>=0 ){ LOG_WARN << "Incorrect Det" << endm; }
822  if( linedirection[2]<0 && linedirection[0]<=0 ){ LOG_WARN << "Incorrect Det" << endm; }
823  }
824  else{ //South side is positive x-axis
825  if( linedirection[2]>=0 && linedirection[0]<0 ){ LOG_WARN << "Incorrect Det" << endm; }
826  if( linedirection[2]<0 && linedirection[0]>=0 ){ LOG_WARN << "Incorrect Det" << endm; } //(If x and z direction are both negative then also projects to south side which is positive x-axis
827  }
828  double detangle = getDetectorAngle(det)*M_PI/180.0;
829  if( det%2==0 ){ detangle *= -1.0; } //North side use negative angle
830  StThreeVectorD xyzoff = getDetectorOffset(det,showermaxz);
831  StThreeVectorD planenormal = getNormal(det);
832  //Solution of intersection of line and plane where line has direction {xdir,ydir,zdir}*t and starts at {xorigin,yorigin,zorigin} and a plane that has some normal with a point on the plane as the detector offset; "t" is the free parameter in the parametric equation of the line.
833  double tintersection =
834  (planenormal.x()*(xyzoff.x()-lineorigin[0])+planenormal.y()*(xyzoff.y()-lineorigin[1])+planenormal.z()*(xyzoff.z()-lineorigin[2])) /
835  (planenormal.x()*linedirection[0]+planenormal.y()*linedirection[1]+planenormal.z()*linedirection[2]);
836 
837  return StThreeVectorD( linedirection[0]*tintersection+lineorigin[0], linedirection[1]*tintersection+lineorigin[1], linedirection[2]*tintersection+lineorigin[2] );
838 }
839 
841 StThreeVectorD StFcsDb::getStarXYZ(const StFcsHit* hit, float FcsZ) const{
842  return getStarXYZ(hit->detectorId(),hit->id(),FcsZ);
843 }
844 
846 StThreeVectorD StFcsDb::getStarXYZ(const StFcsCluster* clu, float FcsZ) const{
847  return getStarXYZfromColumnRow(clu->detectorId(),clu->x(),clu->y(),FcsZ);
848 }
849 
851 StThreeVectorD StFcsDb::getStarXYZ(int det, int id, float FcsZ) const{
852  return getStarXYZ(det,getColumnNumber(det,id),getRowNumber(det,id),FcsZ);
853 }
854 
856 StThreeVectorD StFcsDb::getStarXYZ(int det, int col, int row, float FcsZ) const{
857  float x,y;
858  getLocalXYinCell(det,col,row,x,y);
859  return getStarXYZfromColumnRow(det,x,y,FcsZ);
860 }
861 
863 StThreeVectorD StFcsDb::getStarXYZfromColumnRow(int det, float col, float row, float FcsZ) const{
864  return getStarXYZ(det,col*getXWidth(det),row*getYWidth(det),FcsZ);
865 }
866 
868 StThreeVectorD StFcsDb::getStarXYZ(int det, float FcsX, float FcsY, float FcsZ, float zVertex) const{
869  if(FcsZ<0.0) FcsZ = getShowerMaxZ(det);
870  double x = 0.0, y=0.0, z=0.0;
872  double a = getDetectorAngle(det) / 180.0 * M_PI;
873  y = off.y() + (double(nRow(det)) / 2.0 * getYWidth(det)) - FcsY;
874  if(northSouth(det) == 0) {
875  x = off.x() - FcsX * cos(a) - FcsZ * sin(a);
876  z = off.z() + FcsZ * cos(a) - FcsX * sin(a);
877  }else{ // south side
878  x = off.x() + FcsX * cos(a) + FcsZ * sin(a);
879  z = off.z() + FcsZ * cos(a) - FcsX * sin(a);
880  }
881  z -= zVertex;
882  if(mDebug>1) LOG_DEBUG << Form("getStarXYZ XOFF=%f YOFF=%f ZOFF=%f Angle=%f : x=%f y=%f z=%f",
883  off.x(),off.y(),off.z(),a,x,y,z) << endm;
884  return StThreeVectorD(x,y,z);
885 }
886 
887 float StFcsDb::getPhi(int det,float FcsX, float FcsY, float FcsZ) const{
888  return (getStarXYZ(det,FcsX,FcsY,FcsZ)).phi();
889 }
890 
891 float StFcsDb::getEta(int det,float FcsX, float FcsY, float FcsZ, float zVertex) const{
892  return (getStarXYZ(det,FcsX,FcsY,FcsZ,zVertex)).pseudoRapidity();
893 }
894 
895 StLorentzVectorD StFcsDb::getLorentzVector(const StThreeVectorD& xyz, float energy, float zVertex){
896  // Calculate a 4 momentum from a direction/momentum vector and energy assuming zero mass i.e. E=p
897  // Taking into account beamline offsets and angles from DB
898  StThreeVectorD xyznew(xyz.x()-mVx,xyz.y()-mVy,xyz.z()-zVertex);
899  xyznew.rotateX(+mThetaY);
900  xyznew.rotateY(-mThetaX);
901  double e=energy;
902  StThreeVectorD mom3 = xyznew.unit() * e;
903  if(mDebug>1){
904  LOG_DEBUG << Form("mVx=%8.4f mVdxdz=%8.4f mThetaX=%8.4f",mVx,mVdxdz,mThetaX) << endm;
905  LOG_DEBUG << Form("mVy=%8.4f mVdydz=%8.4f mThetaY=%8.4f",mVy,mVdydz,mThetaY) << endm;
906  LOG_DEBUG << Form("xyz = %lf %lf %lf",xyz.x(), xyz.y(), xyz.z()) << endm;
907  LOG_DEBUG << Form("xyz rot = %lf %lf %lf",xyznew.x(), xyznew.y(), xyznew.z()) << endm;
908  LOG_DEBUG << Form("p = %lf %lf %lf %lf",mom3.x(), mom3.y(), mom3.z(),e) << endm;
909  }
910  return StLorentzVectorD(mom3, e);
911 }
912 
913 float StFcsDb::getSamplingFraction(int det) const{
914  if(det==0 || det==1) return 0.2;
915  if(det==2 || det==3) return 0.0145;
916  if(det==4 || det==5) return 2.0; //2MeV for MIP
917  return 0.0;
918 }
919 
920 int StFcsDb::getZeroSuppression(int det) const {
921  return 1;
922 }
923 
925  return getGain(hit->detectorId(), hit->id());
926 }
927 
928 float StFcsDb::getGain(int det, int id) const {
929  if(det>=0 && det<kFcsNDet && id>=0 && id<maxId(det)) {
930  switch(mGainMode){
931  case GAINMODE::FIXED :
932  if(det<=kFcsHcalSouthDetId) return 0.0053; //default 5.3MeV/ch
933  return 1.0/100.0; //100ch for MIP for Pres
934  case GAINMODE::FORCED :
935  if(det<=kFcsEcalSouthDetId) return mForceUniformGainEcal;
936  if(det<=kFcsHcalSouthDetId) return mForceUniformGainHcal;
937  if(det<=kFcsPresSouthDetId) return mForceUniformGainPres;
938  case GAINMODE::DB :
939  default:
940  int ehp,ns,dep,ch,crt,slt;
941  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
942  return mGain[ehp][ns][dep][ch];
943  }
944  }
945  return 1.0;
946 }
947 
949  return getGainCorrection(hit->detectorId(), hit->id());
950 }
951 
952 float StFcsDb::getGainCorrection(int det, int id) const {
953  if(det>=0 && det<kFcsNDet && id>=0 && id<maxId(det)) {
954  switch(mGainCorrMode){
955  case GAINMODE::FIXED :
956  if(det<=kFcsHcalSouthDetId) return 1.0; // default 1.0
957  return 0.5; // 1/2 MIP for Pres
958  case GAINMODE::FORCED :
959  if(det<=kFcsEcalSouthDetId) return mForceUniformGainCorrectionEcal;
960  if(det<=kFcsHcalSouthDetId) return mForceUniformGainCorrectionHcal;
961  if(det<=kFcsPresSouthDetId) return mForceUniformGainCorrectionPres;
962  case GAINMODE::DB :
963  default:
964  int ehp,ns,dep,ch,crt,slt;
965  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
966  return mGainCorr[ehp][ns][dep][ch];
967  }
968  }
969  return 1.0;
970 }
971 
973  return getGainCorrection(hit->detectorId(), hit->id());
974 }
975 
976 float StFcsDb::getPresValley(int det, int id) const {
977  return getGainCorrection(det,id);
978 }
979 
981  return getGainOnline(hit->detectorId(), hit->id());
982 }
983 
984 float StFcsDb::getGainOnline(int det, int id) const {
985  switch(mGainOnlineMode){
986  case GAINMODE::FORCED :
987  if(det<=kFcsEcalSouthDetId) return mForceUniformGainOnlineEcal;
988  if(det<=kFcsHcalSouthDetId) return mForceUniformGainOnlineHcal;
989  if(det<=kFcsPresSouthDetId) return mForceUniformGainOnlinePres;
990  case GAINMODE::DB :
991  default:
992  if(det>=0 && det<kFcsNDet && id>=0 && id<maxId(det)) {
993  int ehp,ns,dep,ch,crt,slt;
994  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
995  return mGainOnline[ehp][ns][dep][ch];
996  }
997  }
998  return 1.0;
999 }
1000 
1002  return getGainCorrection(hit->detectorId(), hit->id());
1003 }
1004 
1005 float StFcsDb::getPresThreshold(int det, int id) const {
1006  return getGainCorrection(det,id);
1007 }
1008 
1009 
1011  return getGain8(hit->detectorId(), hit->id());
1012 }
1013 
1014 float StFcsDb::getGain8(int det, int id) const {
1015  return getGain(det,id)/1.21;
1016 }
1017 
1018 void StFcsDb::getDepfromId(int detectorId, int id, int &ehp, int &ns, int &crt, int &slt, int &dep, int &ch) const{
1019  ehp=-1; ns=-1; crt=-1; slt=-1; dep=-1; ch=-1;
1020  if(detectorId<0 || detectorId>=kFcsNDet) return;
1021  if(id<0 || id>=kFcsMaxId) return;
1022  ehp = mMap_ehp[detectorId][id];
1023  ns = mMap_ns [detectorId][id];
1024  crt = mMap_crt[detectorId][id];
1025  slt = mMap_slt[detectorId][id];
1026  dep = mMap_dep[detectorId][id];
1027  ch = mMap_ch [detectorId][id];
1028  return;
1029 }
1030 
1031 void StFcsDb::getIdfromDep(int ehp, int ns, int dep, int ch, int &detectorId, int &id, int &crt, int &slt) const{
1032  detectorId=6; id=4095; crt=0; slt=0;
1033  if(ehp<0 || ehp>=kFcsEHP) return;
1034  if(ns<0 || ns>=kFcsNorthSouth) return;
1035  if(dep<0 || dep>=kFcsMaxDepBd) return;
1036  if(ch<0 || ch>=kFcsMaxDepCh) return;
1037  detectorId = mRMap_det[ehp][ns][dep][ch];
1038  id = mRMap_id [ehp][ns][dep][ch];
1039  crt = mRMap_crt[ehp][ns][dep][ch];
1040  slt = mRMap_slt[ehp][ns][dep][ch];
1041  return;
1042 }
1043 
1044 int StFcsDb::getNDep(int ehp, int ns) const{
1045  if(mRun19>0){
1046  if(ns==0) return 0;
1047  switch(ehp){
1048  case 0: return 2;
1049  case 1: return 1;
1050  case 2: return 1;
1051  case 3: return 1;
1052  default: return 0;
1053  }
1054  }else{
1055  switch(ehp){
1056  case 0: return 24;
1057  case 1: return 9;
1058  case 2: return 6;
1059  case 3: return 3;
1060  default: return 0;
1061  }
1062  }
1063 }
1064 
1065 void StFcsDb::getSCmap(int det, int id,
1066  int &ehp, int &ns, int &scdep, int &branch, int &fee_i2c, int &sipm,
1067  int &pp, int &jacket) const{
1068  ehp=-1; ns=-1; scdep=-1; branch=-1; fee_i2c=-1; sipm=-1, pp=-1; jacket=-1;
1069  if(det<0 || det>=kFcsNDet) return;
1070  if(id<0 || id>=kFcsMaxId) return;
1071  ehp=mScMap_ehp[det][id];
1072  ns=mScMap_ns[det][id];
1073  scdep=mScMap_dep[det][id];
1074  branch=mScMap_bra[det][id];
1075  fee_i2c= mScMap_add[det][id];
1076  sipm=mScMap_sipm[det][id];
1077  pp=mScMap_pp[det][id];
1078  jacket=mScMap_j[det][id];
1079  return;
1080 }
1081 
1082 void StFcsDb::getIdfromSCmap(int ehp, int ns, int scdep, int branch, int fee_i2c, int sipm,
1083  int &det, int &id)const{
1084  det=-1;
1085  id=-1;
1086  if(ehp<0 || det>=kFcsEHP) return;
1087  if(ns<0 || ns>=kFcsNorthSouth) return;
1088  if(scdep<0 || scdep>=kFcsMaxDepBd) return;
1089  if(branch<0 || branch>=kFcsMaxBranch) return;
1090  if(fee_i2c<0 || fee_i2c>=kFcsMaxAddr) return;
1091  if(sipm<0 || sipm>=kFcsMaxSiPM) return;
1092  det=mRScMap_det[ehp][ns][scdep][branch][fee_i2c][sipm];
1093  id =mRScMap_id [ehp][ns][scdep][branch][fee_i2c][sipm];
1094  return;
1095 }
1096 
1097 int StFcsDb::jacketColor(int ehp, int ns, int dep, int ch){
1098  switch(ehp){
1099  case 0:
1100  if(dep<=19) return dep%5;
1101  switch(dep){
1102  case 20: case 21:
1103  if(ch<4) return 7;
1104  return ch/4;
1105  case 22: return 6;
1106  case 23: return 5;
1107  default: return -1;
1108  }
1109  case 1:
1110  switch(dep){
1111  case 0: case 3: return 0 + ns*4;
1112  case 1: case 4: return 1 + ns*4;
1113  case 2: case 5: return 2 + ns*4;
1114  case 6: case 7:
1115  if(ch<8 ) return 0 + ns*4;
1116  if(ch<16) return 1 + ns*4;
1117  if(ch<24) return 2 + ns*4;
1118  return -1;
1119  case 8: return 3 + ns*4;
1120  default: return -1;
1121  }
1122  default:
1123  return -1;
1124  }
1125 }
1126 
1127 
1128 
1130  for(int b=1; b<=8; b++){
1131  for(int p=2; p<=7; p++){
1132  short dep = EPPMap[b-1][p-2][0];
1133  short lh = EPPMap[b-1][p-2][1];
1134  short scr = EPPMap[b-1][p-2][2];
1135  if(dep>=0){
1136  EMapPPB[dep][lh]=b;
1137  EMapPPP[dep][lh]=p;
1138  if(scr>0){
1139  EMapSCDEP[scr-1]=dep;
1140  EMapSCBRA[scr-1]=lh;
1141  EMapSCPP[scr-1]=b;
1142  EMapSCJ[scr-1]=p/2;
1143  }
1144  }
1145  }
1146  }
1147  for(int b=1; b<=4; b++){
1148  for(int p=2; p<=7; p++){
1149  short dep = HPPMap[b-1][p-2][0];
1150  short lh = HPPMap[b-1][p-2][1];
1151  short scr = HPPMap[b-1][p-2][2];
1152  if(dep>=0){
1153  HMapPPB[dep][lh]=b;
1154  HMapPPP[dep][lh]=p;
1155  if(scr>0){
1156  HMapSCDEP[scr-1]=dep;
1157  HMapSCBRA[scr-1]=lh;
1158  HMapSCPP[scr-1]=b;
1159  HMapSCJ[scr-1]=p/2;
1160  }
1161  }
1162  }
1163  }
1164 }
1165 
1167  makePPMap();
1168  int ehp,crt,slt,dep,ch,det,id;
1169  for(int det=0; det<kFcsNDet; det++){
1170  for(int id=0; id<kFcsMaxId; id++){
1171  mMap_ehp[det][id]=-1;
1172  mMap_ch[det][id]=-1;
1173  mMap_ppb[det][id]=-1;
1174  mMap_ppp[det][id]=-1;
1175  mMap_pph[det][id]=-1;
1176  mMap_wcol[det][id]=-1;
1177  mMap_jcol[det][id]=-1;
1178  mScMap_ehp[det][id]=-1;
1179  mScMap_pp[det][id]=-1;
1180  mScMap_j[det][id]=-1;
1181  }
1182  }
1183  for(int ehp=0; ehp<kFcsEHP; ehp++){
1184  for(int ns=0; ns<kFcsNorthSouth; ns++){
1185  for(int dep=0; dep<kFcsMaxDepBd; dep++){
1186  for(int ch=0; ch<kFcsMaxDepCh; ch++){
1187  mRMap_det[ehp][ns][dep][ch]=6;
1188  mRMap_id[ehp][ns][dep][ch]=-1;
1189  }
1190  for(int bra=0; bra<kFcsMaxBranch; bra++){
1191  for(int add=0; add<kFcsMaxAddr; add++){
1192  for(int sipm=0; sipm<kFcsMaxSiPM; sipm++){
1193  mRScMap_det[ehp][ns][dep][bra][add][sipm]=-1;
1194  mRScMap_id [ehp][ns][dep][bra][add][sipm]=-1;
1195  }
1196  }
1197  }
1198  }
1199  }
1200  }
1201 
1202  //Ecal
1203  ehp=0;
1204  for(int ns=0; ns<2; ns++){
1205  id=0;
1206  det = ns;
1207  for(int row=1; row<=kFcsEcalNRow; row++){
1208  for(int col=1; col<=kFcsEcalNCol; col++){
1209  if (row== 1){crt=1+ns*2; slt=16; dep=20; ch=col-1;} //top row (ch0-21)
1210  else if(row==34){crt=1+ns*2; slt=17; dep=21; ch=col-1;} //bottom row (ch0-21)
1211  else if(col== 1){crt=1+ns*2; slt=18; dep=22; ch=row-2;} //near side column (ch0-31)
1212  else if(col==22){crt=1+ns*2; slt=19; dep=23; ch=row-2;} //far side column (ch0-31)
1213  else{
1214  crt=ns*4;
1215  dep=(col-2)/4 + (row-2)/8*5;
1216  slt=dep;
1217  ch =(col-2)%4 + ((row-2)%8)*4;
1218  }
1219  mMap_ehp[det][id] = ehp;
1220  mMap_ns [det][id] = ns;
1221  mMap_crt[det][id] = crt;
1222  mMap_slt[det][id] = slt;
1223  mMap_dep[det][id] = dep;
1224  mMap_ch [det][id] =ch ;
1225  mRMap_det[ehp][ns][dep][ch] = det;
1226  mRMap_id [ehp][ns][dep][ch] = id ;
1227  mRMap_crt[ehp][ns][dep][ch] = crt;
1228  mRMap_slt[ehp][ns][dep][ch] = slt;
1229 
1230  //cable
1231  int lh= ch/16;
1232  int b = EMapPPB[dep][lh];
1233  int p = EMapPPP[dep][lh];
1234  int h = (ch%16)/4 + (p-2)*4 + 1;
1235  int w = ch%4;
1236  int j = jacketColor(ehp,ns,dep,ch);
1237  mMap_ppb[det][id] = b;
1238  mMap_ppp[det][id] = p;
1239  mMap_pph[det][id] = h;
1240  mMap_wcol[det][id] = w;
1241  mMap_jcol[det][id] = j;
1242 
1243  //SC map
1244  int scr=(row-1)/2;
1245  unsigned short scdep=EMapSCDEP[scr];
1246  unsigned short bra=EMapSCBRA[scr];
1247  unsigned short pp=EMapSCPP[scr];
1248  unsigned short pj=EMapSCJ[scr];;
1249  unsigned short add=(col-1)/2;
1250  unsigned short sipm;
1251  if(ns==0){
1252  sipm = (col-1)%2 + ((row-1)%2)*2;
1253  }else{
1254  sipm = col%2 + ((row-1)%2)*2;
1255  }
1256  mScMap_ehp[det][id] = ehp;
1257  mScMap_ns[det][id] = ns;
1258  mScMap_dep[det][id] = scdep;
1259  mScMap_bra[det][id] = bra;
1260  mScMap_add[det][id] = add;
1261  mScMap_sipm[det][id] = sipm;
1262  mScMap_pp[det][id] = pp;
1263  mScMap_j[det][id] = pj;
1264  mRScMap_det[ehp][ns][scdep][bra][add][sipm]=det;
1265  mRScMap_id[ehp][ns][scdep][bra][add][sipm]=id;
1266 
1267  id++;
1268  }
1269  }
1270  }
1271 
1272  //Hcal
1273  ehp=1;
1274  for(int ns=0; ns<2; ns++){
1275  id=0;
1276  det = ns + 2;
1277  crt = 1+ns*2;
1278  for(int row=1; row<=kFcsHcalNRow; row++){
1279  for(int col=1; col<=kFcsHcalNCol; col++){
1280  if (col==13){dep=8; ch=row-1;} //far side column (ch0-19)
1281  else if(row== 1){dep=6; ch=(col-1)%4 + ((col-1)/4)*8 ;} //top row (ch0,1,2,3, 8, 9...19)
1282  else if(row== 2){dep=6; ch=(col-1)%4 + ((col-1)/4)*8 + 4;} //2nd row (ch4,5,6,7,12,13...23)
1283  else if(row==19){dep=7; ch=(col-1)%4 + ((col-1)/4)*8 ;} //2nd bottom row
1284  else if(row==20){dep=7; ch=(col-1)%4 + ((col-1)/4)*8 + 4;} //bottom row
1285  else{
1286  dep= (col-1)/4 + ((row-3)/8)*3;
1287  ch = (col-1)%4 + ((row-3)%8)*4;
1288  }
1289  slt=dep;
1290  mMap_ehp[det][id] = ehp;
1291  mMap_ns [det][id] = ns;
1292  mMap_crt[det][id] = crt;
1293  mMap_slt[det][id] = slt;
1294  mMap_dep[det][id] = dep;
1295  mMap_ch[det][id] =ch ;
1296  mRMap_det[ehp][ns][dep][ch] = det;
1297  mRMap_id [ehp][ns][dep][ch] = id ;
1298  mRMap_crt[ehp][ns][dep][ch] = crt;
1299  mRMap_slt[ehp][ns][dep][ch] = slt;
1300 
1301  //cable
1302  int lh= ch/16;
1303  int b = HMapPPB[dep][lh];
1304  int p = HMapPPP[dep][lh];
1305  int h = (ch%16)/4 + (p-2)*4 + 1;
1306  int w = ch%4;
1307  int j = jacketColor(ehp,ns,dep,ch);
1308  mMap_ppb[det][id] = b;
1309  mMap_ppp[det][id] = p;
1310  mMap_pph[det][id] = h;
1311  mMap_wcol[det][id] = w;
1312  mMap_jcol[det][id] = j;
1313 
1314  //SC map
1315  unsigned short feerow = (row-1)/2;
1316  unsigned short scdep=HMapSCDEP[feerow];
1317  unsigned short bra=HMapSCBRA[feerow];
1318  unsigned short pp=HMapSCPP[feerow];
1319  unsigned short pj=HMapSCJ[feerow];
1320  unsigned short add=col-1;
1321  unsigned short sipm;
1322  if(ns==0){
1323  sipm = (row-1)%2;
1324  }else{
1325  sipm = row%2;
1326  }
1327  mScMap_ehp[det][id] = ehp;
1328  mScMap_ns[det][id] = ns;
1329  mScMap_dep[det][id] = scdep;
1330  mScMap_bra[det][id] = bra;
1331  mScMap_add[det][id] = add;
1332  mScMap_pp[det][id] = pp;
1333  mScMap_j[det][id] = pj;
1334  mScMap_sipm[det][id] = sipm;
1335  mRScMap_det[ehp][ns][scdep][bra][add][sipm]=det;
1336  mRScMap_id[ehp][ns][scdep][bra][add][sipm]=id;
1337  id++;
1338  }
1339  }
1340  }
1341 
1342  //EPD west as PRES
1343  // EPD PP= 1 ~ 6 ==> ns=0 (north) and dep=0 (near top) to 5 (near bottom)
1344  // EPD PP= 12 ~ 7 ==> ns=1 (south) and dep=0 (near top) to 5 (near bottom), odd/even reversed
1345  ehp=2;
1346  for(int ns=0; ns<2; ns++){
1347  det = ns + 4;
1348  crt = 1+ns*2;
1349  for(int dep=0; dep<6; dep++){
1350  for(int ch=0; ch<32; ch++){
1351  slt=dep+10;
1352  id=dep*32+ch;
1353  mMap_ehp[det][id] = ehp;
1354  mMap_ns [det][id] = ns;
1355  mMap_crt[det][id] = crt;
1356  mMap_slt[det][id] = slt;
1357  mMap_dep[det][id] = dep;
1358  mMap_ch[det][id] = ch ;
1359  mRMap_det[ehp][ns][dep][ch] = det;
1360  mRMap_id [ehp][ns][dep][ch] = id ;
1361  mRMap_crt[ehp][ns][dep][ch] = crt;
1362  mRMap_slt[ehp][ns][dep][ch] = slt;
1363  }
1364  }
1365  }
1366 }
1367 
1369  int ehp,crt,slt,dep,ch,det,id,ns;
1370  crt=1;
1371  slt=0;
1372  ns=1;
1373 
1374  for(int det=0; det<kFcsNDet; det++){
1375  for(int id=0; id<kFcsMaxId; id++){
1376  mMap_ehp[det][id]=-1;
1377  mMap_ns[det][id]=-1;
1378  mMap_crt[det][id]=-1;
1379  mMap_slt[det][id]=-1;
1380  mMap_dep[det][id]=-1;
1381  mMap_ch[det][id]=-1;
1382  }
1383  }
1384  for(int ehp=0; ehp<kFcsEHP; ehp++){
1385  for(int ns=0; ns<kFcsNorthSouth; ns++){
1386  for(int dep=0; dep<kFcsMaxDepBd; dep++){
1387  for(int ch=0; ch<kFcsMaxDepCh; ch++){
1388  mRMap_det[ehp][ns][dep][ch]=6;
1389  mRMap_id[ehp][ns][dep][ch]=-1;
1390  mRMap_crt[ehp][ns][dep][ch]=-1;
1391  mRMap_slt[ehp][ns][dep][ch]=-1;
1392  }
1393  }
1394  }
1395  }
1396 
1397  //Ecal
1398  ehp=0;
1399  id=0;
1400  det = ns;
1401  dep = 0;
1402  int c[2] = {0,0};
1403  for(int row=1; row<=8; row++){
1404  for(int col=1; col<=8; col++){
1405  if(col<=4) {dep=0;}
1406  else {dep=1;}
1407  mMap_ehp[det][id] = ehp;
1408  mMap_ns [det][id] = ns;
1409  mMap_crt[det][id] = crt;
1410  mMap_slt[det][id] = slt;
1411  mMap_dep[det][id] = dep;
1412  mMap_ch [det][id] = c[dep] ;
1413 
1414  mRMap_det[ehp][ns][dep][c[dep]] = det;
1415  mRMap_id [ehp][ns][dep][c[dep]] = id ;
1416  mRMap_crt[ehp][ns][dep][c[dep]] = crt;
1417  mRMap_slt[ehp][ns][dep][c[dep]] = slt;
1418  id++;
1419  c[dep]++;
1420  }
1421  }
1422 
1423  //Hcal
1424  ehp=1;
1425  id=0;
1426  det = ns + 2;
1427  dep = 0; //number for 2019/5/15 with OLD DEP16 before we install new DEP32
1428  ch = 0;
1429  for(int row=1; row<=4; row++){
1430  for(int col=1; col<=4; col++){
1431  mMap_ehp[det][id] = ehp;
1432  mMap_ns [det][id] = ns;
1433  mMap_crt[det][id] = crt;
1434  mMap_slt[det][id] = slt;
1435  mMap_dep[det][id] = dep;
1436  mMap_ch[det][id] =ch ;
1437  mRMap_det[ehp][ns][dep][ch] = det;
1438  mRMap_id [ehp][ns][dep][ch] = id ;
1439  mRMap_crt[ehp][ns][dep][ch] = crt;
1440  mRMap_slt[ehp][ns][dep][ch] = slt;
1441  id++;
1442  ch++;
1443  }
1444  }
1445 
1446  //Pres
1447  ehp=2;
1448  id=0;
1449  det = ns + 4;
1450  dep = 0;
1451  ch = 8;
1452  for(int id=0; id<9; id++){
1453  mMap_ehp[det][id] = ehp;
1454  mMap_ns [det][id] = ns;
1455  mMap_crt[det][id] = crt;
1456  mMap_slt[det][id] = slt;
1457  mMap_dep[det][id] = dep;
1458  mMap_ch[det][id] = ch ;
1459  mRMap_det[ehp][ns][dep][ch] = det;
1460  mRMap_id [ehp][ns][dep][ch] = id ;
1461  mRMap_crt[ehp][ns][dep][ch] = crt;
1462  mRMap_slt[ehp][ns][dep][ch] = slt;
1463  ch--;
1464  }
1465 }
1466 
1467 void StFcsDb::getIdfromEPD(int pp, int tt, int& det, int &id){
1468  det=-1;
1469  id=-1;
1470  int row=0,col=0;
1471  if(tt<0 || tt>=32) return;
1472  if(pp>=1 && pp<=6){ //north side
1473  det=4;
1474  row=(pp-1)*2 + (tt+1)%2 + 1;
1475  col=tt/2;
1476  }else if(pp>=7 && pp<=12){ //south side
1477  det=5;
1478  row=(12-pp)*2 + (tt)%2 + 1;
1479  col=tt/2;
1480  }
1481  id=(row-1)*16 + col;
1482 }
1483 
1484 void StFcsDb::getEPDfromId(int det, int id, int &pp, int &tt){
1485  int row=id/16 + 1;
1486  int col=id%16 + 1;
1487  if(det==4){
1488  pp = (row-1)/2 + 1;
1489  tt = (col-1)*2 - (row-1)%2 + 1;
1490  }else if(det==5){
1491  pp = 12 - (row-1)/2;
1492  tt = (col-1)*2 + (row-1)%2;
1493  }
1494 }
1495 
1496 void StFcsDb::printHeader(FILE* f, int flag=0, int csv=0){
1497  fprintf(f,"### Detector\n");
1498  fprintf(f,"#det : 0/1=ECal-N/S 2/3=Hcal-N/S 4/5=PRS-N/S\n");
1499  fprintf(f,"#id : 0~747 for Ecal 0~259 for Hcal\n");
1500  fprintf(f,"#row : 1~34 for Ecal 1~20 for Hcal\n");
1501  fprintf(f,"#col : 1~22 for Ecal 1~13 for Hcal\n");
1502  fprintf(f,"### Readout\n");
1503  fprintf(f,"#ehp : 0=ECal 1=Hcal 2=PRS\n");
1504  fprintf(f,"#ns : 0=north 1=south\n");
1505  fprintf(f,"#crt : 0=EcalNorth 1=MixNorth 2=Main 3=MixSouth 4=EcalSouth\n");
1506  fprintf(f,"#slt : 0~19\n");
1507  fprintf(f,"#dep : 0~24 for Ecal 0~7 for Hcal 0~3 for Pres\n");
1508  fprintf(f,"#ch : 0~31 for all DEP\n");
1509  fprintf(f,"### Patchpanel and cable\n");
1510  fprintf(f,"#FRow: FEEBd Row = (row-1)/2 [0-16 for Ecal 0-9 for Hcal]\n");
1511  fprintf(f,"#FCol: FEEBd Col = FEEBd Addr [0-10 for Ecal 0-12 for Hcal]\n");
1512  fprintf(f,"#SiPM: FEEBd Ch# [0-3 for Ecal 0-1 for Hcal]\n");
1513  fprintf(f,"#ppb : PatchPanel Board# [1~8 for Ecal 1-4 for Hcal]\n");
1514  fprintf(f,"#ppp : PatchPanel MDR cable P# [2-7]\n");
1515  fprintf(f,"#pph : PatchPanel Header# [1-24]\n");
1516  fprintf(f,"#jcol: Cable Jacket color\n");
1517  fprintf(f,"#wcol: Cable Wire color\n");
1518  fprintf(f,"#length: Cable length\n");
1519  if(csv==0){
1520  if(flag==0){
1521  fprintf(f,"#det id row col ehp ns crt slt dep ch Frow Fcol SiPM ppb ppp pph jcol wcol length\n");
1522  }else{
1523  fprintf(f,"#ehp ns dep ch crt slt det id row col Frow Fcol SiPM ppb ppp pph jcol wcol length\n");
1524  }
1525  }else{
1526  if(flag==0){
1527  fprintf(f,"#det,id,row,col,ehp,ns,crt,slt,dep,ch,Frow,Fcol,SiPM,ppb,ppp,pph,jcol,wcol,length\n");
1528  }else{
1529  fprintf(f,"#ehp,ns,dep,ch,crt,slt,det,id,row,col,Frow,Fcol,SiPM,ppb,ppp,pph,jcol,wcol,length\n");
1530  }
1531  }
1532 }
1533 
1535  fprintf(f,"# Css-DDch\n");
1536  fprintf(f,"# C : crate 0~4 for EcalN,MixN,Main,MixS,EcalS\n");
1537  fprintf(f,"# ss : slot (0~19)\n");
1538  fprintf(f,"# DD : DEP bd# (0~24 for Ecal, 0~7 for Hcal\n");
1539  fprintf(f,"# ch : DEP ch# (0~31 for all DEP)\n");
1540 }
1541 
1543  fprintf(f,"# crt-slt : ns ehp dep : DNiii-RR/CC DNiii-RR/CC DNiii-RR/CC ...\n");
1544  fprintf(f,"# D : \"E\"cal, \"H\"cal, \"P\"res\n");
1545  fprintf(f,"# N : North or South\n");
1546  fprintf(f,"# iii : id\n");
1547  fprintf(f,"# RR : Row\n");
1548  fprintf(f,"# CC : Column\n");
1549 }
1550 
1551 void StFcsDb::printHeader4(FILE* f, int flag=0){
1552  fprintf(f,"### Detector\n");
1553  fprintf(f,"#det : 0/1=ECal N/S, 2/3=Hcal N/S , 4/5=PRS N/S\n");
1554  fprintf(f,"#id : 0~747 for Ecal, 0~259 for Hcal\n");
1555  fprintf(f,"#row : 1~34 for Ecal, 1~20 for Hcal\n");
1556  fprintf(f,"#col : 1~22 for Ecal, 1~13 for Hcal\n");
1557  fprintf(f,"### Slow Control\n");
1558  fprintf(f,"#ehp : 0=ECal, 1=Hcal, 2=PRS\n");
1559  fprintf(f,"#ns : 0=north, 1=south\n");
1560  fprintf(f,"#dep : 0~24 for Ecal, 0~7 for Hcal, 0~3 for Pres\n");
1561  fprintf(f,"#branch : 0~1\n");
1562  fprintf(f,"#switch addr: 0~15\n");
1563  fprintf(f,"#SiPM# : 0~3 for Ecal, 0~1 for Hcal&Pres\n");
1564  fprintf(f,"### Patchpanel and cable\n");
1565  fprintf(f,"#ppb : PatchPanel Board# (1~8 for ecal, 1-4 for hcal)\n");
1566  fprintf(f,"#J : PatchPanel SC conection (J1~J3)\n");
1567  if(flag==0){
1568  fprintf(f,"#det id row col ehp ns dep bra add SiPM ppb J\n");
1569  }else{
1570  fprintf(f,"#ehp ns dep bra add SiPM det id row col\n");
1571  }
1572 }
1573 
1575  int ehp,ns,crt,slt,dep,ch,det,id,row,col=0;
1576 
1577  FILE *f1 = fopen("fcsMap.txt","w"); printHeader(f1);
1578  FILE *f1c = fopen("fcsMap.csv","w"); printHeader(f1c,0,1);
1579  FILE *f1e = fopen("fcs_ecal_readout_map.csv","w"); printHeader(f1e);
1580  FILE *f1h = fopen("fcs_hcal_readout_map.csv","w"); printHeader(f1h);
1581  FILE *f1p = fopen("fcs_pres_readout_map.csv","w"); printHeader(f1p);
1582  FILE *f2 = fopen("fcsMap2.txt","w"); printHeader2(f2);
1583  FILE *f3 = fopen("fcsDepMap.txt","w"); printHeader(f3,1);
1584  FILE *f3c= fopen("fcsDepMap.csv","w"); printHeader(f3c,1,1);
1585  FILE *f4 = fopen("fcsDepMap2.txt","w"); printHeader3(f4);
1586  FILE *f5 = fopen("fcsScMap.txt","w"); printHeader4(f5);
1587  FILE *f5e = fopen("fcs_ecal_sc_map.csv","w"); printHeader4(f5e);
1588  FILE *f5h = fopen("fcs_hcal_sc_map.csv","w"); printHeader4(f5h);
1589  FILE *f5p = fopen("fcs_pres_sc_map.csv","w"); printHeader4(f5p);
1590  FILE *f6 = fopen("fcsScRevMap.txt","w"); printHeader4(f6,1);
1591 
1592  FILE *f7 = fopen("fcsEpdMap.txt","w");
1593  FILE *fpp = fopen("fcsPPMap.txt","w");
1594 
1595  //Ecal
1596  for(ns=0; ns<2; ns++){
1597  det=ns;
1598  id=0;
1599  fprintf(f2,"Ecal NS=%1d\n",ns);
1600  for(row=1; row<=nRow(det); row++){
1601  for(col=1; col<=nColumn(det); col++){
1602  if(mMap_ehp[det][id]>=0){
1603  fprintf(f1,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %2d %2d %2d %2d P%1d H%02d %6s %6s %3.1f\n",
1604  det,id,row,col,
1605  mMap_ehp[det][id],
1606  mMap_ns [det][id],
1607  mMap_crt[det][id],
1608  mMap_slt[det][id],
1609  mMap_dep[det][id],
1610  mMap_ch [det][id],
1611  (row-1)/2,
1612  mScMap_add[det][id],
1613  mScMap_sipm[det][id],
1614  mMap_ppb[det][id],
1615  mMap_ppp[det][id],
1616  mMap_pph[det][id],
1617  colJ[mMap_jcol[det][id]],
1618  colW[mMap_wcol[det][id]],
1619  leng[mMap_jcol[det][id]]);
1620  fprintf(f1c,"%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%2d,%2d,%2d,%2d,P%1d,H%02d,%6s,%6s,%3.1f\n",
1621  det,id,row,col,
1622  mMap_ehp[det][id],
1623  mMap_ns [det][id],
1624  mMap_crt[det][id],
1625  mMap_slt[det][id],
1626  mMap_dep[det][id],
1627  mMap_ch [det][id],
1628  (row-1)/2,
1629  mScMap_add[det][id],
1630  mScMap_sipm[det][id],
1631  mMap_ppb[det][id],
1632  mMap_ppp[det][id],
1633  mMap_pph[det][id],
1634  colJ[mMap_jcol[det][id]],
1635  colW[mMap_wcol[det][id]],
1636  leng[mMap_jcol[det][id]]);
1637  fprintf(f1e,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1638  det,id,row,col,
1639  mMap_ehp[det][id],
1640  mMap_ns [det][id],
1641  mMap_crt[det][id],
1642  mMap_slt[det][id],
1643  mMap_dep[det][id],
1644  mMap_ch [det][id]);
1645  fprintf(f2,"%1d%02d-%02d%02d ",
1646  mMap_crt[det][id],mMap_slt[det][id],
1647  mMap_dep[det][id],mMap_ch[det][id]);
1648  }
1649  if(mScMap_ehp[det][id]>=0){
1650  fprintf(f5,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %2d J%1d\n",
1651  det,id,row,col,
1652  mScMap_ehp[det][id],
1653  mScMap_ns [det][id],
1654  mScMap_dep[det][id],
1655  mScMap_bra[det][id],
1656  mScMap_add[det][id],
1657  mScMap_sipm[det][id],
1658  mScMap_pp[det][id],
1659  mScMap_j[det][id]);
1660  fprintf(f5e,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1661  det,id,row,col,
1662  mScMap_ehp[det][id],
1663  mScMap_ns [det][id],
1664  mScMap_dep[det][id],
1665  mScMap_bra[det][id],
1666  mScMap_add[det][id],
1667  mScMap_sipm[det][id]);
1668  }
1669  id++;
1670  }
1671  fprintf(f2,"\n");
1672  }
1673  }
1674 
1675  //Hcal
1676  for(int ns=0; ns<2; ns++){
1677  det=ns+2;
1678  id=0;
1679  fprintf(f2,"Hcal NS=%1d\n",ns);
1680  for(row=1; row<=nRow(det); row++){
1681  for(col=1; col<=nColumn(det); col++){
1682  if(mMap_ehp[det][id]>=0){
1683  fprintf(f1,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %2d %2d %2d %2d P%1d H%02d %6s %6s %3.1f\n",
1684  det,id,row,col,
1685  mMap_ehp[det][id],
1686  mMap_ns[det][id],
1687  mMap_crt[det][id],
1688  mMap_slt[det][id],
1689  mMap_dep[det][id],
1690  mMap_ch[det][id],
1691  (row-1)/2,
1692  mScMap_add[det][id],
1693  mScMap_sipm[det][id],
1694  mMap_ppb[det][id],
1695  mMap_ppp[det][id],
1696  mMap_pph[det][id],
1697  colJH[mMap_jcol[det][id]],
1698  colW[mMap_wcol[det][id]],
1699  lengH[mMap_jcol[det][id]]);
1700  fprintf(f1c,"%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%2d,%2d,%2d,%2d,P%1d,H%02d,%6s,%6s,%3.1f\n",
1701  det,id,row,col,
1702  mMap_ehp[det][id],
1703  mMap_ns[det][id],
1704  mMap_crt[det][id],
1705  mMap_slt[det][id],
1706  mMap_dep[det][id],
1707  mMap_ch[det][id],
1708  (row-1)/2,
1709  mScMap_add[det][id],
1710  mScMap_sipm[det][id],
1711  mMap_ppb[det][id],
1712  mMap_ppp[det][id],
1713  mMap_pph[det][id],
1714  colJH[mMap_jcol[det][id]],
1715  colW[mMap_wcol[det][id]],
1716  lengH[mMap_jcol[det][id]]);
1717  fprintf(f1h,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1718  det,id,row,col,
1719  mMap_ehp[det][id],
1720  mMap_ns[det][id],
1721  mMap_crt[det][id],
1722  mMap_slt[det][id],
1723  mMap_dep[det][id],
1724  mMap_ch[det][id]);
1725  fprintf(f2,"%1d%02d-%02d%02d ",
1726  mMap_crt[det][id],mMap_slt[det][id],
1727  mMap_dep[det][id],mMap_ch[det][id]);
1728  }
1729  if(mScMap_ehp[det][id]>=0){
1730  fprintf(f5,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %2d J%1d\n",
1731  det,id,row,col,
1732  mScMap_ehp[det][id],
1733  mScMap_ns [det][id],
1734  mScMap_dep[det][id],
1735  mScMap_bra[det][id],
1736  mScMap_add[det][id],
1737  mScMap_sipm[det][id],
1738  mScMap_pp[det][id],
1739  mScMap_j[det][id]);
1740  fprintf(f5h,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1741  det,id,row,col,
1742  mScMap_ehp[det][id],
1743  mScMap_ns [det][id],
1744  mScMap_dep[det][id],
1745  mScMap_bra[det][id],
1746  mScMap_add[det][id],
1747  mScMap_sipm[det][id]);
1748  }
1749  id++;
1750  }
1751  fprintf(f2,"\n");
1752  }
1753  }
1754 
1755  //Prs
1756  for(int ns=0; ns<2; ns++){
1757  det=ns+4;
1758  id=0;
1759  fprintf(f2,"PRS NS=%1d\n",ns);
1760  for(row=1; row<=nRow(det); row++){
1761  for(col=1; col<=nColumn(det); col++){
1762  if(mMap_ehp[det][id]>=0){
1763  fprintf(f1,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %2d %2d %2d %2d P%1d H%02d %6s %6s %3.1f\n",
1764  det,id,row,col,
1765  mMap_ehp[det][id],
1766  mMap_ns[det][id],
1767  mMap_crt[det][id],
1768  mMap_slt[det][id],
1769  mMap_dep[det][id],
1770  mMap_ch[det][id],
1771  0,0,0,0,0,0,"x","x",0.0);
1772  fprintf(f1c,"%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%2d,%2d,%2d,%2d,P%1d,H%02d,%6s,%6s,%3.1f\n",
1773  det,id,row,col,
1774  mMap_ehp[det][id],
1775  mMap_ns[det][id],
1776  mMap_crt[det][id],
1777  mMap_slt[det][id],
1778  mMap_dep[det][id],
1779  mMap_ch[det][id],
1780  0,0,0,0,0,0,"x","x",0.0);
1781  fprintf(f1p,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1782  det,id,row,col,
1783  mMap_ehp[det][id],
1784  mMap_ns[det][id],
1785  mMap_crt[det][id],
1786  mMap_slt[det][id],
1787  mMap_dep[det][id],
1788  mMap_ch[det][id]);
1789  fprintf(f2,"%1d%02d-%02d%02d ",
1790  mMap_crt[det][id],mMap_slt[det][id],
1791  mMap_dep[det][id],mMap_ch[det][id]);
1792  }
1793  if(mScMap_ehp[det][id]>=0){
1794  fprintf(f5,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1795  det,id,row,col,
1796  mScMap_ehp[det][id],
1797  mScMap_ns [det][id],
1798  mScMap_dep[det][id],
1799  mScMap_bra[det][id],
1800  mScMap_add[det][id],
1801  mScMap_sipm[det][id]);
1802  fprintf(f5p,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1803  det,id,row,col,
1804  mScMap_ehp[det][id],
1805  mScMap_ns [det][id],
1806  mScMap_dep[det][id],
1807  mScMap_bra[det][id],
1808  mScMap_add[det][id],
1809  mScMap_sipm[det][id]);
1810  }
1811  id++;
1812  }
1813  fprintf(f2,"\n");
1814  }
1815  }
1816 
1817  //DEP map
1818  for(ehp=0; ehp<kFcsEHP; ehp++){
1819  for(ns=0; ns<2; ns++){
1820  for(dep=0; dep<kFcsMaxDepBd; dep++){
1821  if(ehp==1 && dep>8) break;
1822  if(ehp==2 && dep>3) break;
1823  int flag=0;
1824  for(ch=0; ch<kFcsMaxDepCh; ch++){
1825  if(mRMap_det[ehp][ns][dep][ch]<0){
1826  if(flag==1 && ch%8==7) fprintf(f4,"\n");
1827  continue;
1828  }
1829  flag=1;
1830  det = mRMap_det[ehp][ns][dep][ch];
1831  id = mRMap_id[ehp][ns][dep][ch];
1832  crt=mRMap_crt[ehp][ns][dep][ch];
1833  slt=mRMap_slt[ehp][ns][dep][ch];
1834  if(ch==0){
1835  fprintf(f4,"%2s%2d : NS=%1d %1d(%4s) DEP%02d : ",CRT[crt],slt,ns,ehp,EHP[ehp],dep);
1836  }else if(ch%8==0){
1837  fprintf(f4," : ");
1838  }
1839  if(det>=0 && det<kFcsNDet){
1840  row=getRowNumber(det,id);
1841  col=getColumnNumber(det,id);
1842  if(ehp<2){
1843  const char* colj; float len;
1844  if(ehp==0) { colj=colJ[mMap_jcol[det][id]]; len=leng[mMap_jcol[det][id]]; }
1845  else { colj=colJH[mMap_jcol[det][id]]; len=lengH[mMap_jcol[det][id]];}
1846  fprintf(f3,"%3d %3d %3d %3d %2s %2d %3d %3d %3d %3d %2d %2d %2d %2d P%1d H%02d %6s %6s %3.1f\n",
1847  ehp,ns,dep,ch,
1848  CRT[crt],slt,
1849  det,id,row,col,
1850  (row-1)/2,
1851  mScMap_add[det][id],
1852  mScMap_sipm[det][id],
1853  mMap_ppb[det][id],
1854  mMap_ppp[det][id],
1855  mMap_pph[det][id],
1856  colj,
1857  colW[mMap_wcol[det][id]],
1858  len);
1859  fprintf(f3c,"%3d,%3d,%3d,%3d,%2s,%2d,%3d,%3d,%3d,%3d,%2d,%2d,%2d,%2d,P%1d,H%02d,%6s,%6s,%3.1f\n",
1860  ehp,ns,dep,ch,
1861  CRT[crt],slt,
1862  det,id,row,col,
1863  (row-1)/2,
1864  mScMap_add[det][id],
1865  mScMap_sipm[det][id],
1866  mMap_ppb[det][id],
1867  mMap_ppp[det][id],
1868  mMap_pph[det][id],
1869  colj,
1870  colW[mMap_wcol[det][id]],
1871  len);
1872  }else{
1873  fprintf(f3,"%3d %3d %3d %3d %2s %2d %3d %3d %3d %3d\n",
1874  ehp,ns,dep,ch,
1875  CRT[crt],slt,
1876  det,id,row,col);
1877  fprintf(f3c,"%3d,%3d,%3d,%3d,%2s,%2d,%3d,%3d,%3d,%3d\n",
1878  ehp,ns,dep,ch,
1879  CRT[crt],slt,
1880  det,id,row,col);
1881  }
1882  }
1883  fprintf(f4,"%2s%03d-%02d/%02d ",DET[det],id,row,col);
1884  if(ch%8==7) fprintf(f4,"\n");
1885  }
1886  }
1887  }
1888  }
1889 
1890  //SC reverse map
1891  for(ehp=0; ehp<kFcsEHP; ehp++){
1892  for(ns=0; ns<2; ns++){
1893  for(dep=0; dep<kFcsMaxDepBd; dep++){
1894  if(ehp==1 && dep>8) break;
1895  if(ehp==2 && dep>3) break;
1896  for(int bra=0; bra<kFcsMaxBranch; bra++){
1897  for(int add=0; add<kFcsMaxAddr; add++){
1898  for(int sipm=0; sipm<kFcsMaxSiPM; sipm++){
1899  if(mRScMap_det[ehp][ns][dep][bra][add][sipm]>=0){
1900  int det=mRScMap_det[ehp][ns][dep][bra][add][sipm];
1901  int id =mRScMap_id[ehp][ns][dep][bra][add][sipm];
1902  int col=getColumnNumber(det,id);
1903  int row=getRowNumber(det,id);
1904  fprintf(f6,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1905  ehp,ns,dep,bra,add,sipm,
1906  det,id,row,col);
1907  }
1908  }
1909  }
1910  }
1911  }
1912  }
1913  }
1914 
1915  //EPD map
1916  fprintf(f7,"#ehp ns crt slt dep ch det id row col pp tt Reversed(det id r c)\n");
1917  for(int det=4; det<=5; det++){
1918  for(int r=1; r<=nRow(det); r++){
1919  for(int c=1; c<=nColumn(det); c++){
1920  int id = getId(det,r,c);
1921  int ehp,ns,crt,slt,dep,ch,det2,id2,r2,c2,pp,tt;
1922  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
1923  getEPDfromId(det,id,pp,tt);
1924  getIdfromEPD(pp,tt,det2,id2);
1925  c2=getColumnNumber(det2,id2);
1926  r2=getRowNumber(det2,id2);
1927  fprintf(f7,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1928  ehp,ns,crt,slt,dep,ch,
1929  det,id,r,c,
1930  pp,tt,
1931  det2,id2,r2,c2);
1932  }
1933  }
1934  }
1935 
1936  for(int ehp=0; ehp<2; ehp++){
1937  for(int ns=0; ns<2; ns++){
1938  if(ehp==0 && ns==1) continue;
1939  int bmax=0;
1940  if(ehp==0) {bmax=8; fprintf(fpp,"Ecal\n");}
1941  if(ehp==1 && ns==0) {bmax=4; fprintf(fpp,"Hcal North\n");}
1942  if(ehp==1 && ns==1) {bmax=4; fprintf(fpp,"Hcal South\n");}
1943  fprintf(fpp,"PPB# P# DEP Ch T Pwr/ctrl Row#\n");
1944  for(int b=1; b<=bmax; b++){
1945  for(int p=2; p<=7; p++){
1946  int dep,lh,scr;
1947  if(ehp==0){
1948  dep = EPPMap[b-1][p-2][0];
1949  lh = EPPMap[b-1][p-2][1];
1950  scr = EPPMap[b-1][p-2][2];
1951  }else{
1952  dep = HPPMap[b-1][p-2][0];
1953  lh = HPPMap[b-1][p-2][1];
1954  scr = HPPMap[b-1][p-2][2];
1955  }
1956  fprintf(fpp,"%2d %2d ",b,p);
1957  if(lh>=0) {
1958  fprintf(fpp,"%2d %02d-%02d ",dep,lh*16,lh*16+15);
1959  for(int i=0; i<4; i++){
1960  int t = (p-2)*4+i;
1961  int ch = lh*16 + i*4;
1962  int det = mRMap_det[ehp][ns][dep][ch];
1963  int id = mRMap_id[ehp][ns][dep][ch];
1964  int row = getRowNumber(det,id);
1965  int col = jacketColor(ehp,ns,dep,ch);
1966  if(id>=0) {
1967  if(ehp==0){
1968  fprintf(fpp,"T%02d=R%02d-%6s ",t,row,colJ[col]);
1969  }else{
1970  fprintf(fpp,"T%02d=R%02d-%6s ",t,row,colJH[col]);
1971  }
1972  }else{
1973  fprintf(fpp,"T%02d= ",t);
1974  }
1975  }
1976  }else{
1977  fprintf(fpp," - ");
1978  }
1979  if(p%2==0) {
1980  fprintf(fpp,"J%1d ",p/2);
1981  if(scr>0) fprintf(fpp,"%2d,%2d",scr*2-1,scr*2);
1982  else if(scr==-2) fprintf(fpp,"no power");
1983  else fprintf(fpp,"-");
1984  }
1985  fprintf(fpp,"\n");
1986  }
1987  }
1988  }
1989  }
1990 
1991  fclose(f1);
1992  fclose(f1c);
1993  fclose(f1e);
1994  fclose(f1h);
1995  fclose(f1p);
1996  fclose(f2);
1997  fclose(f3);
1998  fclose(f3c);
1999  fclose(f4);
2000  fclose(f5);
2001  fclose(f5e);
2002  fclose(f5h);
2003  fclose(f5p);
2004  fclose(f6);
2005  fclose(f7);
2006  fclose(fpp);
2007 }
2008 
2009 // factor= 1(ET Match), 0(E Match), 0.5(halfway)
2010 float StFcsDb::getEtGain(int det, int id, float factor) const{
2011  if(det<0 || det>=kFcsNDet) return 0.0;
2012  if(id<0 || id>=kFcsMaxId) return 0.0;
2013  return (mEtGain[det][id]-1.0)*factor+1.0;
2014 }
2015 
2017  // double norm[2]={0.24711, 0.21781}; // [MeV/coint]
2018  double norm[2]={0.24711, 0.24711};
2019  double xoff[2]={6.570*2.54 - 6.850*2.54, 7.430*2.54-8.380*2.54};
2020  double yoff[2]={-5.26,1.80};
2021  double gain=0.0053;
2022  if(mEtGainMode==1){
2023  gain/=0.7;
2024  norm[1]=0.24711;
2025  }
2026  for(int det=0; det<kFcsNDet; det++){
2027  int eh=det/2;
2028  int ns=det%2;
2029  for(int i=0; i<maxId(det); i++){
2030  double ratio=1.0;
2031  if(eh<2){ //PRES stays 1.0
2032  StThreeVectorD xyz=getStarXYZ(det,i);
2033  if(mEtGainMode==1) {
2034  if(ns==0) xyz.setX(xyz.x()-xoff[eh]);
2035  else xyz.setX(xyz.x()+xoff[eh]);
2036  xyz.setY(xyz.y()+yoff[eh]);
2037  }
2038  double r=xyz.perp();
2039  double l=xyz.mag();
2040  double ptch=gain/l*r;
2041  ratio=ptch/norm[eh]*1000;
2042  //printf("BBB det=%1d id=%3d gain=%8.5f xyz=%6.2f %6.2f %6.2f r=%6.2f l=%6.2f ptch=%6.2f ratio==%6.2f\n",
2043  //det,i,gain,xyz.x(),xyz.y(),xyz.z(),r,l,ptch,ratio);
2044  }
2045  mEtGain[det][i]=ratio;
2046  }
2047  }
2048  if(mDebug==0) return;
2049 
2050  FILE *f1 = fopen("fcsPtGain.txt","w");
2051  FILE *f2 = fopen("fcsPtGain2.txt","w");
2052  FILE *f3 = fopen("fcsPtGain3.txt","w");
2053  FILE *f4 = fopen("fcs_ecal_phys_gains.txt","w");
2054  FILE *f5 = fopen("fcs_hcal_phys_gains.txt","w");
2055  FILE *f6 = fopen("fcs_ecal_calib_gains.txt","w");
2056  FILE *f7 = fopen("fcs_hcal_calib_gains.txt","w");
2057  fprintf(f4,"#ehp ns dep ch EtGain\n");
2058  fprintf(f5,"#ehp ns dep ch EtGain\n");
2059  fprintf(f6,"#ehp ns dep ch CalibGain\n");
2060  fprintf(f7,"#ehp ns dep ch CalibGain\n");
2061  for(int det=0; det<kFcsNDet; det++){
2062  int id=0;
2063  int eh=det/2;
2064  double gain=getGain(det,0);
2065  fprintf(f2,"DET=%1d ET/ch [unit = MeV/count]\n", det);
2066  fprintf(f3,"DET=%1d normalized ET/ch [unit=%f MeV/count]\n", det,norm[eh]);
2067  for(int row=1; row<=nRow(det); row++){
2068  for(int col=1; col<=nColumn(det); col++){
2069  StThreeVectorD xyz=getStarXYZ(det,id);
2070  double r=xyz.perp();
2071  double x=xyz.x();
2072  double y=xyz.y();
2073  double z=xyz.z();
2074  double l=xyz.mag();
2075  double ptch=gain/l*r;
2076  double ratio=1.0;
2077  if(eh<2) ratio=ptch/norm[eh]*1000; //PRES stays 1.0
2078  fprintf(f1,"D=%1d Id=%3d Row=%2d Column=%2d xyz=%7.2f %7.2f %7.2f Gain=%7.5f ET/ch=%6.4f [MeV/count] norm=%6.4f\n",
2079  det,id,row,col,x,y,z,gain,ptch*1000,ratio);
2080  fprintf(f2,"%7.5f ", ptch*1000);
2081  fprintf(f3,"%7.5f ", ratio);
2082  id++;
2083  }
2084  fprintf(f2,"\n");
2085  fprintf(f3,"\n");
2086  }
2087  }
2088  for(int ehp=0; ehp<2; ehp++){
2089  for(int ns=0; ns<2; ns++){
2090  for(int dep=0; dep<getNDep(ehp,ns); dep++){
2091  for(int ch=0; ch<kFcsMaxDepCh; ch++){
2092  int det,id,crt,slt;
2093  getIdfromDep(ehp,ns,dep,ch,det,id,crt,slt);
2094  float ratio = mEtGain[det][id];
2095  float calib = (ratio-1.0)/2.0+1.0;
2096  if(ehp==0){
2097  fprintf(f4,"%3d %3d %3d %3d %8.4f\n",ehp,ns,dep,ch,ratio);
2098  fprintf(f6,"%3d %3d %3d %3d %8.4f\n",ehp,ns,dep,ch,calib);
2099  }else{
2100  fprintf(f5,"%3d %3d %3d %3d %8.4f\n",ehp,ns,dep,ch,ratio);
2101  fprintf(f7,"%3d %3d %3d %3d %8.4f\n",ehp,ns,dep,ch,calib);
2102  }
2103  }
2104  }
2105  }
2106  }
2107  fclose(f1);
2108  fclose(f2);
2109  fclose(f3);
2110  fclose(f4);
2111  fclose(f5);
2112  fclose(f6);
2113  fclose(f7);
2114 }
2115 
2116 float StFcsDb::pedestal(int ehp, int ns, int dep, int ch){
2117  return mPed[ehp][ns][dep][ch];
2118 }
2119 
2120 void StFcsDb::setPedestal(int ehp, int ns, int dep, int ch, float ped){
2121  mPed[ehp][ns][dep][ch]=ped;
2122 }
2123 
2124 void StFcsDb::readPedFromText(const char* file){
2125  memset(mPed,0,sizeof(mPed));
2126  LOG_INFO << Form("Reading Pedestal from %s",file)<<endm;
2127  FILE* F=fopen(file,"r");
2128  if(F == NULL){
2129  LOG_ERROR << Form("Could not open %s",file)<<endm;
2130  return;
2131  }
2132  int ehp,ns,dep,ch;
2133  float mean,rms;
2134  while(fscanf(F,"%d %d %d %d %f %f",&ehp,&ns,&dep,&ch,&mean,&rms) != EOF){
2135  mPed[ehp][ns][dep][ch]=mean;
2136  LOG_INFO << Form("PED %1d %1d %2d %2d %f %f",ehp,ns,dep,ch,mPed[ehp][ns][dep][ch],rms)<<endm;
2137  }
2138  fclose(F);
2139 }
2140 
2141 void StFcsDb::readGainFromText(){
2142  memset(mGain,0,sizeof(mGain));
2143  LOG_INFO << Form("Reading Gain from %s",mGainFilename)<<endm;
2144  FILE* F=fopen(mGainFilename,"r");
2145  if(F == NULL){
2146  LOG_ERROR << Form("Could not open %s",mGainFilename)<<endm;
2147  return;
2148  }
2149  int ehp,ns,dep,ch;
2150  float gain;
2151  while(fscanf(F,"%d %d %d %d %f",&ehp,&ns,&dep,&ch,&gain) != EOF){
2152  mGain[ehp][ns][dep][ch]=gain;
2153  LOG_INFO<<Form("GAIN %1d %1d %2d %2d %f",ehp,ns,dep,ch,mGain[ehp][ns][dep][ch])<<endm;
2154  }
2155  fclose(F);
2156 }
2157 
2158 void StFcsDb::readGainCorrFromText(){
2159  memset(mGainCorr,0,sizeof(mGainCorr));
2160  LOG_INFO << Form("Reading GainCorr from %s",mGainCorrFilename)<<endm;
2161  FILE* F=fopen(mGainCorrFilename,"r");
2162  if(F == NULL){
2163  LOG_ERROR << Form("Could not open %s",mGainCorrFilename)<<endm;
2164  return;
2165  }
2166  int ehp,ns,dep,ch;
2167  float gain;
2168  while(fscanf(F,"%d %d %d %d %f",&ehp,&ns,&dep,&ch,&gain) != EOF){
2169  mGainCorr[ehp][ns][dep][ch]=gain;
2170  LOG_INFO<<Form("GAIN CORR %1d %1d %2d %2d %f",ehp,ns,dep,ch,mGainCorr[ehp][ns][dep][ch])<<endm;
2171  }
2172  fclose(F);
2173 }
2174 
2175 //g2t track info
2176 unsigned int StFcsDb::backTraceG2tTrack(unsigned int id, g2t_track_st* g2ttrk){
2177  int i = id - 1;
2178  while(g2ttrk[i].next_parent_p !=0){
2179  if(mDebug>3)
2180  LOG_INFO<<Form(" BackTrace from=%3d id=%3d Epid=%4d Gpid=%3d Vtx=%3d Parent=%3d E=%6.2f",
2181  id,g2ttrk[i].id,g2ttrk[i].eg_pid,g2ttrk[i].ge_pid,g2ttrk[i].start_vertex_p,
2182  g2ttrk[i].next_parent_p,g2ttrk[i].e)<<endm;
2183  i = g2ttrk[i].next_parent_p - 1;
2184  }
2185  if(mDebug>3) LOG_INFO<<Form(" BackTrace from=%3d id=%3d Epid=%4d Gpid=%3d Vtx=%3d Parent=%3d E=%6.2f Primary!!",
2186  id,g2ttrk[i].id,g2ttrk[i].eg_pid,g2ttrk[i].ge_pid,g2ttrk[i].start_vertex_p,
2187  g2ttrk[i].next_parent_p,g2ttrk[i].e)<<endm;
2188  return i + 1;
2189 }
2190 
2191 const g2t_track_st* StFcsDb::getParentG2tTrack(StFcsHit* h, g2t_track_st* g2ttrk, float& fraction, int& ntrk, unsigned int order){
2192  StFcsCluster c;
2193  c.hits().push_back(h); //dummy cluster with 1 hit
2194  return getG2tTrack(&c,g2ttrk,fraction,ntrk,order,0);
2195 }
2196 
2197 const g2t_track_st* StFcsDb::getPrimaryG2tTrack(StFcsHit* h, g2t_track_st* g2ttrk, float& fraction, int& ntrk,unsigned int order){
2198  StFcsCluster c;
2199  c.hits().push_back(h); //dummy cluster with 1 hit
2200  return getG2tTrack(&c,g2ttrk,fraction,ntrk,order,1);
2201 }
2202 
2203 const g2t_track_st* StFcsDb::getParentG2tTrack(StFcsCluster* c, g2t_track_st* g2ttrk, float& fraction, int& ntrk, unsigned int order){
2204  return getG2tTrack(c,g2ttrk,fraction,ntrk,order,0);
2205 }
2206 
2207 const g2t_track_st* StFcsDb::getPrimaryG2tTrack(StFcsCluster* c, g2t_track_st* g2ttrk, float& fraction, int& ntrk, unsigned int order){
2208  return getG2tTrack(c,g2ttrk,fraction,ntrk,order,1);
2209 }
2210 
2211 const g2t_track_st* StFcsDb::getG2tTrack(StFcsCluster* c, g2t_track_st* g2ttrk, float& fraction, int& ntrk, unsigned int order, int mode){
2212  float detot=0;
2213  vector<pair<unsigned int, float>> parents;
2214  for(const StFcsHit* hit : c->hits()) {
2215  for(const pair<unsigned int, float> & gt : hit->getGeantTracks()){
2216  unsigned int id=0;
2217  switch(mode){
2218  case 0: id=gt.first; break;
2219  case 1: id=backTraceG2tTrack(gt.first,g2ttrk); break;
2220  }
2221  float de=gt.second;
2222  int found=0;
2223  for(pair<unsigned int, float>& p : parents){
2224  if(p.first == id) {p.second += de; found=1; break;}
2225  }
2226  if(found==0) parents.push_back(make_pair(id,de));
2227  detot+=de;
2228  }
2229  }
2230  ntrk=parents.size();
2231  if(order >= ntrk) {fraction=0; return 0;}
2232  std::nth_element(parents.begin(), parents.begin()+order, parents.end(),
2233  [](const pair<unsigned int,float>&a, const pair<unsigned int,float>&b){
2234  return b.second < a.second;
2235  });
2236  fraction = parents[order].second / detot;
2237  if(mDebug>3){
2238  for(unsigned int jtrk=0; jtrk<ntrk; jtrk++){
2239  LOG_INFO << Form("Cluster's G2T Track %3d mode=%1d id=%3d dE=%f",jtrk,mode,parents[jtrk].first,parents[jtrk].second)<<endm;
2240  }
2241  }
2242  return &g2ttrk[parents[order].first-1];
2243 }
float getGainOnline(int det, int id) const
get the pres valley position for cut
Definition: StFcsDb.cxx:984
int getColumnNumber(int det, int id) const
get the row number for the channel
Definition: StFcsDb.cxx:490
void setPedestal(int ehp, int ns, int dep, int ch, float ped)
get Pedestal
Definition: StFcsDb.cxx:2120
void printHeader3(FILE *f)
Map header.
Definition: StFcsDb.cxx:1542
void setFcsEcalGainOnline(fcsEcalGainOnline_st *)
set fcsPresValley_st*
Definition: StFcsDb.cxx:290
void makePPMap()
Generate maps (this is the origin of the map)
Definition: StFcsDb.cxx:1129
void makeMap2019()
cable jacket color
Definition: StFcsDb.cxx:1368
void getIdfromEPD(int pp, int tt, int &det, int &id)
Map header.
Definition: StFcsDb.cxx:1467
void printHeader(FILE *f, int flag, int csv)
Print maps.
Definition: StFcsDb.cxx:1496
float pedestal(int ehp, int ns, int dep, int ch)
Get EPD&#39;s EPD map from FCS mapping.
Definition: StFcsDb.cxx:2116
StLorentzVectorD getLorentzVector(const StThreeVectorD &xyz, float energy, float zVertex=0.0)
Get get 4 vector assuing m=0 and taking beamline from DB.
Definition: StFcsDb.cxx:895
int getZeroSuppression(int det) const
fcsGain/GainCorrection related
Definition: StFcsDb.cxx:920
float getGainCorrection(int det, int id) const
get the gain for the channel for 8 timebin sum
Definition: StFcsDb.cxx:952
void setFcsPresGain(fcsPresGain_st *)
set fcsHcalGain_st*
Definition: StFcsDb.cxx:270
int getRowNumber(int det, int id) const
maximum number of id
Definition: StFcsDb.cxx:485
float getEta(int det, float FcsX, float FcsY, float FcsZ=-1.0, float zVertex=0.0) const
get the STAR frame pseudo rapidity from the vertex from local X/Y [cm]
Definition: StFcsDb.cxx:891
int jacketColor(int ehp, int ns, int dep, int ch)
Generate Patchpanel map.
Definition: StFcsDb.cxx:1097
static int getDetFromName(const std::string &detname)
Get det/id from name.
Definition: StFcsDb.cxx:556
float getPhi(int det, float FcsX, float FcsY, float FcsZ=-1.0) const
get the STAR frame phi angle from from local X/Y [cm]
Definition: StFcsDb.cxx:887
void setFcsPresValley(fcsPresValley_st *)
set fcsHcalGainCorr_st*
Definition: StFcsDb.cxx:285
void setFcsHcalGain(fcsHcalGain_st *)
set fcsEcalGain_st*
Definition: StFcsDb.cxx:265
StThreeVectorD projectLine(int det, StThreeVectorD &linedirection, StThreeVectorD &lineorigin, double showermaxz=-1) const
Like #projectLine(det, double*, double*, double) except use StThreeVectorD for line direction and ori...
Definition: StFcsDb.cxx:810
int northSouth(int det) const
Ecal=0, Hcal=1, Pres=2.
Definition: StFcsDb.cxx:430
void getName(int det, int id, char name[])
get the DEP/ch id
Definition: StFcsDb.cxx:505
void setLeakyHcal(int v=1)
set run19 geometry, otherwise final run21
Definition: StFcsDb.cxx:253
StThreeVectorD getDetectorOffset(int det, double zdepth=-1) const
Utility functions related to DetectorPosition.
Definition: StFcsDb.cxx:578
int detectorId(int eh, int ns) const
6
Definition: StFcsDb.cxx:418
double getHcalProjectedToEcalX(int ns, double hcalLocalX, double zvtx=0.0)
Project Hcal local X to Ecal local X [cm].
Definition: StFcsDb.cxx:695
const g2t_track_st * getParentG2tTrack(StFcsHit *h, g2t_track_st *g2ttrk, float &fraction, int &ntrk, unsigned int order=0)
Definition: StFcsDb.cxx:2191
float getSamplingFraction(int det) const
get zero suppression threshold
Definition: StFcsDb.cxx:913
StThreeVectorD getStarXYZfromColumnRow(int det, float col, float row, float FcsZ=-1.0) const
get the STAR frame cooridnates from other way
Definition: StFcsDb.cxx:863
void readPedFromText(const char *file="fcsped.txt")
setting pedestal
Definition: StFcsDb.cxx:2124
void getIdfromDep(int ehp, int ns, int dep, int ch, int &detectorId, int &id, int &crt, int &slt) const
Get DEP map.
Definition: StFcsDb.cxx:1031
void setRun19(int v=1)
set run#
Definition: StFcsDb.cxx:252
StThreeVectorD projectTrackToEcal(const g2t_track_st *g2ttrk, const g2t_vertex_st *g2tvert=0) const
project a g2t track to Ecal with a given track and vertex. If no vertex given assume a vertex of (0...
Definition: StFcsDb.cxx:760
float getPresThreshold(int det, int id) const
get the gain correction for the channel
Definition: StFcsDb.cxx:1005
float getZDepth(int det) const
get the Y width of the cell
Definition: StFcsDb.cxx:639
void printHeader4(FILE *f, int flag)
Map header.
Definition: StFcsDb.cxx:1551
float getXWidth(int det) const
get the angle of the detector
Definition: StFcsDb.cxx:627
float getGain8(int det, int id) const
get the gain for the channel for 16 timebin sum
Definition: StFcsDb.cxx:1014
void setFcsHcalGainOnline(fcsHcalGainOnline_st *)
set fcsEcalGainOnline_st*
Definition: StFcsDb.cxx:295
void setFcsEcalGainCorr(fcsEcalGainCorr_st *)
set fcsPresGain_st*
Definition: StFcsDb.cxx:275
void getLocalXYinCell(StFcsHit *hit, float &x, float &y) const
getting XY in local cell coordinate
Definition: StFcsDb.cxx:652
StThreeVectorD getStarXYZ(int det, float FcsX, float FcsY, float FcsZ=-1.0, float zVertex=0.0) const
get the STAR frame cooridnates from local XYZ [cm]
Definition: StFcsDb.cxx:868
void setRun(int run)
enable(1) or disable(0) offline DB access
Definition: StFcsDb.cxx:251
void setFcsEcalGain(fcsEcalGain_st *)
set fcsDetectorPosition_st*
Definition: StFcsDb.cxx:260
void getEPDfromId(int det, int id, int &pp, int &tt)
Get FCS&#39;s EPD map foom EPD mapping.
Definition: StFcsDb.cxx:1484
int ecalHcalPres(int det) const
Ecal North=0, Ecal South=1, Hcal North=2, Hcal South=3, Pres=4/5.
Definition: StFcsDb.cxx:423
float getGain(int det, int id) const
get sampling fraction
Definition: StFcsDb.cxx:928
int maxDetectorId() const
set fcsPresThreshold_st*
Definition: StFcsDb.cxx:416
void setDbAccess(int v=1)
debug level
Definition: StFcsDb.cxx:250
StThreeVectorD projectTrackToEcalSMax(const g2t_track_st *g2ttrk, const g2t_vertex_st *g2tvert=0) const
SMax = Shower Max Z.
Definition: StFcsDb.cxx:778
static unsigned short getKey(unsigned short detid, unsigned short id)
Get det from name.
Definition: StFcsDb.cxx:569
float getPresValley(int det, int id) const
get the gain correction for the channel
Definition: StFcsDb.cxx:976
float getDetectorAngle(int det) const
This is the vector normal to the detector plane.
Definition: StFcsDb.cxx:617
void printHeader2(FILE *f)
Map header.
Definition: StFcsDb.cxx:1534
int getNDep(int ehp, int ns) const
Get Det map.
Definition: StFcsDb.cxx:1044
void printMap()
Generate map for run19.
Definition: StFcsDb.cxx:1574
int getDepCh(int dep, int ch) const
get the id from row/col
Definition: StFcsDb.cxx:501
Definition: Stypes.h:40
StThreeVectorD projectTrack(int det, const g2t_track_st *g2ttrk, const g2t_vertex_st *g2tvert, double showermaxz=-1) const
Generic g2t track projection function but #det and #showermaxz needs to be specified; if #det or #sho...
Definition: StFcsDb.cxx:796
int maxId(int det) const
number of column
Definition: StFcsDb.cxx:468
void makeMap()
Get Id from SC.
Definition: StFcsDb.cxx:1166
void getIdfromSCmap(int ehp, int ns, int scdep, int branch, int fee_i2c, int sipm, int &det, int &id) const
Get SC map.
Definition: StFcsDb.cxx:1082
void getSCmap(int det, int id, int &ehp, int &ns, int &scdep, int &branch, int &fee_i2c, int &sipm, int &pp, int &jacket) const
of DEP
Definition: StFcsDb.cxx:1065
int getId(int det, int row, int col) const
get the column number for the channel
Definition: StFcsDb.cxx:495
int nRow(int det) const
north or south side
Definition: StFcsDb.cxx:434
StThreeVectorD projectTrackToHcal(const g2t_track_st *g2ttrk, const g2t_vertex_st *g2tvert=0) const
project a g2t track to Hcal with a given track and vertex. If no vertex given assume a vertex of (0...
Definition: StFcsDb.cxx:769
void setFcsPresThreshold(fcsPresThreshold_st *)
set fcsHcalGainOnline_st*
Definition: StFcsDb.cxx:300
void setFcsDetectorPosition(fcsDetectorPosition_st *t)
set Et Gain Setting 0=Auto from run#, 1=Old, 2=New(23027048)
Definition: StFcsDb.cxx:255
int nColumn(int det) const
number of rows
Definition: StFcsDb.cxx:451
unsigned int backTraceG2tTrack(unsigned int id, g2t_track_st *g2ttrk)
reading pedestal from text
Definition: StFcsDb.cxx:2176
static void getFromName(const char name[], int &det, int &id)
Get Name of a channel.
Definition: StFcsDb.cxx:547
void printEtGain()
ET gain.
Definition: StFcsDb.cxx:2016
StThreeVectorD getStarXYZ_4x4(int det, int col, int row) const
Get the STAR frame cooridnates for 4x4 sum.
Definition: StFcsDb.cxx:678
StThreeVectorD projectTrackToHcalSMax(const g2t_track_st *g2ttrk, const g2t_vertex_st *g2tvert=0) const
SMax = Shower Max Z.
Definition: StFcsDb.cxx:787
void setFcsHcalGainCorr(fcsHcalGainCorr_st *)
set fcsEcalGainCorr_st*
Definition: StFcsDb.cxx:280
float getYWidth(int det) const
get the X width of the cell
Definition: StFcsDb.cxx:635
double getHcalProjectedToEcalY(int ns, double hcalLocalY, double zvtx=0.0)
Project Hcal local Y to Ecal local Y [cm].
Definition: StFcsDb.cxx:713
void getDepfromId(int detectorId, int id, int &ehp, int &ns, int &crt, int &slt, int &dep, int &ch) const
print ET gain
Definition: StFcsDb.cxx:1018
double getProjectedDistance(StFcsCluster *ecal, StFcsCluster *hcal, double zvtx=0.0)
Project Hcal cluster to Ecal plane and get distance from Ecal cluster [cm].
Definition: StFcsDb.cxx:725