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 int StFcsDb::InitRun(int runNumber) {
291  LOG_INFO << "StFcsDb::InitRun - run = " << runNumber << endm;
292  mRun=runNumber;
293 
294  if(mRun19>0){
295  makeMap2019();
296  }else{
297  makeMap();
298  printEtGain();
299  }
300  if(mDebug>0) {
301  printMap();
302  }
303 
304  //storing in DEP sorted table
305  if(mGainMode==GAINMODE::DB){
306  int ie=0, ih=0, ip=0, ehp, ns, crt, slt, dep, ch;
307  for(int ins=0; ins<kFcsNorthSouth; ins++){
308  int det=kFcsEcalNorthDetId+ins;
309  for(int id=0; id<maxId(det); id++){
310  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
311  mGain[ehp][ns][dep][ch]=mFcsEcalGain.gain[ie];
312  ie++;
313  }
314  det=kFcsHcalNorthDetId+ins;
315  for(int id=0; id<maxId(det); id++){
316  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
317  mGain[ehp][ns][dep][ch]=mFcsHcalGain.gain[ih];
318  ih++;
319  }
320  det=kFcsPresNorthDetId+ins;
321  for(int id=0; id<maxId(det); id++){
322  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
323  mGain[ehp][ns][dep][ch]=mFcsPresGain.gain[ip];
324  ip++;
325  }
326  }
327  }else if(mGainMode==GAINMODE::TXT){
328  readGainFromText();
329  }
330 
331  if(mGainCorrMode==GAINMODE::DB){
332  int ie=0, ih=0, ip=0, ehp, ns, crt, slt, dep, ch;
333  for(int ins=0; ins<kFcsNorthSouth; ins++){
334  int det=kFcsEcalNorthDetId+ins;
335  for(int id=0; id<maxId(det); id++){
336  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
337  mGainCorr[ehp][ns][dep][ch]=mFcsEcalGainCorr.gaincorr[ie];
338  ie++;
339  }
340  det=kFcsHcalNorthDetId+ins;
341  for(int id=0; id<maxId(det); id++){
342  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
343  mGainCorr[ehp][ns][dep][ch]=mFcsHcalGainCorr.gaincorr[ih];
344  ih++;
345  }
346  det=kFcsPresNorthDetId+ins;
347  for(int id=0; id<maxId(det); id++){
348  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
349  mGainCorr[ehp][ns][dep][ch]=mFcsPresValley.valley[ip];
350  ip++;
351  }
352  }
353  }else if(mGainCorrMode==GAINMODE::TXT){
354  readGainCorrFromText();
355  }
356 
357  // Get beamline
358  TDataSet* dbDataSet = StMaker::GetChain()->GetDataBase("Calibrations/rhic/vertexSeed");
359  if(dbDataSet){
360  vertexSeed_st* vSeed = ((St_vertexSeed*) (dbDataSet->FindObject("vertexSeed")))->GetTable();
361  if(vSeed){
362  mVx = vSeed->x0;
363  mVy = vSeed->y0;
364  mVdxdz = vSeed->dxdz;
365  mVdydz = vSeed->dydz;
366  mThetaX = TMath::ATan( mVdxdz );
367  mThetaY = TMath::ATan( mVdydz );
368  }
369  }
370 
371  return kStOK;
372 }
373 
374 int StFcsDb::maxDetectorId() const {return kFcsNDet;}
375 
376 int StFcsDb::detectorId(int eh, int ns) const {
377  if(eh>=0 && eh<kFcsEHP && ns>=0 && ns<kFcsNorthSouth) return eh*2 + ns;
378  return -1;
379 }
380 
381 int StFcsDb::ecalHcalPres(int det) const {
382  if(det==0 || det==1) return 0;
383  if(det==2 || det==3) return 1;
384  if(det==4 || det==5) return 2;
385  return -1;
386 }
387 
388 int StFcsDb::northSouth(int det) const{
389  return det%2;
390 }
391 
392 int StFcsDb::nRow(int det) const{
393  int ehp=ecalHcalPres(det);
394  if(mRun19>0){
395  int ns = northSouth(det);
396  if(ns==0) return 0;
397  if (ehp==0){return 8;}
398  else if(ehp==1){return 4;}
399  else if(ehp==2){return 9;}
400  return -1;
401  }else{
402  if (ehp==0){return kFcsEcalNRow;}
403  else if(ehp==1){return kFcsHcalNRow;}
404  else if(ehp==2){return kFcsPresNRow;}
405  return -1;
406  }
407 }
408 
409 int StFcsDb::nColumn(int det) const{
410  int ehp=ecalHcalPres(det);
411  if(mRun19>0){
412  int ns = northSouth(det);
413  if(ns==0) return 0;
414  if (ehp==0){return 8;}
415  else if(ehp==1){return 4;}
416  else if(ehp==2){return 1;}
417  return -1;
418  }else{
419  if (ehp==0){return kFcsEcalNCol;}
420  else if(ehp==1){return kFcsHcalNCol;}
421  else if(ehp==2){return kFcsPresNCol;}
422  return -1;
423  }
424 }
425 
426 int StFcsDb::maxId(int det) const{
427  int ehp=ecalHcalPres(det);
428  if(mRun19>0){
429  int ns = northSouth(det);
430  if(ns==0) return 0;
431  if (ehp==0){return 64;}
432  else if(ehp==1){return 16;}
433  else if(ehp==2){return 9;}
434  return -1;
435  }else{
436  if (ehp==0){return kFcsEcalMaxId;}
437  else if(ehp==1){return kFcsHcalMaxId;}
438  else if(ehp==2){return kFcsPresMaxId;}
439  return -1;
440  }
441 }
442 
443 int StFcsDb::getRowNumber(int det, int id) const{
444  if(id<0 || id>=maxId(det)) return -1;
445  return id/nColumn(det) + 1;
446 }
447 
448 int StFcsDb::getColumnNumber(int det, int id) const{
449  if(id<0 || id>=maxId(det)) return -1;
450  return id%nColumn(det) + 1;
451 }
452 
453 int StFcsDb::getId(int det, int row, int col) const{
454  if(row<=0 || row>nRow(det) || nRow(det)<0) return -1;
455  if(col<=0 || col>nColumn(det) || nRow(det)<0) return -1;
456  return col - 1 + nColumn(det)*(row-1);
457 }
458 
459 int StFcsDb::getDepCh(int dep, int ch) const{
460  return dep*kFcsMaxDepCh + ch;
461 }
462 
463 void StFcsDb::getName(int det, int id, char name[]) {
464  int ehp,ns,crt,slt,dep,ch;
465  int c=getColumnNumber(det,id);
466  int r=getRowNumber(det,id);
467  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
468  if(ehp<2){
469  int scehp,scns,scdep,br,i2c,sipm,pp,j;
470  getSCmap(det,id,scehp,scns,scdep,br,i2c,sipm,pp,j);
471  sprintf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_F%02d/%1d/%02d/%1d",
472  DET[det],id,r,c,dep,ch,scdep,br,i2c,sipm);
473  }else{
474  int pp,tt;
475  getEPDfromId(det,id,pp,tt);
476  sprintf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_PP%02d%1sTT%02d",
477  DET[det],id,r,c,dep,ch,pp,tt%2==0?"E":"O",tt);
478  }
479 }
480 
481 void StFcsDb::getName(int ehp, int ns, int dep, int ch, char name[]) {
482  int det,id,crt,slt;
483  getIdfromDep(ehp,ns,dep,ch,det,id,crt,slt);
484  if(id==-1){
485  det = detectorId(ehp, ns);
486  sprintf(name,"%2s---_r--c--_Dep%02dCh%02d_F--/-/--/-",
487  DET[det],dep,ch);
488  }else{
489  int c=getColumnNumber(det,id);
490  int r=getRowNumber(det,id);
491  if(ehp<2){
492  int scehp,scns,scdep,br,i2c,sipm,pp,j;
493  getSCmap(det,id,scehp,scns,scdep,br,i2c,sipm,pp,j);
494  sprintf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_F%02d/%1d/%02d/%1d",
495  DET[det],id,r,c,dep,ch,scdep,br,i2c,sipm);
496  }else{
497  int pp,tt;
498  getEPDfromId(det,id,pp,tt);
499  sprintf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_PP%02d%1s/TT%02d",
500  DET[det],id,r,c,dep,ch,pp,tt%2==0?"E":"O",tt);
501  }
502  }
503 }
504 
505 void StFcsDb::getFromName(const char name[], int &det, int &id){
506  char detname[5];
507  int r,c,dep,ch,scdep,br,i2c,sipm;
508  sscanf(name,"%2s%03d_r%02dc%02d_Dep%02dCh%02d_F%02d/%1d/%02d/%1d",
509  detname,&id,&r,&c,&dep,&ch,&scdep,&br,&i2c,&sipm);
510  det = getDetFromName(detname);
511  return;
512 }
513 
514 int StFcsDb::getDetFromName(const std::string& detname){
515  if ( detname=="EN") {return 0;}
516  else if( detname=="ES") {return 1;}
517  else if( detname=="HN") {return 2;}
518  else if( detname=="HS") {return 3;}
519  else if( detname=="PN") {return 4;}
520  else if( detname=="PS") {return 5;}
521  else {
522  LOG_ERROR << "ERROR:Invalid name for detector;Input:"<<detname<<endm;
523  return -1;
524  }
525 }
526 
527 unsigned short StFcsDb::getKey(unsigned short detid, unsigned short id){ return ( (detid & 0x7)<<12 | (id & 0xffff) ); }
528 void StFcsDb::getDetIdFromKey(unsigned short key, unsigned short& detid, unsigned short& id)
529 {
530  detid = (key >> 12) & 0x0007;
531  id = (key & 0x0fff);
532 }
533 unsigned short StFcsDb::getDetFromKey(unsigned short key){ return (key >> 12) & 0x0007; }
534 unsigned short StFcsDb::getIdFromKey(unsigned short key) { return (key & 0x0fff); }
535 
536 StThreeVectorD StFcsDb::getDetectorOffset(int det, double zdepth ) const{
537  if(mRun19>0){
538  const float bOffY=-(17.0*5.81); //40in=101.6cm and 17*5.81=98.76 so I will leave this unchanged
539  if(det==1) return StThreeVectorD( 25.25*2.54, bOffY + getYWidth(det)*nRow(det)/2.0, 710.16);
540  if(det==3) return StThreeVectorD( 27.50*2.54, bOffY + getYWidth(det)*nRow(det)/2.0, 782.63);
541  if(det==5){
542  //this is before June5
543  if(mRun<1105942) return StThreeVectorD( 14*2.54, bOffY + 2.875 + 2.54 + getYWidth(det)*nRow(det)/2.0, 700.00);
544  //this is afetr June5, moved up by 3.5inch for ecal cooling
545  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);
546  //after June19, added 4 inches due to cooling & mounted on STGC supoort structure
547  return StThreeVectorD( 14*2.54, bOffY + 2.875 + 2.54 + getYWidth(det)*nRow(det)/2.0 + 4.0*2.54, 700.00);
548  }
549  return StThreeVectorD(0.0, 0.0, 0.0);
550  }else{
551  double xoff = 0;
552  double zoff = 0;
553  if( zdepth>0 ){
554  double detangle = getDetectorAngle(det)*M_PI/180.0;
555  if( det%2==0 ){ detangle *= -1.0; } //North side use negative angle
556  xoff = zdepth*sin(detangle);
557  zoff = zdepth*cos(detangle);
558  }
559  if(mDbAccess==0){ //no DB
560  if(det==0) return StThreeVectorD(-17.399+xoff, -5.26, 710.16+zoff);
561  if(det==1) return StThreeVectorD( 17.399+xoff, -5.26, 710.16+zoff);
562  if(det==2) return StThreeVectorD(-21.285+xoff, +1.80, 782.63+zoff);
563  if(det==3) return StThreeVectorD( 21.285+xoff, +1.80, 782.63+zoff);
564  return StThreeVectorD(0.0, 0.0, 0.0);
565  }else{ //from DB
566  if(det>=0 && det<4)
567  return StThreeVectorD(mFcsDetectorPosition.xoff[det]+xoff,
568  mFcsDetectorPosition.yoff[det],
569  mFcsDetectorPosition.zoff[det]+zoff);
570  return StThreeVectorD(0.0, 0.0, 0.0);
571  }
572  }
573 }
574 
575 float StFcsDb::getDetectorAngle(int det) const{
576  if(det==0) return 1.73;
577  if(det==1) return 1.73;
578  if(det==2) return 1.73;
579  if(det==3) return 1.73;
580  if(det==4) return 0.0;
581  if(det==5) return 0.0;
582  return 0.0;
583 }
584 
585 float StFcsDb::getXWidth(int det) const{
586  if(det==0) return 5.542+0.03;
587  if(det==1) return 5.542+0.03;
588  if(det==2) return 9.99+0.00;
589  if(det==3) return 9.99+0.00;
590  return 0.0;
591 }
592 
593 float StFcsDb::getYWidth(int det) const{
594  return getXWidth(det);
595 }
596 
597 float StFcsDb::getZDepth(int det) const{
598  if(det==0 || det==1) {return 30.97;} //66*(0.4+0.01+0.01)+(66-1)*0.05
599  if(det==2 || det==3) {return 84.24;} //36*2.34
600  else {return 1.0;}
601 }
602 
603 float StFcsDb::getShowerMaxZ(int det) const{
604  if(det==0 || det==1) return 15.0;
605  if(det==2 || det==3) return 25.0;
606  return 0.0;
607 }
608 
610 void StFcsDb::getLocalXYinCell(StFcsHit* hit, float &x, float &y) const{
611  getLocalXYinCell(hit->detectorId(),hit->id(),x,y);
612 }
613 
614 void StFcsDb::getLocalXYinCell(int det, int id, float &x, float &y) const{
615  getLocalXYinCell(det,getColumnNumber(det,id),getRowNumber(det,id),x,y);
616 }
617 
618 void StFcsDb::getLocalXYinCell(int det, int col, int row, float &x, float &y) const{
619  if(mLeakyHcal==1 && (det==kFcsHcalNorthDetId || det==kFcsHcalSouthDetId)){
620  if(col==1){
621  x=float(col)-0.4;
622  }else if(col==kFcsHcalNCol){
623  x=float(col)-0.6;
624  }else{
625  x=float(col);
626  }
627  }else{
628  x=float(col)-0.5;
629  }
630  y=float(row)-0.5;
631 }
632 
636 StThreeVectorD StFcsDb::getStarXYZ_4x4(int det,int col, int row) const{
637  int c1=0, c2=0, r1=0, r2=0;
638  if(det<=kFcsEcalSouthDetId){
639  c1=(col-1)*2 + 2;
640  c2=(col-1)*2 + 5;
641  r1=(row-1)*2 + 2;
642  r2=(row-1)*2 + 5;
643  }else if(det<=kFcsHcalSouthDetId){
644  c1=(col-1)*2 + 1;
645  c2=(col-1)*2 + 4;
646  r1=(row-1)*2 + 1;
647  r2=(row-1)*2 + 4;
648  }
649  return (getStarXYZ(det,c1,r1)+getStarXYZ(det,c2,r2))/2.0;
650 }
651 
653 double StFcsDb::getHcalProjectedToEcalX(int ns, double hcalLocalX, double zvtx){
654  if(zvtx==0.0){
655  StThreeVectorD ecalfront = getDetectorOffset(ns);
656  StThreeVectorD hcalfront = getDetectorOffset(ns+2);
657  double ecalD = sqrt(ecalfront.x()*ecalfront.x() + ecalfront.z()*ecalfront.z());
658  double hcalD = sqrt(hcalfront.x()*hcalfront.x() + ecalfront.z()*hcalfront.z());
659  double ecalSMD = ecalD + getShowerMaxZ(ns);
660  double hcalSMD = hcalD + getShowerMaxZ(ns+2);
661  double hcalLocalcm = hcalLocalX * getXWidth(ns+2); //convert to [cm]
662  return hcalLocalcm*ecalSMD/hcalSMD;
663  }else{
664  //to be impremented
665  LOG_INFO << "Need to be implememted!!!"<<endm;
666  return 1000;
667  }
668 };
669 
671 double StFcsDb::getHcalProjectedToEcalY(int ns, double hcalLocalY, double zvtx){
672  StThreeVectorD ecalfront = getDetectorOffset(ns);
673  StThreeVectorD hcalfront = getDetectorOffset(ns+2);
674  double ecalSMD = ecalfront.z() + getShowerMaxZ(ns) - zvtx;
675  double hcalSMD = hcalfront.z() + getShowerMaxZ(ns+2) - zvtx;
676  double hcalLocalcm = hcalLocalY * getYWidth(ns+2); //convert to [cm]
677  double hcalStar = double(nRow(ns+2))/2.0*getYWidth(ns+2)+hcalfront.y()-hcalLocalcm; //STAR Y
678  double hcalatEcal = hcalStar*ecalSMD/hcalSMD; //project assuming vtx_y=0
679  return double(nRow(ns))/2.0*getYWidth(ns) + ecalfront.y() - hcalatEcal; //put in Ecal local
680 };
681 
683 double StFcsDb::getProjectedDistance(StFcsCluster* ecal,StFcsCluster* hcal, double zvtx){
684  int eNS = northSouth(ecal->detectorId());
685  int hNS = northSouth(hcal->detectorId());
686  if(eNS!=hNS) return 1000;
687  double hX = getHcalProjectedToEcalX(hNS,hcal->x(),zvtx);
688  double hY = getHcalProjectedToEcalY(hNS,hcal->y(),zvtx);
689  double eX = ecal->x() * getXWidth(eNS);
690  double eY = ecal->y() * getYWidth(eNS);
691  double dX = eX-hX;
692  double dY = eY-hY;
693  return sqrt(dX*dX + dY*dY);
694 };
695 
697 double StFcsDb::getProjectedDistance(StFcsPoint* ecal, StFcsCluster* hcal, double zvtx){
698  int eNS = northSouth(ecal->detectorId());
699  int hNS = northSouth(hcal->detectorId());
700  if(eNS!=hNS) return 1000;
701  double hX = getHcalProjectedToEcalX(hNS,hcal->x(),zvtx);
702  double hY = getHcalProjectedToEcalY(hNS,hcal->y(),zvtx);
703  double eX = ecal->x() * getXWidth(eNS);
704  double eY = ecal->y() * getYWidth(eNS);
705  double dX = eX-hX;
706  double dY = eY-hY;
707  return sqrt(dX*dX + dY*dY);
708 };
709 
710 StThreeVectorD StFcsDb::getNormal(int det) const
711 {
712  double detangle = getDetectorAngle(det)*M_PI/180.0;
713  if( det%2==0 ){ detangle *= -1.0; } //North side use negative angle
714  return StThreeVectorD( sin(detangle), 0 ,cos(detangle) );
715  double planenormal[3] = {sin(detangle),0,cos(detangle)};
716 }
717 
718 StThreeVectorD StFcsDb::projectTrackToEcal(const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert) const
719 {
720  int det=0; // North side for negative px
721  // South side for positive px, since px==0 does not hit detector choose south side for that case
722  if( g2ttrk->p[2]>=0 && g2ttrk->p[0]>=0 ){ det=1; }
723  if( g2ttrk->p[2]<0 && g2ttrk->p[0]<0 ){ det=1; }
724  return projectTrack(det,g2ttrk,g2tvert,0);
725 }
726 
727 StThreeVectorD StFcsDb::projectTrackToHcal(const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert) const
728 {
729  int det = 2; // North side for negative px
730  // South side for positive px, since px==0 does not hit detector choose south side for that case
731  if( g2ttrk->p[2]>=0 && g2ttrk->p[0]>=0 ){ det=3; }
732  if( g2ttrk->p[2]<0 && g2ttrk->p[0]<0 ){ det=3; }
733  return projectTrack(det,g2ttrk,g2tvert,0);
734 }
735 
736 StThreeVectorD StFcsDb::projectTrackToEcalSMax(const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert) const
737 {
738  int det=0; // North side for negative px
739  // South side for positive px, since px==0 does not hit detector choose south side for that case
740  if( g2ttrk->p[2]>=0 && g2ttrk->p[0]>=0 ){ det=1; }
741  if( g2ttrk->p[2]<0 && g2ttrk->p[0]<0 ){ det=1; }
742  return projectTrack(det,g2ttrk,g2tvert,-1);
743 }
744 
745 StThreeVectorD StFcsDb::projectTrackToHcalSMax(const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert) const
746 {
747  int det = 2; // North side for negative px
748  // South side for positive px, since px==0 does not hit detector choose south side for that case
749  if( g2ttrk->p[2]>=0 && g2ttrk->p[0]>=0 ){ det=3; }
750  if( g2ttrk->p[2]<0 && g2ttrk->p[0]<0 ){ det=3; }
751  return projectTrack(det,g2ttrk,g2tvert,-1);
752 }
753 
754 StThreeVectorD StFcsDb::projectTrack(int det, const g2t_track_st* g2ttrk, const g2t_vertex_st* g2tvert, double showermaxz) const
755 {
756  double linedir[3] = {g2ttrk->p[0],g2ttrk->p[1],g2ttrk->p[2]};
757  if( g2tvert!=0 ){
758  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
759  double linestart[3] = {g2tvert[vertind].ge_x[0],g2tvert[vertind].ge_x[1],g2tvert[vertind].ge_x[2]};
760  if( vertind >= 0 ){//Since start index==0 means no start then vertind<0 will default to using origin
761  return projectLine(det, linedir, linestart, showermaxz);
762  }
763  }
764  double zero[3] = {0,0,0};
765  return projectLine(det,linedir,zero,showermaxz);
766 }
767 
768 StThreeVectorD StFcsDb::projectLine(int det, StThreeVectorD &linedirection, StThreeVectorD &lineorigin, double showermaxz) const
769 {
770  double linedir[3] = {linedirection.x(),linedirection.y(),linedirection.z()};
771  double linestart[3] = {lineorigin.x(),lineorigin.y(),lineorigin.z()};
772  return projectLine(det,linedir,linestart,showermaxz);
773 }
774 
775 StThreeVectorD StFcsDb::projectLine(int det, double* linedirection, double* lineorigin, double showermaxz) const
776 {
777  if( showermaxz<0 ){ showermaxz = getShowerMaxZ(det); } //when negative use default showermax
778  if( det%2==0 ){ //North side is negative x-axis
779  if( linedirection[2]>=0 && linedirection[0]>=0 ){ LOG_WARN << "Incorrect Det" << endm; }
780  if( linedirection[2]<0 && linedirection[0]<=0 ){ LOG_WARN << "Incorrect Det" << endm; }
781  }
782  else{ //South side is positive x-axis
783  if( linedirection[2]>=0 && linedirection[0]<0 ){ LOG_WARN << "Incorrect Det" << endm; }
784  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
785  }
786  double detangle = getDetectorAngle(det)*M_PI/180.0;
787  if( det%2==0 ){ detangle *= -1.0; } //North side use negative angle
788  StThreeVectorD xyzoff = getDetectorOffset(det,showermaxz);
789  StThreeVectorD planenormal = getNormal(det);
790  //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.
791  double tintersection =
792  (planenormal.x()*(xyzoff.x()-lineorigin[0])+planenormal.y()*(xyzoff.y()-lineorigin[1])+planenormal.z()*(xyzoff.z()-lineorigin[2])) /
793  (planenormal.x()*linedirection[0]+planenormal.y()*linedirection[1]+planenormal.z()*linedirection[2]);
794 
795  return StThreeVectorD( linedirection[0]*tintersection+lineorigin[0], linedirection[1]*tintersection+lineorigin[1], linedirection[2]*tintersection+lineorigin[2] );
796 }
797 
799 StThreeVectorD StFcsDb::getStarXYZ(const StFcsHit* hit, float FcsZ) const{
800  return getStarXYZ(hit->detectorId(),hit->id(),FcsZ);
801 }
802 
804 StThreeVectorD StFcsDb::getStarXYZ(const StFcsCluster* clu, float FcsZ) const{
805  return getStarXYZfromColumnRow(clu->detectorId(),clu->x(),clu->y(),FcsZ);
806 }
807 
809 StThreeVectorD StFcsDb::getStarXYZ(int det, int id, float FcsZ) const{
810  return getStarXYZ(det,getColumnNumber(det,id),getRowNumber(det,id),FcsZ);
811 }
812 
814 StThreeVectorD StFcsDb::getStarXYZ(int det, int col, int row, float FcsZ) const{
815  float x,y;
816  getLocalXYinCell(det,col,row,x,y);
817  return getStarXYZfromColumnRow(det,x,y,FcsZ);
818 }
819 
821 StThreeVectorD StFcsDb::getStarXYZfromColumnRow(int det, float col, float row, float FcsZ) const{
822  return getStarXYZ(det,col*getXWidth(det),row*getYWidth(det),FcsZ);
823 }
824 
826 StThreeVectorD StFcsDb::getStarXYZ(int det, float FcsX, float FcsY, float FcsZ, float zVertex) const{
827  if(FcsZ<0.0) FcsZ = getShowerMaxZ(det);
828  double x = 0.0, y=0.0, z=0.0;
830  double a = getDetectorAngle(det) / 180.0 * M_PI;
831  y = off.y() + (double(nRow(det)) / 2.0 * getYWidth(det)) - FcsY;
832  if(northSouth(det) == 0) {
833  x = off.x() - FcsX * cos(a) - FcsZ * sin(a);
834  z = off.z() + FcsZ * cos(a) - FcsX * sin(a);
835  }else{ // south side
836  x = off.x() + FcsX * cos(a) + FcsZ * sin(a);
837  z = off.z() + FcsZ * cos(a) - FcsX * sin(a);
838  }
839  z -= zVertex;
840  if(mDebug>1) LOG_DEBUG << Form("getStarXYZ XOFF=%f YOFF=%f ZOFF=%f Angle=%f : x=%f y=%f z=%f",
841  off.x(),off.y(),off.z(),a,x,y,z) << endm;
842  return StThreeVectorD(x,y,z);
843 }
844 
845 float StFcsDb::getPhi(int det,float FcsX, float FcsY, float FcsZ) const{
846  return (getStarXYZ(det,FcsX,FcsY,FcsZ)).phi();
847 }
848 
849 float StFcsDb::getEta(int det,float FcsX, float FcsY, float FcsZ, float zVertex) const{
850  return (getStarXYZ(det,FcsX,FcsY,FcsZ,zVertex)).pseudoRapidity();
851 }
852 
853 StLorentzVectorD StFcsDb::getLorentzVector(const StThreeVectorD& xyz, float energy, float zVertex){
854  // Calculate a 4 momentum from a direction/momentum vector and energy assuming zero mass i.e. E=p
855  // Taking into account beamline offsets and angles from DB
856  StThreeVectorD xyznew(xyz.x()-mVx,xyz.y()-mVy,xyz.z()-zVertex);
857  xyznew.rotateX(+mThetaY);
858  xyznew.rotateY(-mThetaX);
859  double e=energy;
860  StThreeVectorD mom3 = xyznew.unit() * e;
861  if(mDebug>1){
862  LOG_DEBUG << Form("mVx=%8.4f mVdxdz=%8.4f mThetaX=%8.4f",mVx,mVdxdz,mThetaX) << endm;
863  LOG_DEBUG << Form("mVy=%8.4f mVdydz=%8.4f mThetaY=%8.4f",mVy,mVdydz,mThetaY) << endm;
864  LOG_DEBUG << Form("xyz = %lf %lf %lf",xyz.x(), xyz.y(), xyz.z()) << endm;
865  LOG_DEBUG << Form("xyz rot = %lf %lf %lf",xyznew.x(), xyznew.y(), xyznew.z()) << endm;
866  LOG_DEBUG << Form("p = %lf %lf %lf %lf",mom3.x(), mom3.y(), mom3.z(),e) << endm;
867  }
868  return StLorentzVectorD(mom3, e);
869 }
870 
871 float StFcsDb::getSamplingFraction(int det) const{
872  if(det==0 || det==1) return 0.2;
873  if(det==2 || det==3) return 0.0145;
874  if(det==4 || det==5) return 2.0; //2MeV for MIP
875  return 0.0;
876 }
877 
878 int StFcsDb::getZeroSuppression(int det) const {
879  return 1;
880 }
881 
883  return getGain(hit->detectorId(), hit->id());
884 }
885 
886 float StFcsDb::getGain(int det, int id) const {
887  if(det>=0 && det<kFcsNDet && id>=0 && id<maxId(det)) {
888  switch(mGainMode){
889  case GAINMODE::FIXED :
890  if(det<=kFcsHcalSouthDetId) return 0.0053; //default 5.3MeV/ch
891  return 1.0/100.0; //100ch for MIP for Pres
892  case GAINMODE::FORCED :
893  if(det<=kFcsEcalSouthDetId) return mForceUniformGainEcal;
894  if(det<=kFcsHcalSouthDetId) return mForceUniformGainHcal;
895  if(det<=kFcsPresSouthDetId) return mForceUniformGainPres;
896  case GAINMODE::DB :
897  default:
898  int ehp,ns,dep,ch,crt,slt;
899  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
900  return mGain[ehp][ns][dep][ch];
901  }
902  }
903  return 1.0;
904 }
905 
907  return getGainCorrection(hit->detectorId(), hit->id());
908 }
909 
910 float StFcsDb::getGainCorrection(int det, int id) const {
911  if(det>=0 && det<kFcsNDet && id>=0 && id<maxId(det)) {
912  switch(mGainCorrMode){
913  case GAINMODE::FIXED :
914  if(det<=kFcsHcalSouthDetId) return 1.0; // default 1.0
915  return 0.5; // 1/2 MIP for Pres
916  case GAINMODE::FORCED :
917  if(det<=kFcsEcalSouthDetId) return mForceUniformGainCorrectionEcal;
918  if(det<=kFcsHcalSouthDetId) return mForceUniformGainCorrectionHcal;
919  if(det<=kFcsPresSouthDetId) return mForceUniformGainCorrectionPres;
920  case GAINMODE::DB :
921  default:
922  int ehp,ns,dep,ch,crt,slt;
923  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
924  return mGainCorr[ehp][ns][dep][ch];
925  }
926  }
927  return 1.0;
928 }
929 
931  return getGainCorrection(hit->detectorId(), hit->id());
932 }
933 
934 float StFcsDb::getPresValley(int det, int id) const {
935  return getGainCorrection(det,id);
936 }
937 
939  return getGain8(hit->detectorId(), hit->id());
940 }
941 
942 float StFcsDb::getGain8(int det, int id) const {
943  return getGain(det,id)/1.21;
944 }
945 
946 void StFcsDb::getDepfromId(int detectorId, int id, int &ehp, int &ns, int &crt, int &slt, int &dep, int &ch) const{
947  ehp=-1; ns=-1; crt=-1; slt=-1; dep=-1; ch=-1;
948  if(detectorId<0 || detectorId>=kFcsNDet) return;
949  if(id<0 || id>=kFcsMaxId) return;
950  ehp = mMap_ehp[detectorId][id];
951  ns = mMap_ns [detectorId][id];
952  crt = mMap_crt[detectorId][id];
953  slt = mMap_slt[detectorId][id];
954  dep = mMap_dep[detectorId][id];
955  ch = mMap_ch [detectorId][id];
956  return;
957 }
958 
959 void StFcsDb::getIdfromDep(int ehp, int ns, int dep, int ch, int &detectorId, int &id, int &crt, int &slt) const{
960  detectorId=6; id=4095; crt=0; slt=0;
961  if(ehp<0 || ehp>=kFcsEHP) return;
962  if(ns<0 || ns>=kFcsNorthSouth) return;
963  if(dep<0 || dep>=kFcsMaxDepBd) return;
964  if(ch<0 || ch>=kFcsMaxDepCh) return;
965  detectorId = mRMap_det[ehp][ns][dep][ch];
966  id = mRMap_id [ehp][ns][dep][ch];
967  crt = mRMap_crt[ehp][ns][dep][ch];
968  slt = mRMap_slt[ehp][ns][dep][ch];
969  return;
970 }
971 
972 int StFcsDb::getNDep(int ehp, int ns) const{
973  if(mRun19>0){
974  if(ns==0) return 0;
975  switch(ehp){
976  case 0: return 2;
977  case 1: return 1;
978  case 2: return 1;
979  case 3: return 1;
980  default: return 0;
981  }
982  }else{
983  switch(ehp){
984  case 0: return 24;
985  case 1: return 9;
986  case 2: return 6;
987  case 3: return 3;
988  default: return 0;
989  }
990  }
991 }
992 
993 void StFcsDb::getSCmap(int det, int id,
994  int &ehp, int &ns, int &scdep, int &branch, int &fee_i2c, int &sipm,
995  int &pp, int &jacket) const{
996  ehp=-1; ns=-1; scdep=-1; branch=-1; fee_i2c=-1; sipm=-1, pp=-1; jacket=-1;
997  if(det<0 || det>=kFcsNDet) return;
998  if(id<0 || id>=kFcsMaxId) return;
999  ehp=mScMap_ehp[det][id];
1000  ns=mScMap_ns[det][id];
1001  scdep=mScMap_dep[det][id];
1002  branch=mScMap_bra[det][id];
1003  fee_i2c= mScMap_add[det][id];
1004  sipm=mScMap_sipm[det][id];
1005  pp=mScMap_pp[det][id];
1006  jacket=mScMap_j[det][id];
1007  return;
1008 }
1009 
1010 void StFcsDb::getIdfromSCmap(int ehp, int ns, int scdep, int branch, int fee_i2c, int sipm,
1011  int &det, int &id)const{
1012  det=-1;
1013  id=-1;
1014  if(ehp<0 || det>=kFcsEHP) return;
1015  if(ns<0 || ns>=kFcsNorthSouth) return;
1016  if(scdep<0 || scdep>=kFcsMaxDepBd) return;
1017  if(branch<0 || branch>=kFcsMaxBranch) return;
1018  if(fee_i2c<0 || fee_i2c>=kFcsMaxAddr) return;
1019  if(sipm<0 || sipm>=kFcsMaxSiPM) return;
1020  det=mRScMap_det[ehp][ns][scdep][branch][fee_i2c][sipm];
1021  id =mRScMap_id [ehp][ns][scdep][branch][fee_i2c][sipm];
1022  return;
1023 }
1024 
1025 int StFcsDb::jacketColor(int ehp, int ns, int dep, int ch){
1026  switch(ehp){
1027  case 0:
1028  if(dep<=19) return dep%5;
1029  switch(dep){
1030  case 20: case 21:
1031  if(ch<4) return 7;
1032  return ch/4;
1033  case 22: return 6;
1034  case 23: return 5;
1035  default: return -1;
1036  }
1037  case 1:
1038  switch(dep){
1039  case 0: case 3: return 0 + ns*4;
1040  case 1: case 4: return 1 + ns*4;
1041  case 2: case 5: return 2 + ns*4;
1042  case 6: case 7:
1043  if(ch<8 ) return 0 + ns*4;
1044  if(ch<16) return 1 + ns*4;
1045  if(ch<24) return 2 + ns*4;
1046  return -1;
1047  case 8: return 3 + ns*4;
1048  default: return -1;
1049  }
1050  default:
1051  return -1;
1052  }
1053 }
1054 
1055 
1056 
1058  for(int b=1; b<=8; b++){
1059  for(int p=2; p<=7; p++){
1060  short dep = EPPMap[b-1][p-2][0];
1061  short lh = EPPMap[b-1][p-2][1];
1062  short scr = EPPMap[b-1][p-2][2];
1063  if(dep>=0){
1064  EMapPPB[dep][lh]=b;
1065  EMapPPP[dep][lh]=p;
1066  if(scr>0){
1067  EMapSCDEP[scr-1]=dep;
1068  EMapSCBRA[scr-1]=lh;
1069  EMapSCPP[scr-1]=b;
1070  EMapSCJ[scr-1]=p/2;
1071  }
1072  }
1073  }
1074  }
1075  for(int b=1; b<=4; b++){
1076  for(int p=2; p<=7; p++){
1077  short dep = HPPMap[b-1][p-2][0];
1078  short lh = HPPMap[b-1][p-2][1];
1079  short scr = HPPMap[b-1][p-2][2];
1080  if(dep>=0){
1081  HMapPPB[dep][lh]=b;
1082  HMapPPP[dep][lh]=p;
1083  if(scr>0){
1084  HMapSCDEP[scr-1]=dep;
1085  HMapSCBRA[scr-1]=lh;
1086  HMapSCPP[scr-1]=b;
1087  HMapSCJ[scr-1]=p/2;
1088  }
1089  }
1090  }
1091  }
1092 }
1093 
1095  makePPMap();
1096  int ehp,crt,slt,dep,ch,det,id;
1097  for(int det=0; det<kFcsNDet; det++){
1098  for(int id=0; id<kFcsMaxId; id++){
1099  mMap_ehp[det][id]=-1;
1100  mMap_ch[det][id]=-1;
1101  mMap_ppb[det][id]=-1;
1102  mMap_ppp[det][id]=-1;
1103  mMap_pph[det][id]=-1;
1104  mMap_wcol[det][id]=-1;
1105  mMap_jcol[det][id]=-1;
1106  mScMap_ehp[det][id]=-1;
1107  mScMap_pp[det][id]=-1;
1108  mScMap_j[det][id]=-1;
1109  }
1110  }
1111  for(int ehp=0; ehp<kFcsEHP; ehp++){
1112  for(int ns=0; ns<kFcsNorthSouth; ns++){
1113  for(int dep=0; dep<kFcsMaxDepBd; dep++){
1114  for(int ch=0; ch<kFcsMaxDepCh; ch++){
1115  mRMap_det[ehp][ns][dep][ch]=6;
1116  mRMap_id[ehp][ns][dep][ch]=-1;
1117  }
1118  for(int bra=0; bra<kFcsMaxBranch; bra++){
1119  for(int add=0; add<kFcsMaxAddr; add++){
1120  for(int sipm=0; sipm<kFcsMaxSiPM; sipm++){
1121  mRScMap_det[ehp][ns][dep][bra][add][sipm]=-1;
1122  mRScMap_id [ehp][ns][dep][bra][add][sipm]=-1;
1123  }
1124  }
1125  }
1126  }
1127  }
1128  }
1129 
1130  //Ecal
1131  ehp=0;
1132  for(int ns=0; ns<2; ns++){
1133  id=0;
1134  det = ns;
1135  for(int row=1; row<=kFcsEcalNRow; row++){
1136  for(int col=1; col<=kFcsEcalNCol; col++){
1137  if (row== 1){crt=1+ns*2; slt=16; dep=20; ch=col-1;} //top row (ch0-21)
1138  else if(row==34){crt=1+ns*2; slt=17; dep=21; ch=col-1;} //bottom row (ch0-21)
1139  else if(col== 1){crt=1+ns*2; slt=18; dep=22; ch=row-2;} //near side column (ch0-31)
1140  else if(col==22){crt=1+ns*2; slt=19; dep=23; ch=row-2;} //far side column (ch0-31)
1141  else{
1142  crt=ns*4;
1143  dep=(col-2)/4 + (row-2)/8*5;
1144  slt=dep;
1145  ch =(col-2)%4 + ((row-2)%8)*4;
1146  }
1147  mMap_ehp[det][id] = ehp;
1148  mMap_ns [det][id] = ns;
1149  mMap_crt[det][id] = crt;
1150  mMap_slt[det][id] = slt;
1151  mMap_dep[det][id] = dep;
1152  mMap_ch [det][id] =ch ;
1153  mRMap_det[ehp][ns][dep][ch] = det;
1154  mRMap_id [ehp][ns][dep][ch] = id ;
1155  mRMap_crt[ehp][ns][dep][ch] = crt;
1156  mRMap_slt[ehp][ns][dep][ch] = slt;
1157 
1158  //cable
1159  int lh= ch/16;
1160  int b = EMapPPB[dep][lh];
1161  int p = EMapPPP[dep][lh];
1162  int h = (ch%16)/4 + (p-2)*4 + 1;
1163  int w = ch%4;
1164  int j = jacketColor(ehp,ns,dep,ch);
1165  mMap_ppb[det][id] = b;
1166  mMap_ppp[det][id] = p;
1167  mMap_pph[det][id] = h;
1168  mMap_wcol[det][id] = w;
1169  mMap_jcol[det][id] = j;
1170 
1171  //SC map
1172  int scr=(row-1)/2;
1173  unsigned short scdep=EMapSCDEP[scr];
1174  unsigned short bra=EMapSCBRA[scr];
1175  unsigned short pp=EMapSCPP[scr];
1176  unsigned short pj=EMapSCJ[scr];;
1177  unsigned short add=(col-1)/2;
1178  unsigned short sipm;
1179  if(ns==0){
1180  sipm = (col-1)%2 + ((row-1)%2)*2;
1181  }else{
1182  sipm = col%2 + ((row-1)%2)*2;
1183  }
1184  mScMap_ehp[det][id] = ehp;
1185  mScMap_ns[det][id] = ns;
1186  mScMap_dep[det][id] = scdep;
1187  mScMap_bra[det][id] = bra;
1188  mScMap_add[det][id] = add;
1189  mScMap_sipm[det][id] = sipm;
1190  mScMap_pp[det][id] = pp;
1191  mScMap_j[det][id] = pj;
1192  mRScMap_det[ehp][ns][scdep][bra][add][sipm]=det;
1193  mRScMap_id[ehp][ns][scdep][bra][add][sipm]=id;
1194 
1195  id++;
1196  }
1197  }
1198  }
1199 
1200  //Hcal
1201  ehp=1;
1202  for(int ns=0; ns<2; ns++){
1203  id=0;
1204  det = ns + 2;
1205  crt = 1+ns*2;
1206  for(int row=1; row<=kFcsHcalNRow; row++){
1207  for(int col=1; col<=kFcsHcalNCol; col++){
1208  if (col==13){dep=8; ch=row-1;} //far side column (ch0-19)
1209  else if(row== 1){dep=6; ch=(col-1)%4 + ((col-1)/4)*8 ;} //top row (ch0,1,2,3, 8, 9...19)
1210  else if(row== 2){dep=6; ch=(col-1)%4 + ((col-1)/4)*8 + 4;} //2nd row (ch4,5,6,7,12,13...23)
1211  else if(row==19){dep=7; ch=(col-1)%4 + ((col-1)/4)*8 ;} //2nd bottom row
1212  else if(row==20){dep=7; ch=(col-1)%4 + ((col-1)/4)*8 + 4;} //bottom row
1213  else{
1214  dep= (col-1)/4 + ((row-3)/8)*3;
1215  ch = (col-1)%4 + ((row-3)%8)*4;
1216  }
1217  slt=dep;
1218  mMap_ehp[det][id] = ehp;
1219  mMap_ns [det][id] = ns;
1220  mMap_crt[det][id] = crt;
1221  mMap_slt[det][id] = slt;
1222  mMap_dep[det][id] = dep;
1223  mMap_ch[det][id] =ch ;
1224  mRMap_det[ehp][ns][dep][ch] = det;
1225  mRMap_id [ehp][ns][dep][ch] = id ;
1226  mRMap_crt[ehp][ns][dep][ch] = crt;
1227  mRMap_slt[ehp][ns][dep][ch] = slt;
1228 
1229  //cable
1230  int lh= ch/16;
1231  int b = HMapPPB[dep][lh];
1232  int p = HMapPPP[dep][lh];
1233  int h = (ch%16)/4 + (p-2)*4 + 1;
1234  int w = ch%4;
1235  int j = jacketColor(ehp,ns,dep,ch);
1236  mMap_ppb[det][id] = b;
1237  mMap_ppp[det][id] = p;
1238  mMap_pph[det][id] = h;
1239  mMap_wcol[det][id] = w;
1240  mMap_jcol[det][id] = j;
1241 
1242  //SC map
1243  unsigned short feerow = (row-1)/2;
1244  unsigned short scdep=HMapSCDEP[feerow];
1245  unsigned short bra=HMapSCBRA[feerow];
1246  unsigned short pp=HMapSCPP[feerow];
1247  unsigned short pj=HMapSCJ[feerow];
1248  unsigned short add=col-1;
1249  unsigned short sipm;
1250  if(ns==0){
1251  sipm = (row-1)%2;
1252  }else{
1253  sipm = row%2;
1254  }
1255  mScMap_ehp[det][id] = ehp;
1256  mScMap_ns[det][id] = ns;
1257  mScMap_dep[det][id] = scdep;
1258  mScMap_bra[det][id] = bra;
1259  mScMap_add[det][id] = add;
1260  mScMap_pp[det][id] = pp;
1261  mScMap_j[det][id] = pj;
1262  mScMap_sipm[det][id] = sipm;
1263  mRScMap_det[ehp][ns][scdep][bra][add][sipm]=det;
1264  mRScMap_id[ehp][ns][scdep][bra][add][sipm]=id;
1265  id++;
1266  }
1267  }
1268  }
1269 
1270  //EPD west as PRES
1271  // EPD PP= 1 ~ 6 ==> ns=0 (north) and dep=0 (near top) to 5 (near bottom)
1272  // EPD PP= 12 ~ 7 ==> ns=1 (south) and dep=0 (near top) to 5 (near bottom), odd/even reversed
1273  ehp=2;
1274  for(int ns=0; ns<2; ns++){
1275  det = ns + 4;
1276  crt = 1+ns*2;
1277  for(int dep=0; dep<6; dep++){
1278  for(int ch=0; ch<32; ch++){
1279  slt=dep+10;
1280  id=dep*32+ch;
1281  mMap_ehp[det][id] = ehp;
1282  mMap_ns [det][id] = ns;
1283  mMap_crt[det][id] = crt;
1284  mMap_slt[det][id] = slt;
1285  mMap_dep[det][id] = dep;
1286  mMap_ch[det][id] = ch ;
1287  mRMap_det[ehp][ns][dep][ch] = det;
1288  mRMap_id [ehp][ns][dep][ch] = id ;
1289  mRMap_crt[ehp][ns][dep][ch] = crt;
1290  mRMap_slt[ehp][ns][dep][ch] = slt;
1291  }
1292  }
1293  }
1294 }
1295 
1297  int ehp,crt,slt,dep,ch,det,id,ns;
1298  crt=1;
1299  slt=0;
1300  ns=1;
1301 
1302  for(int det=0; det<kFcsNDet; det++){
1303  for(int id=0; id<kFcsMaxId; id++){
1304  mMap_ehp[det][id]=-1;
1305  mMap_ns[det][id]=-1;
1306  mMap_crt[det][id]=-1;
1307  mMap_slt[det][id]=-1;
1308  mMap_dep[det][id]=-1;
1309  mMap_ch[det][id]=-1;
1310  }
1311  }
1312  for(int ehp=0; ehp<kFcsEHP; ehp++){
1313  for(int ns=0; ns<kFcsNorthSouth; ns++){
1314  for(int dep=0; dep<kFcsMaxDepBd; dep++){
1315  for(int ch=0; ch<kFcsMaxDepCh; ch++){
1316  mRMap_det[ehp][ns][dep][ch]=6;
1317  mRMap_id[ehp][ns][dep][ch]=-1;
1318  mRMap_crt[ehp][ns][dep][ch]=-1;
1319  mRMap_slt[ehp][ns][dep][ch]=-1;
1320  }
1321  }
1322  }
1323  }
1324 
1325  //Ecal
1326  ehp=0;
1327  id=0;
1328  det = ns;
1329  dep = 0;
1330  int c[2] = {0,0};
1331  for(int row=1; row<=8; row++){
1332  for(int col=1; col<=8; col++){
1333  if(col<=4) {dep=0;}
1334  else {dep=1;}
1335  mMap_ehp[det][id] = ehp;
1336  mMap_ns [det][id] = ns;
1337  mMap_crt[det][id] = crt;
1338  mMap_slt[det][id] = slt;
1339  mMap_dep[det][id] = dep;
1340  mMap_ch [det][id] = c[dep] ;
1341 
1342  mRMap_det[ehp][ns][dep][c[dep]] = det;
1343  mRMap_id [ehp][ns][dep][c[dep]] = id ;
1344  mRMap_crt[ehp][ns][dep][c[dep]] = crt;
1345  mRMap_slt[ehp][ns][dep][c[dep]] = slt;
1346  id++;
1347  c[dep]++;
1348  }
1349  }
1350 
1351  //Hcal
1352  ehp=1;
1353  id=0;
1354  det = ns + 2;
1355  dep = 0; //number for 2019/5/15 with OLD DEP16 before we install new DEP32
1356  ch = 0;
1357  for(int row=1; row<=4; row++){
1358  for(int col=1; col<=4; col++){
1359  mMap_ehp[det][id] = ehp;
1360  mMap_ns [det][id] = ns;
1361  mMap_crt[det][id] = crt;
1362  mMap_slt[det][id] = slt;
1363  mMap_dep[det][id] = dep;
1364  mMap_ch[det][id] =ch ;
1365  mRMap_det[ehp][ns][dep][ch] = det;
1366  mRMap_id [ehp][ns][dep][ch] = id ;
1367  mRMap_crt[ehp][ns][dep][ch] = crt;
1368  mRMap_slt[ehp][ns][dep][ch] = slt;
1369  id++;
1370  ch++;
1371  }
1372  }
1373 
1374  //Pres
1375  ehp=2;
1376  id=0;
1377  det = ns + 4;
1378  dep = 0;
1379  ch = 8;
1380  for(int id=0; id<9; id++){
1381  mMap_ehp[det][id] = ehp;
1382  mMap_ns [det][id] = ns;
1383  mMap_crt[det][id] = crt;
1384  mMap_slt[det][id] = slt;
1385  mMap_dep[det][id] = dep;
1386  mMap_ch[det][id] = ch ;
1387  mRMap_det[ehp][ns][dep][ch] = det;
1388  mRMap_id [ehp][ns][dep][ch] = id ;
1389  mRMap_crt[ehp][ns][dep][ch] = crt;
1390  mRMap_slt[ehp][ns][dep][ch] = slt;
1391  ch--;
1392  }
1393 }
1394 
1395 void StFcsDb::getIdfromEPD(int pp, int tt, int& det, int &id){
1396  det=-1;
1397  id=-1;
1398  int row=0,col=0;
1399  if(tt<0 || tt>=32) return;
1400  if(pp>=1 && pp<=6){ //north side
1401  det=4;
1402  row=(pp-1)*2 + (tt+1)%2 + 1;
1403  col=tt/2;
1404  }else if(pp>=7 && pp<=12){ //south side
1405  det=5;
1406  row=(12-pp)*2 + (tt)%2 + 1;
1407  col=tt/2;
1408  }
1409  id=(row-1)*16 + col;
1410 }
1411 
1412 void StFcsDb::getEPDfromId(int det, int id, int &pp, int &tt){
1413  int row=id/16 + 1;
1414  int col=id%16 + 1;
1415  if(det==4){
1416  pp = (row-1)/2 + 1;
1417  tt = (col-1)*2 - (row-1)%2 + 1;
1418  }else if(det==5){
1419  pp = 12 - (row-1)/2;
1420  tt = (col-1)*2 + (row-1)%2;
1421  }
1422 }
1423 
1424 void StFcsDb::printHeader(FILE* f, int flag=0, int csv=0){
1425  fprintf(f,"### Detector\n");
1426  fprintf(f,"#det : 0/1=ECal-N/S 2/3=Hcal-N/S 4/5=PRS-N/S\n");
1427  fprintf(f,"#id : 0~747 for Ecal 0~259 for Hcal\n");
1428  fprintf(f,"#row : 1~34 for Ecal 1~20 for Hcal\n");
1429  fprintf(f,"#col : 1~22 for Ecal 1~13 for Hcal\n");
1430  fprintf(f,"### Readout\n");
1431  fprintf(f,"#ehp : 0=ECal 1=Hcal 2=PRS\n");
1432  fprintf(f,"#ns : 0=north 1=south\n");
1433  fprintf(f,"#crt : 0=EcalNorth 1=MixNorth 2=Main 3=MixSouth 4=EcalSouth\n");
1434  fprintf(f,"#slt : 0~19\n");
1435  fprintf(f,"#dep : 0~24 for Ecal 0~7 for Hcal 0~3 for Pres\n");
1436  fprintf(f,"#ch : 0~31 for all DEP\n");
1437  fprintf(f,"### Patchpanel and cable\n");
1438  fprintf(f,"#FRow: FEEBd Row = (row-1)/2 [0-16 for Ecal 0-9 for Hcal]\n");
1439  fprintf(f,"#FCol: FEEBd Col = FEEBd Addr [0-10 for Ecal 0-12 for Hcal]\n");
1440  fprintf(f,"#SiPM: FEEBd Ch# [0-3 for Ecal 0-1 for Hcal]\n");
1441  fprintf(f,"#ppb : PatchPanel Board# [1~8 for Ecal 1-4 for Hcal]\n");
1442  fprintf(f,"#ppp : PatchPanel MDR cable P# [2-7]\n");
1443  fprintf(f,"#pph : PatchPanel Header# [1-24]\n");
1444  fprintf(f,"#jcol: Cable Jacket color\n");
1445  fprintf(f,"#wcol: Cable Wire color\n");
1446  fprintf(f,"#length: Cable length\n");
1447  if(csv==0){
1448  if(flag==0){
1449  fprintf(f,"#det id row col ehp ns crt slt dep ch Frow Fcol SiPM ppb ppp pph jcol wcol length\n");
1450  }else{
1451  fprintf(f,"#ehp ns dep ch crt slt det id row col Frow Fcol SiPM ppb ppp pph jcol wcol length\n");
1452  }
1453  }else{
1454  if(flag==0){
1455  fprintf(f,"#det,id,row,col,ehp,ns,crt,slt,dep,ch,Frow,Fcol,SiPM,ppb,ppp,pph,jcol,wcol,length\n");
1456  }else{
1457  fprintf(f,"#ehp,ns,dep,ch,crt,slt,det,id,row,col,Frow,Fcol,SiPM,ppb,ppp,pph,jcol,wcol,length\n");
1458  }
1459  }
1460 }
1461 
1463  fprintf(f,"# Css-DDch\n");
1464  fprintf(f,"# C : crate 0~4 for EcalN,MixN,Main,MixS,EcalS\n");
1465  fprintf(f,"# ss : slot (0~19)\n");
1466  fprintf(f,"# DD : DEP bd# (0~24 for Ecal, 0~7 for Hcal\n");
1467  fprintf(f,"# ch : DEP ch# (0~31 for all DEP)\n");
1468 }
1469 
1471  fprintf(f,"# crt-slt : ns ehp dep : DNiii-RR/CC DNiii-RR/CC DNiii-RR/CC ...\n");
1472  fprintf(f,"# D : \"E\"cal, \"H\"cal, \"P\"res\n");
1473  fprintf(f,"# N : North or South\n");
1474  fprintf(f,"# iii : id\n");
1475  fprintf(f,"# RR : Row\n");
1476  fprintf(f,"# CC : Column\n");
1477 }
1478 
1479 void StFcsDb::printHeader4(FILE* f, int flag=0){
1480  fprintf(f,"### Detector\n");
1481  fprintf(f,"#det : 0/1=ECal N/S, 2/3=Hcal N/S , 4/5=PRS N/S\n");
1482  fprintf(f,"#id : 0~747 for Ecal, 0~259 for Hcal\n");
1483  fprintf(f,"#row : 1~34 for Ecal, 1~20 for Hcal\n");
1484  fprintf(f,"#col : 1~22 for Ecal, 1~13 for Hcal\n");
1485  fprintf(f,"### Slow Control\n");
1486  fprintf(f,"#ehp : 0=ECal, 1=Hcal, 2=PRS\n");
1487  fprintf(f,"#ns : 0=north, 1=south\n");
1488  fprintf(f,"#dep : 0~24 for Ecal, 0~7 for Hcal, 0~3 for Pres\n");
1489  fprintf(f,"#branch : 0~1\n");
1490  fprintf(f,"#switch addr: 0~15\n");
1491  fprintf(f,"#SiPM# : 0~3 for Ecal, 0~1 for Hcal&Pres\n");
1492  fprintf(f,"### Patchpanel and cable\n");
1493  fprintf(f,"#ppb : PatchPanel Board# (1~8 for ecal, 1-4 for hcal)\n");
1494  fprintf(f,"#J : PatchPanel SC conection (J1~J3)\n");
1495  if(flag==0){
1496  fprintf(f,"#det id row col ehp ns dep bra add SiPM ppb J\n");
1497  }else{
1498  fprintf(f,"#ehp ns dep bra add SiPM det id row col\n");
1499  }
1500 }
1501 
1503  int ehp,ns,crt,slt,dep,ch,det,id,row,col=0;
1504 
1505  FILE *f1 = fopen("fcsMap.txt","w"); printHeader(f1);
1506  FILE *f1c = fopen("fcsMap.csv","w"); printHeader(f1c,0,1);
1507  FILE *f1e = fopen("fcs_ecal_readout_map.csv","w"); printHeader(f1e);
1508  FILE *f1h = fopen("fcs_hcal_readout_map.csv","w"); printHeader(f1h);
1509  FILE *f1p = fopen("fcs_pres_readout_map.csv","w"); printHeader(f1p);
1510  FILE *f2 = fopen("fcsMap2.txt","w"); printHeader2(f2);
1511  FILE *f3 = fopen("fcsDepMap.txt","w"); printHeader(f3,1);
1512  FILE *f3c= fopen("fcsDepMap.csv","w"); printHeader(f3c,1,1);
1513  FILE *f4 = fopen("fcsDepMap2.txt","w"); printHeader3(f4);
1514  FILE *f5 = fopen("fcsScMap.txt","w"); printHeader4(f5);
1515  FILE *f5e = fopen("fcs_ecal_sc_map.csv","w"); printHeader4(f5e);
1516  FILE *f5h = fopen("fcs_hcal_sc_map.csv","w"); printHeader4(f5h);
1517  FILE *f5p = fopen("fcs_pres_sc_map.csv","w"); printHeader4(f5p);
1518  FILE *f6 = fopen("fcsScRevMap.txt","w"); printHeader4(f6,1);
1519 
1520  FILE *f7 = fopen("fcsEpdMap.txt","w");
1521  FILE *fpp = fopen("fcsPPMap.txt","w");
1522 
1523  //Ecal
1524  for(ns=0; ns<2; ns++){
1525  det=ns;
1526  id=0;
1527  fprintf(f2,"Ecal NS=%1d\n",ns);
1528  for(row=1; row<=nRow(det); row++){
1529  for(col=1; col<=nColumn(det); col++){
1530  if(mMap_ehp[det][id]>=0){
1531  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",
1532  det,id,row,col,
1533  mMap_ehp[det][id],
1534  mMap_ns [det][id],
1535  mMap_crt[det][id],
1536  mMap_slt[det][id],
1537  mMap_dep[det][id],
1538  mMap_ch [det][id],
1539  (row-1)/2,
1540  mScMap_add[det][id],
1541  mScMap_sipm[det][id],
1542  mMap_ppb[det][id],
1543  mMap_ppp[det][id],
1544  mMap_pph[det][id],
1545  colJ[mMap_jcol[det][id]],
1546  colW[mMap_wcol[det][id]],
1547  leng[mMap_jcol[det][id]]);
1548  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",
1549  det,id,row,col,
1550  mMap_ehp[det][id],
1551  mMap_ns [det][id],
1552  mMap_crt[det][id],
1553  mMap_slt[det][id],
1554  mMap_dep[det][id],
1555  mMap_ch [det][id],
1556  (row-1)/2,
1557  mScMap_add[det][id],
1558  mScMap_sipm[det][id],
1559  mMap_ppb[det][id],
1560  mMap_ppp[det][id],
1561  mMap_pph[det][id],
1562  colJ[mMap_jcol[det][id]],
1563  colW[mMap_wcol[det][id]],
1564  leng[mMap_jcol[det][id]]);
1565  fprintf(f1e,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1566  det,id,row,col,
1567  mMap_ehp[det][id],
1568  mMap_ns [det][id],
1569  mMap_crt[det][id],
1570  mMap_slt[det][id],
1571  mMap_dep[det][id],
1572  mMap_ch [det][id]);
1573  fprintf(f2,"%1d%02d-%02d%02d ",
1574  mMap_crt[det][id],mMap_slt[det][id],
1575  mMap_dep[det][id],mMap_ch[det][id]);
1576  }
1577  if(mScMap_ehp[det][id]>=0){
1578  fprintf(f5,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %2d J%1d\n",
1579  det,id,row,col,
1580  mScMap_ehp[det][id],
1581  mScMap_ns [det][id],
1582  mScMap_dep[det][id],
1583  mScMap_bra[det][id],
1584  mScMap_add[det][id],
1585  mScMap_sipm[det][id],
1586  mScMap_pp[det][id],
1587  mScMap_j[det][id]);
1588  fprintf(f5e,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1589  det,id,row,col,
1590  mScMap_ehp[det][id],
1591  mScMap_ns [det][id],
1592  mScMap_dep[det][id],
1593  mScMap_bra[det][id],
1594  mScMap_add[det][id],
1595  mScMap_sipm[det][id]);
1596  }
1597  id++;
1598  }
1599  fprintf(f2,"\n");
1600  }
1601  }
1602 
1603  //Hcal
1604  for(int ns=0; ns<2; ns++){
1605  det=ns+2;
1606  id=0;
1607  fprintf(f2,"Hcal NS=%1d\n",ns);
1608  for(row=1; row<=nRow(det); row++){
1609  for(col=1; col<=nColumn(det); col++){
1610  if(mMap_ehp[det][id]>=0){
1611  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",
1612  det,id,row,col,
1613  mMap_ehp[det][id],
1614  mMap_ns[det][id],
1615  mMap_crt[det][id],
1616  mMap_slt[det][id],
1617  mMap_dep[det][id],
1618  mMap_ch[det][id],
1619  (row-1)/2,
1620  mScMap_add[det][id],
1621  mScMap_sipm[det][id],
1622  mMap_ppb[det][id],
1623  mMap_ppp[det][id],
1624  mMap_pph[det][id],
1625  colJH[mMap_jcol[det][id]],
1626  colW[mMap_wcol[det][id]],
1627  lengH[mMap_jcol[det][id]]);
1628  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",
1629  det,id,row,col,
1630  mMap_ehp[det][id],
1631  mMap_ns[det][id],
1632  mMap_crt[det][id],
1633  mMap_slt[det][id],
1634  mMap_dep[det][id],
1635  mMap_ch[det][id],
1636  (row-1)/2,
1637  mScMap_add[det][id],
1638  mScMap_sipm[det][id],
1639  mMap_ppb[det][id],
1640  mMap_ppp[det][id],
1641  mMap_pph[det][id],
1642  colJH[mMap_jcol[det][id]],
1643  colW[mMap_wcol[det][id]],
1644  lengH[mMap_jcol[det][id]]);
1645  fprintf(f1h,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1646  det,id,row,col,
1647  mMap_ehp[det][id],
1648  mMap_ns[det][id],
1649  mMap_crt[det][id],
1650  mMap_slt[det][id],
1651  mMap_dep[det][id],
1652  mMap_ch[det][id]);
1653  fprintf(f2,"%1d%02d-%02d%02d ",
1654  mMap_crt[det][id],mMap_slt[det][id],
1655  mMap_dep[det][id],mMap_ch[det][id]);
1656  }
1657  if(mScMap_ehp[det][id]>=0){
1658  fprintf(f5,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %2d J%1d\n",
1659  det,id,row,col,
1660  mScMap_ehp[det][id],
1661  mScMap_ns [det][id],
1662  mScMap_dep[det][id],
1663  mScMap_bra[det][id],
1664  mScMap_add[det][id],
1665  mScMap_sipm[det][id],
1666  mScMap_pp[det][id],
1667  mScMap_j[det][id]);
1668  fprintf(f5h,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1669  det,id,row,col,
1670  mScMap_ehp[det][id],
1671  mScMap_ns [det][id],
1672  mScMap_dep[det][id],
1673  mScMap_bra[det][id],
1674  mScMap_add[det][id],
1675  mScMap_sipm[det][id]);
1676  }
1677  id++;
1678  }
1679  fprintf(f2,"\n");
1680  }
1681  }
1682 
1683  //Prs
1684  for(int ns=0; ns<2; ns++){
1685  det=ns+4;
1686  id=0;
1687  fprintf(f2,"PRS NS=%1d\n",ns);
1688  for(row=1; row<=nRow(det); row++){
1689  for(col=1; col<=nColumn(det); col++){
1690  if(mMap_ehp[det][id]>=0){
1691  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",
1692  det,id,row,col,
1693  mMap_ehp[det][id],
1694  mMap_ns[det][id],
1695  mMap_crt[det][id],
1696  mMap_slt[det][id],
1697  mMap_dep[det][id],
1698  mMap_ch[det][id],
1699  0,0,0,0,0,0,"x","x",0.0);
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  0,0,0,0,0,0,"x","x",0.0);
1709  fprintf(f1p,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1710  det,id,row,col,
1711  mMap_ehp[det][id],
1712  mMap_ns[det][id],
1713  mMap_crt[det][id],
1714  mMap_slt[det][id],
1715  mMap_dep[det][id],
1716  mMap_ch[det][id]);
1717  fprintf(f2,"%1d%02d-%02d%02d ",
1718  mMap_crt[det][id],mMap_slt[det][id],
1719  mMap_dep[det][id],mMap_ch[det][id]);
1720  }
1721  if(mScMap_ehp[det][id]>=0){
1722  fprintf(f5,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1723  det,id,row,col,
1724  mScMap_ehp[det][id],
1725  mScMap_ns [det][id],
1726  mScMap_dep[det][id],
1727  mScMap_bra[det][id],
1728  mScMap_add[det][id],
1729  mScMap_sipm[det][id]);
1730  fprintf(f5p,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\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  }
1739  id++;
1740  }
1741  fprintf(f2,"\n");
1742  }
1743  }
1744 
1745  //DEP map
1746  for(ehp=0; ehp<kFcsEHP; ehp++){
1747  for(ns=0; ns<2; ns++){
1748  for(dep=0; dep<kFcsMaxDepBd; dep++){
1749  if(ehp==1 && dep>8) break;
1750  if(ehp==2 && dep>3) break;
1751  int flag=0;
1752  for(ch=0; ch<kFcsMaxDepCh; ch++){
1753  if(mRMap_det[ehp][ns][dep][ch]<0){
1754  if(flag==1 && ch%8==7) fprintf(f4,"\n");
1755  continue;
1756  }
1757  flag=1;
1758  det = mRMap_det[ehp][ns][dep][ch];
1759  id = mRMap_id[ehp][ns][dep][ch];
1760  crt=mRMap_crt[ehp][ns][dep][ch];
1761  slt=mRMap_slt[ehp][ns][dep][ch];
1762  if(ch==0){
1763  fprintf(f4,"%2s%2d : NS=%1d %1d(%4s) DEP%02d : ",CRT[crt],slt,ns,ehp,EHP[ehp],dep);
1764  }else if(ch%8==0){
1765  fprintf(f4," : ");
1766  }
1767  if(det>=0 && det<kFcsNDet){
1768  row=getRowNumber(det,id);
1769  col=getColumnNumber(det,id);
1770  if(ehp<2){
1771  const char* colj; float len;
1772  if(ehp==0) { colj=colJ[mMap_jcol[det][id]]; len=leng[mMap_jcol[det][id]]; }
1773  else { colj=colJH[mMap_jcol[det][id]]; len=lengH[mMap_jcol[det][id]];}
1774  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",
1775  ehp,ns,dep,ch,
1776  CRT[crt],slt,
1777  det,id,row,col,
1778  (row-1)/2,
1779  mScMap_add[det][id],
1780  mScMap_sipm[det][id],
1781  mMap_ppb[det][id],
1782  mMap_ppp[det][id],
1783  mMap_pph[det][id],
1784  colj,
1785  colW[mMap_wcol[det][id]],
1786  len);
1787  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",
1788  ehp,ns,dep,ch,
1789  CRT[crt],slt,
1790  det,id,row,col,
1791  (row-1)/2,
1792  mScMap_add[det][id],
1793  mScMap_sipm[det][id],
1794  mMap_ppb[det][id],
1795  mMap_ppp[det][id],
1796  mMap_pph[det][id],
1797  colj,
1798  colW[mMap_wcol[det][id]],
1799  len);
1800  }else{
1801  fprintf(f3,"%3d %3d %3d %3d %2s %2d %3d %3d %3d %3d\n",
1802  ehp,ns,dep,ch,
1803  CRT[crt],slt,
1804  det,id,row,col);
1805  fprintf(f3c,"%3d,%3d,%3d,%3d,%2s,%2d,%3d,%3d,%3d,%3d\n",
1806  ehp,ns,dep,ch,
1807  CRT[crt],slt,
1808  det,id,row,col);
1809  }
1810  }
1811  fprintf(f4,"%2s%03d-%02d/%02d ",DET[det],id,row,col);
1812  if(ch%8==7) fprintf(f4,"\n");
1813  }
1814  }
1815  }
1816  }
1817 
1818  //SC reverse map
1819  for(ehp=0; ehp<kFcsEHP; ehp++){
1820  for(ns=0; ns<2; ns++){
1821  for(dep=0; dep<kFcsMaxDepBd; dep++){
1822  if(ehp==1 && dep>8) break;
1823  if(ehp==2 && dep>3) break;
1824  for(int bra=0; bra<kFcsMaxBranch; bra++){
1825  for(int add=0; add<kFcsMaxAddr; add++){
1826  for(int sipm=0; sipm<kFcsMaxSiPM; sipm++){
1827  if(mRScMap_det[ehp][ns][dep][bra][add][sipm]>=0){
1828  int det=mRScMap_det[ehp][ns][dep][bra][add][sipm];
1829  int id =mRScMap_id[ehp][ns][dep][bra][add][sipm];
1830  int col=getColumnNumber(det,id);
1831  int row=getRowNumber(det,id);
1832  fprintf(f6,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1833  ehp,ns,dep,bra,add,sipm,
1834  det,id,row,col);
1835  }
1836  }
1837  }
1838  }
1839  }
1840  }
1841  }
1842 
1843  //EPD map
1844  fprintf(f7,"#ehp ns crt slt dep ch det id row col pp tt Reversed(det id r c)\n");
1845  for(int det=4; det<=5; det++){
1846  for(int r=1; r<=nRow(det); r++){
1847  for(int c=1; c<=nColumn(det); c++){
1848  int id = getId(det,r,c);
1849  int ehp,ns,crt,slt,dep,ch,det2,id2,r2,c2,pp,tt;
1850  getDepfromId(det,id,ehp,ns,crt,slt,dep,ch);
1851  getEPDfromId(det,id,pp,tt);
1852  getIdfromEPD(pp,tt,det2,id2);
1853  c2=getColumnNumber(det2,id2);
1854  r2=getRowNumber(det2,id2);
1855  fprintf(f7,"%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1856  ehp,ns,crt,slt,dep,ch,
1857  det,id,r,c,
1858  pp,tt,
1859  det2,id2,r2,c2);
1860  }
1861  }
1862  }
1863 
1864  for(int ehp=0; ehp<2; ehp++){
1865  for(int ns=0; ns<2; ns++){
1866  if(ehp==0 && ns==1) continue;
1867  int bmax=0;
1868  if(ehp==0) {bmax=8; fprintf(fpp,"Ecal\n");}
1869  if(ehp==1 && ns==0) {bmax=4; fprintf(fpp,"Hcal North\n");}
1870  if(ehp==1 && ns==1) {bmax=4; fprintf(fpp,"Hcal South\n");}
1871  fprintf(fpp,"PPB# P# DEP Ch T Pwr/ctrl Row#\n");
1872  for(int b=1; b<=bmax; b++){
1873  for(int p=2; p<=7; p++){
1874  int dep,lh,scr;
1875  if(ehp==0){
1876  dep = EPPMap[b-1][p-2][0];
1877  lh = EPPMap[b-1][p-2][1];
1878  scr = EPPMap[b-1][p-2][2];
1879  }else{
1880  dep = HPPMap[b-1][p-2][0];
1881  lh = HPPMap[b-1][p-2][1];
1882  scr = HPPMap[b-1][p-2][2];
1883  }
1884  fprintf(fpp,"%2d %2d ",b,p);
1885  if(lh>=0) {
1886  fprintf(fpp,"%2d %02d-%02d ",dep,lh*16,lh*16+15);
1887  for(int i=0; i<4; i++){
1888  int t = (p-2)*4+i;
1889  int ch = lh*16 + i*4;
1890  int det = mRMap_det[ehp][ns][dep][ch];
1891  int id = mRMap_id[ehp][ns][dep][ch];
1892  int row = getRowNumber(det,id);
1893  int col = jacketColor(ehp,ns,dep,ch);
1894  if(id>=0) {
1895  if(ehp==0){
1896  fprintf(fpp,"T%02d=R%02d-%6s ",t,row,colJ[col]);
1897  }else{
1898  fprintf(fpp,"T%02d=R%02d-%6s ",t,row,colJH[col]);
1899  }
1900  }else{
1901  fprintf(fpp,"T%02d= ",t);
1902  }
1903  }
1904  }else{
1905  fprintf(fpp," - ");
1906  }
1907  if(p%2==0) {
1908  fprintf(fpp,"J%1d ",p/2);
1909  if(scr>0) fprintf(fpp,"%2d,%2d",scr*2-1,scr*2);
1910  else if(scr==-2) fprintf(fpp,"no power");
1911  else fprintf(fpp,"-");
1912  }
1913  fprintf(fpp,"\n");
1914  }
1915  }
1916  }
1917  }
1918 
1919  fclose(f1);
1920  fclose(f1c);
1921  fclose(f1e);
1922  fclose(f1h);
1923  fclose(f1p);
1924  fclose(f2);
1925  fclose(f3);
1926  fclose(f3c);
1927  fclose(f4);
1928  fclose(f5);
1929  fclose(f5e);
1930  fclose(f5h);
1931  fclose(f5p);
1932  fclose(f6);
1933  fclose(f7);
1934  fclose(fpp);
1935 }
1936 
1937 // factor= 1(ET Match), 0(E Match), 0.5(halfway)
1938 float StFcsDb::getEtGain(int det, int id, float factor) const{
1939  if(det<0 || det>=kFcsNDet) return 0.0;
1940  if(id<0 || id>=kFcsMaxId) return 0.0;
1941  return (mEtGain[det][id]-1.0)*factor+1.0;
1942 }
1943 
1945  // double norm[2]={0.24711, 0.21781}; // [MeV/coint]
1946  double norm[2]={0.24711, 0.24711};
1947  for(int det=0; det<kFcsNDet; det++){
1948  int eh=det/2;
1949  double gain=getGain(det,0);
1950  for(int i=0; i<maxId(det); i++){
1951  double ratio=1.0;
1952  if(eh<2){ //PRES stays 1.0
1953  StThreeVectorD xyz=getStarXYZ(det,i);
1954  double r=xyz.perp();
1955  double l=xyz.mag();
1956  double ptch=gain/l*r;
1957  ratio=ptch/norm[eh]*1000;
1958  }
1959  mEtGain[det][i]=ratio;
1960  }
1961  }
1962  if(mDebug==0) return;
1963 
1964  FILE *f1 = fopen("fcsPtGain.txt","w");
1965  FILE *f2 = fopen("fcsPtGain2.txt","w");
1966  FILE *f3 = fopen("fcsPtGain3.txt","w");
1967  FILE *f4 = fopen("fcs_ecal_phys_gains.txt","w");
1968  FILE *f5 = fopen("fcs_hcal_phys_gains.txt","w");
1969  FILE *f6 = fopen("fcs_ecal_calib_gains.txt","w");
1970  FILE *f7 = fopen("fcs_hcal_calib_gains.txt","w");
1971  fprintf(f4,"#ehp ns dep ch EtGain\n");
1972  fprintf(f5,"#ehp ns dep ch EtGain\n");
1973  fprintf(f6,"#ehp ns dep ch CalibGain\n");
1974  fprintf(f7,"#ehp ns dep ch CalibGain\n");
1975  for(int det=0; det<kFcsNDet; det++){
1976  int id=0;
1977  int eh=det/2;
1978  double gain=getGain(det,0);
1979  fprintf(f2,"DET=%1d ET/ch [unit = MeV/count]\n", det);
1980  fprintf(f3,"DET=%1d normalized ET/ch [unit=%f MeV/count]\n", det,norm[eh]);
1981  for(int row=1; row<=nRow(det); row++){
1982  for(int col=1; col<=nColumn(det); col++){
1983  StThreeVectorD xyz=getStarXYZ(det,id);
1984  double r=xyz.perp();
1985  double x=xyz.x();
1986  double y=xyz.y();
1987  double z=xyz.z();
1988  double l=xyz.mag();
1989  double ptch=gain/l*r;
1990  double ratio=1.0;
1991  if(eh<2) ratio=ptch/norm[eh]*1000; //PRES stays 1.0
1992  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",
1993  det,id,row,col,x,y,z,gain,ptch*1000,ratio);
1994  fprintf(f2,"%7.5f ", ptch*1000);
1995  fprintf(f3,"%7.5f ", ratio);
1996  id++;
1997  }
1998  fprintf(f2,"\n");
1999  fprintf(f3,"\n");
2000  }
2001  }
2002  for(int ehp=0; ehp<2; ehp++){
2003  for(int ns=0; ns<2; ns++){
2004  for(int dep=0; dep<getNDep(ehp,ns); dep++){
2005  for(int ch=0; ch<kFcsMaxDepCh; ch++){
2006  int det,id,crt,slt;
2007  getIdfromDep(ehp,ns,dep,ch,det,id,crt,slt);
2008  float ratio = mEtGain[det][id];
2009  float calib = (ratio-1.0)/2.0+1.0;
2010  if(ehp==0){
2011  fprintf(f4,"%3d %3d %3d %3d %8.4f\n",ehp,ns,dep,ch,ratio);
2012  fprintf(f6,"%3d %3d %3d %3d %8.4f\n",ehp,ns,dep,ch,calib);
2013  }else{
2014  fprintf(f5,"%3d %3d %3d %3d %8.4f\n",ehp,ns,dep,ch,ratio);
2015  fprintf(f7,"%3d %3d %3d %3d %8.4f\n",ehp,ns,dep,ch,calib);
2016  }
2017  }
2018  }
2019  }
2020  }
2021  fclose(f1);
2022  fclose(f2);
2023  fclose(f3);
2024  fclose(f4);
2025  fclose(f5);
2026  fclose(f6);
2027  fclose(f7);
2028 }
2029 
2030 float StFcsDb::pedestal(int ehp, int ns, int dep, int ch){
2031  return mPed[ehp][ns][dep][ch];
2032 }
2033 
2034 void StFcsDb::setPedestal(int ehp, int ns, int dep, int ch, float ped){
2035  mPed[ehp][ns][dep][ch]=ped;
2036 }
2037 
2038 void StFcsDb::readPedFromText(const char* file){
2039  memset(mPed,0,sizeof(mPed));
2040  LOG_INFO << Form("Reading Pedestal from %s",file)<<endm;
2041  FILE* F=fopen(file,"r");
2042  if(F == NULL){
2043  LOG_ERROR << Form("Could not open %s",file)<<endm;
2044  return;
2045  }
2046  int ehp,ns,dep,ch;
2047  float mean,rms;
2048  while(fscanf(F,"%d %d %d %d %f %f",&ehp,&ns,&dep,&ch,&mean,&rms) != EOF){
2049  mPed[ehp][ns][dep][ch]=mean;
2050  LOG_INFO << Form("PED %1d %1d %2d %2d %f %f",ehp,ns,dep,ch,mPed[ehp][ns][dep][ch],rms)<<endm;
2051  }
2052  fclose(F);
2053 }
2054 
2055 void StFcsDb::readGainFromText(){
2056  memset(mGain,0,sizeof(mGain));
2057  LOG_INFO << Form("Reading Gain from %s",mGainFilename)<<endm;
2058  FILE* F=fopen(mGainFilename,"r");
2059  if(F == NULL){
2060  LOG_ERROR << Form("Could not open %s",mGainFilename)<<endm;
2061  return;
2062  }
2063  int ehp,ns,dep,ch;
2064  float gain;
2065  while(fscanf(F,"%d %d %d %d %f",&ehp,&ns,&dep,&ch,&gain) != EOF){
2066  mGain[ehp][ns][dep][ch]=gain;
2067  LOG_INFO<<Form("GAIN %1d %1d %2d %2d %f",ehp,ns,dep,ch,mGain[ehp][ns][dep][ch])<<endm;
2068  }
2069  fclose(F);
2070 }
2071 
2072 void StFcsDb::readGainCorrFromText(){
2073  memset(mGainCorr,0,sizeof(mGainCorr));
2074  LOG_INFO << Form("Reading GainCorr from %s",mGainCorrFilename)<<endm;
2075  FILE* F=fopen(mGainCorrFilename,"r");
2076  if(F == NULL){
2077  LOG_ERROR << Form("Could not open %s",mGainCorrFilename)<<endm;
2078  return;
2079  }
2080  int ehp,ns,dep,ch;
2081  float gain;
2082  while(fscanf(F,"%d %d %d %d %f",&ehp,&ns,&dep,&ch,&gain) != EOF){
2083  mGainCorr[ehp][ns][dep][ch]=gain;
2084  LOG_INFO<<Form("GAIN CORR %1d %1d %2d %2d %f",ehp,ns,dep,ch,mGainCorr[ehp][ns][dep][ch])<<endm;
2085  }
2086  fclose(F);
2087 }
2088 
2089 //g2t track info
2090 unsigned int StFcsDb::backTraceG2tTrack(unsigned int id, g2t_track_st* g2ttrk){
2091  int i = id - 1;
2092  while(g2ttrk[i].next_parent_p !=0){
2093  if(mDebug>3)
2094  LOG_INFO<<Form(" BackTrace from=%3d id=%3d Epid=%4d Gpid=%3d Vtx=%3d Parent=%3d E=%6.2f",
2095  id,g2ttrk[i].id,g2ttrk[i].eg_pid,g2ttrk[i].ge_pid,g2ttrk[i].start_vertex_p,
2096  g2ttrk[i].next_parent_p,g2ttrk[i].e)<<endm;
2097  i = g2ttrk[i].next_parent_p - 1;
2098  }
2099  if(mDebug>3) LOG_INFO<<Form(" BackTrace from=%3d id=%3d Epid=%4d Gpid=%3d Vtx=%3d Parent=%3d E=%6.2f Primary!!",
2100  id,g2ttrk[i].id,g2ttrk[i].eg_pid,g2ttrk[i].ge_pid,g2ttrk[i].start_vertex_p,
2101  g2ttrk[i].next_parent_p,g2ttrk[i].e)<<endm;
2102  return i + 1;
2103 }
2104 
2105 const g2t_track_st* StFcsDb::getParentG2tTrack(StFcsHit* h, g2t_track_st* g2ttrk, float& fraction, int& ntrk, unsigned int order){
2106  StFcsCluster c;
2107  c.hits().push_back(h); //dummy cluster with 1 hit
2108  return getG2tTrack(&c,g2ttrk,fraction,ntrk,order,0);
2109 }
2110 
2111 const g2t_track_st* StFcsDb::getPrimaryG2tTrack(StFcsHit* h, g2t_track_st* g2ttrk, float& fraction, int& ntrk,unsigned int order){
2112  StFcsCluster c;
2113  c.hits().push_back(h); //dummy cluster with 1 hit
2114  return getG2tTrack(&c,g2ttrk,fraction,ntrk,order,1);
2115 }
2116 
2117 const g2t_track_st* StFcsDb::getParentG2tTrack(StFcsCluster* c, g2t_track_st* g2ttrk, float& fraction, int& ntrk, unsigned int order){
2118  return getG2tTrack(c,g2ttrk,fraction,ntrk,order,0);
2119 }
2120 
2121 const g2t_track_st* StFcsDb::getPrimaryG2tTrack(StFcsCluster* c, g2t_track_st* g2ttrk, float& fraction, int& ntrk, unsigned int order){
2122  return getG2tTrack(c,g2ttrk,fraction,ntrk,order,1);
2123 }
2124 
2125 const g2t_track_st* StFcsDb::getG2tTrack(StFcsCluster* c, g2t_track_st* g2ttrk, float& fraction, int& ntrk, unsigned int order, int mode){
2126  float detot=0;
2127  vector<pair<unsigned int, float>> parents;
2128  for(const StFcsHit* hit : c->hits()) {
2129  for(const pair<unsigned int, float> & gt : hit->getGeantTracks()){
2130  unsigned int id=0;
2131  switch(mode){
2132  case 0: id=gt.first; break;
2133  case 1: id=backTraceG2tTrack(gt.first,g2ttrk); break;
2134  }
2135  float de=gt.second;
2136  int found=0;
2137  for(pair<unsigned int, float>& p : parents){
2138  if(p.first == id) {p.second += de; found=1; break;}
2139  }
2140  if(found==0) parents.push_back(make_pair(id,de));
2141  detot+=de;
2142  }
2143  }
2144  ntrk=parents.size();
2145  if(order >= ntrk) {fraction=0; return 0;}
2146  std::nth_element(parents.begin(), parents.begin()+order, parents.end(),
2147  [](const pair<unsigned int,float>&a, const pair<unsigned int,float>&b){
2148  return b.second < a.second;
2149  });
2150  fraction = parents[order].second / detot;
2151  if(mDebug>3){
2152  for(unsigned int jtrk=0; jtrk<ntrk; jtrk++){
2153  LOG_INFO << Form("Cluster's G2T Track %3d mode=%1d id=%3d dE=%f",jtrk,mode,parents[jtrk].first,parents[jtrk].second)<<endm;
2154  }
2155  }
2156  return &g2ttrk[parents[order].first-1];
2157 }
int getColumnNumber(int det, int id) const
get the row number for the channel
Definition: StFcsDb.cxx:448
void setPedestal(int ehp, int ns, int dep, int ch, float ped)
get Pedestal
Definition: StFcsDb.cxx:2034
void printHeader3(FILE *f)
Map header.
Definition: StFcsDb.cxx:1470
void makePPMap()
Generate maps (this is the origin of the map)
Definition: StFcsDb.cxx:1057
void makeMap2019()
cable jacket color
Definition: StFcsDb.cxx:1296
void getIdfromEPD(int pp, int tt, int &det, int &id)
Map header.
Definition: StFcsDb.cxx:1395
void printHeader(FILE *f, int flag, int csv)
Print maps.
Definition: StFcsDb.cxx:1424
float pedestal(int ehp, int ns, int dep, int ch)
Get EPD&#39;s EPD map from FCS mapping.
Definition: StFcsDb.cxx:2030
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:853
int getZeroSuppression(int det) const
fcsGain/GainCorrection related
Definition: StFcsDb.cxx:878
float getGainCorrection(int det, int id) const
get the gain for the channel for 8 timebin sum
Definition: StFcsDb.cxx:910
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:443
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:849
int jacketColor(int ehp, int ns, int dep, int ch)
Generate Patchpanel map.
Definition: StFcsDb.cxx:1025
static int getDetFromName(const std::string &detname)
Get det/id from name.
Definition: StFcsDb.cxx:514
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:845
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:768
int northSouth(int det) const
Ecal=0, Hcal=1, Pres=2.
Definition: StFcsDb.cxx:388
void getName(int det, int id, char name[])
get the DEP/ch id
Definition: StFcsDb.cxx:463
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:536
int detectorId(int eh, int ns) const
6
Definition: StFcsDb.cxx:376
double getHcalProjectedToEcalX(int ns, double hcalLocalX, double zvtx=0.0)
Project Hcal local X to Ecal local X [cm].
Definition: StFcsDb.cxx:653
const g2t_track_st * getParentG2tTrack(StFcsHit *h, g2t_track_st *g2ttrk, float &fraction, int &ntrk, unsigned int order=0)
Definition: StFcsDb.cxx:2105
float getSamplingFraction(int det) const
get zero suppression threshold
Definition: StFcsDb.cxx:871
StThreeVectorD getStarXYZfromColumnRow(int det, float col, float row, float FcsZ=-1.0) const
get the STAR frame cooridnates from other way
Definition: StFcsDb.cxx:821
void readPedFromText(const char *file="fcsped.txt")
setting pedestal
Definition: StFcsDb.cxx:2038
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:959
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:718
float getZDepth(int det) const
get the Y width of the cell
Definition: StFcsDb.cxx:597
void printHeader4(FILE *f, int flag)
Map header.
Definition: StFcsDb.cxx:1479
float getXWidth(int det) const
get the angle of the detector
Definition: StFcsDb.cxx:585
float getGain8(int det, int id) const
get the gain for the channel for 16 timebin sum
Definition: StFcsDb.cxx:942
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:610
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:826
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:1412
int ecalHcalPres(int det) const
Ecal North=0, Ecal South=1, Hcal North=2, Hcal South=3, Pres=4/5.
Definition: StFcsDb.cxx:381
float getGain(int det, int id) const
get sampling fraction
Definition: StFcsDb.cxx:886
int maxDetectorId() const
set fcsPresValley_st*
Definition: StFcsDb.cxx:374
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:736
static unsigned short getKey(unsigned short detid, unsigned short id)
Get det from name.
Definition: StFcsDb.cxx:527
float getPresValley(int det, int id) const
get the gain correction for the channel
Definition: StFcsDb.cxx:934
float getDetectorAngle(int det) const
This is the vector normal to the detector plane.
Definition: StFcsDb.cxx:575
void printHeader2(FILE *f)
Map header.
Definition: StFcsDb.cxx:1462
int getNDep(int ehp, int ns) const
Get Det map.
Definition: StFcsDb.cxx:972
void printMap()
Generate map for run19.
Definition: StFcsDb.cxx:1502
int getDepCh(int dep, int ch) const
get the id from row/col
Definition: StFcsDb.cxx:459
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:754
int maxId(int det) const
number of column
Definition: StFcsDb.cxx:426
void makeMap()
Get Id from SC.
Definition: StFcsDb.cxx:1094
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:1010
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:993
int getId(int det, int row, int col) const
get the column number for the channel
Definition: StFcsDb.cxx:453
int nRow(int det) const
north or south side
Definition: StFcsDb.cxx:392
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:727
void setFcsDetectorPosition(fcsDetectorPosition_st *t)
set leaky Hcal
Definition: StFcsDb.cxx:255
int nColumn(int det) const
number of rows
Definition: StFcsDb.cxx:409
unsigned int backTraceG2tTrack(unsigned int id, g2t_track_st *g2ttrk)
reading pedestal from text
Definition: StFcsDb.cxx:2090
static void getFromName(const char name[], int &det, int &id)
Get Name of a channel.
Definition: StFcsDb.cxx:505
void printEtGain()
ET gain.
Definition: StFcsDb.cxx:1944
StThreeVectorD getStarXYZ_4x4(int det, int col, int row) const
Get the STAR frame cooridnates for 4x4 sum.
Definition: StFcsDb.cxx:636
StThreeVectorD projectTrackToHcalSMax(const g2t_track_st *g2ttrk, const g2t_vertex_st *g2tvert=0) const
SMax = Shower Max Z.
Definition: StFcsDb.cxx:745
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:593
double getHcalProjectedToEcalY(int ns, double hcalLocalY, double zvtx=0.0)
Project Hcal local Y to Ecal local Y [cm].
Definition: StFcsDb.cxx:671
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:946
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:683