StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EemcTrigUtil.cxx
1 #include <dirent.h>
2 #include <pwd.h>
3 #include <cstdio>
4 #include <algorithm>
5 
6 using namespace std;
7 
8 #include "StMessMgr.h"
9 #include "TString.h"
10 
11 #include "EemcTrigUtil.h"
12 
13 // -----------------------------------------------------------------------------------------------------
14 //
15 // pp2006 run period http://drupal.star.bnl.gov/STAR/?q=node/8349
16 //
17 // date0 time0 dateF timeF HT[] TP[] JP[] HTTP-select // run number
18 DsmThreshold _pp2006Long1a = {20060323,181204,20060406,210325,{6,12,18},{1,17,31},{32,49,69},2,2,71,21,0,3,109}; // 7082079
19 DsmThreshold _pp2006Trans1a = {20060406,210325,20060509, 10815,{6,12,22},{1,17,31},{32,49,69},2,2,71,21,0,3,109}; // 7096033
20 DsmThreshold _pp2006Trans1b = {20060509, 10815,20060509, 24937,{6,13,22},{1,21,31},{32,49,69},2,2,71,21,0,3,109}; // 7128050
21 DsmThreshold _pp2006Trans1c = {20060509, 24937,20060509, 30315,{6,12,22},{1,17,31},{36,49,69},2,2,71,21,0,3,109}; // 7128060
22 DsmThreshold _pp2006Trans1d = {20060509, 30315,20060510,204714,{6,13,22},{1,21,31},{32,49,69},2,2,71,21,0,3,109}; // 7128061
23 DsmThreshold _pp2006Long2a = {20060510,204714,20060512,192554,{6,12,22},{1,20,31},{38,49,69},2,2,71,21,0,3,109}; // 7130037
24 DsmThreshold _pp2006Long2b = {20060512,192554,20060513,215249,{6,17,22},{1,20,31},{39,50,69},2,2,71,21,0,3,109}; // 7132045
25 DsmThreshold _pp2006Long2c = {20060513,215249,20060607,234011,{6,16,22},{1,20,31},{39,52,69},2,2,71,21,0,3,109}; // 7133052
26 
27 //
28 // end pp2006
29 
30 // #### modified by Liaoyuan ####
31 // pp2009 run period and thresholds
32 // http://www.star.bnl.gov/HyperNews-star/get/startrig/3753.html &
33 // StRoot/StTriggerUtilities/Bemc/StBemcTriggerDbThresholds.cxx
34 // date0 time0 dateF timeF HT[] TP[] JP[] HTTP-select // run number
35 DsmThreshold _pp2009_500GeV = {20090217, 0,20090415, 0,{20,40,-1},{-1,-1,-1},{28,35,52},-1,2,71,21,0,3,109};
36 DsmThreshold _pp2009_200GeV = {20090415, 1,20090515, 0,{17,23,-1},{-1,-1,-1},{20,28,35},-1,2,71,21,0,3,109};
37 
38 //
39 // end pp2009
40 
41 DsmThreshold _thresholds[] = {
42  _pp2006Long1a, _pp2006Trans1a, _pp2006Trans1b, _pp2006Trans1c, _pp2006Trans1d, _pp2006Long2a, _pp2006Long2b, _pp2006Long2c,
43  _pp2009_500GeV, _pp2009_200GeV
44 };
45 
46 // #### modified end ####
47 
48 //==================================================
49 //==================================================
50 
51 void
52 EemcTrigUtil::getDsmThresholds(int yyyymmdd, int hhmmss, DsmThreshold &thresholds) {
53  thresholds.date_start = 0;
54  thresholds.time_start = 0;
55  thresholds.date_finish = 0;
56  thresholds.time_finish = 0;
57  thresholds.HT[0] = -1;
58  thresholds.HT[1] = -1;
59  thresholds.HT[2] = -1;
60  thresholds.TP[0] = -1;
61  thresholds.TP[1] = -1;
62  thresholds.TP[2] = -1;
63  thresholds.JP[0] = -1;
64  thresholds.JP[1] = -1;
65  thresholds.JP[2] = -1;
66  thresholds.HTTPselect = 2;
67 
68  double wallTime= yyyymmdd+ hhmmss/1e6;
69  Bool_t go = false;
70  for (UInt_t i = 0;(i < sizeof(_thresholds) / sizeof(_thresholds[0])) && !go;i++)
71  {
72  Double_t wallStart = _thresholds[i].date_start + _thresholds[i].time_start / 1.0E6;
73  Double_t wallEnd = _thresholds[i].date_finish + _thresholds[i].time_finish / 1.0E6;
74  if ( wallTime >= wallStart && wallTime < wallEnd ) {
75  go = true;
76  thresholds = _thresholds[i];
77  LOG_INFO<<Form( "Load DSM thresholds for %8i %06i: HT %02i %02i %02i TP %02i %02i %02i JP %02i %02i %02i",
78  yyyymmdd,hhmmss,
79  thresholds.HT[0], thresholds.HT[1], thresholds.HT[2],
80  thresholds.TP[0], thresholds.TP[1], thresholds.TP[2],
81  thresholds.JP[0], thresholds.JP[1], thresholds.JP[2]
82  ) << endm;
83  }
84  }
85  if (!go) {
86  LOG_ERROR << "timestamp out of range of lookup table in EemcTrigUtil" << endm;
87  }
88 }
89 
90 //==================================================
91 //==================================================
92 
93 void
94 EemcTrigUtil::getFeePed4(const char *path,int yyyyyymmdd, int hhmmss, int mxChan, int *feePed4){
95  if (yyyyyymmdd<20060307) {
96  LOG_ERROR << "not implemented before 2006" << endm;
97  }
98  if (yyyyyymmdd<=20060620) { // real data ended on June-20, day171
99  const char *dataSet = "03.07.2006";
100  if (yyyyyymmdd>=20060406) dataSet = "04.06.2006";
101  if (yyyyyymmdd>=20060428) dataSet = "04.28.2006";
102  LOG_DEBUG << Form("getEndcap FeePed4 input set=%s\n",dataSet) << endm;
103  char dir[FILENAME_MAX];
104  sprintf(dir,"%s/%s",path,dataSet);
105  readPed4(dir,mxChan,feePed4);
106  } else {
107  // assume it is MC, 12bit ADC peds are at 0
108  int ped4val=5;
109  LOG_DEBUG << Form("set Endcap FeePed4 to val=%d\n",ped4val) << endm;
110  genPed4(ped4val,mxChan, feePed4);
111  }
112 }
113 
114 //==================================================
115 //==================================================
116 
117 static int filter(const struct dirent* d)
118 {
119  int month, day, year;
120  return strlen(d->d_name) == 10 && sscanf(d->d_name,"%d.%d.%d",&month,&day,&year) == 3;
121 }
122 
123 static TDatime getDateTime(const char* dirname)
124 {
125  int month, day, year;
126  sscanf(dirname,"%d.%d.%d",&month,&day,&year);
127  return TDatime(year,month,day,0,0,0);
128 }
129 
130 static void scanDirForDates(const char* dir, vector<TDatime>& dates)
131 {
132  struct dirent** namelist;
133  int n = scandir(dir,&namelist,filter,0);
134  if (n == -1) return;
135  while (n--) {
136  dates.push_back(getDateTime(namelist[n]->d_name));
137  free(namelist[n]);
138  }
139  free(namelist);
140  sort(dates.begin(),dates.end());
141 }
142 
143 static TDatime getTimeStampFromDates(const TDatime& date, const vector<TDatime>& dates)
144 {
145  for (vector<TDatime>::const_reverse_iterator i = dates.rbegin(); i != dates.rend(); ++i)
146  if (date > *i) return *i;
147  return dates.front();
148 }
149 
150 void EemcTrigUtil::getFeePed4(const TDatime& date, int mxChan, int *feePed4)
151 {
152  char dir[FILENAME_MAX];
153  struct passwd* pw = getpwnam("pibero");
154  sprintf(dir,"%s/public/StarTrigSimuSetup/ped",pw->pw_dir);
155  vector<TDatime> dates;
156  scanDirForDates(dir,dates);
157  // Adjust times
158  for (size_t i = 0; i < dates.size(); ++i) {
159  if (dates[i].GetDate() == 20090506) dates[i].Set("2009-05-06 10:00:00");
160  if (dates[i].GetDate() == 20090513) dates[i].Set("2009-05-13 17:00:00");
161  if (dates[i].GetDate() == 20090517) dates[i].Set("2009-05-17 23:30:00");
162  if (dates[i].GetDate() == 20090520) dates[i].Set("2009-05-20 07:00:00");
163  if (dates[i].GetDate() == 20090606) dates[i].Set("2009-06-06 18:00:00");
164  }
165  TDatime timeStamp = getTimeStampFromDates(date,dates);
166  TString timeStampString = Form("%02d.%02d.%4d",timeStamp.GetMonth(),timeStamp.GetDay(),timeStamp.GetYear());
167  char pathname[FILENAME_MAX];
168  sprintf(pathname,"%s/%s",dir,timeStampString.Data());
169  LOG_INFO << "Using ped4 directory " << pathname << endm;
170  readPed4(pathname,mxChan,feePed4);
171 
172  // Check for additional pedestal files from this time stamp, e.g. 05.06.2009_crate5board2.ped4
173  for (int crate = 1; crate <= 6; ++crate) {
174  for (int board = 1; board <= 4; ++board) {
175  sprintf(pathname,"%s/%s_crate%dboard%d.ped4",dir,timeStampString.Data(),crate,board);
176  if (FILE* fp = fopen(pathname,"r")) {
177  LOG_INFO << "Using ped4 file " << pathname << endm;
178  int ped4val;
179  for (int chan = 0; fscanf(fp,"%d",&ped4val)!= EOF; ++chan) {
180  int rdo = (crate-1)*mxChan+(board-1)*32+chan;
181  feePed4[rdo] = ped4val;
182  LOG_DEBUG << Form("crate=%d board=%d chan=%d rdo=%d ped4=%d",crate,board,chan,rdo,ped4val) << endm;
183  }
184  fclose(fp);
185  }
186  }
187  }
188 }
189 
190 //==================================================
191 //==================================================
192 
193 static int maskFilter(const struct dirent* d)
194 {
195  int month, day, year;
196  return strlen(d->d_name) == 16 && sscanf(d->d_name,"eec.%d-%d-%d.dat",&month,&day,&year) == 3;
197 }
198 
199 static TDatime getMaskDateTime(const char* maskfile)
200 {
201  int month, day, year;
202  sscanf(maskfile,"eec.%d-%d-%d.dat",&month,&day,&year);
203  return TDatime(2000+year,month,day,0,0,0);
204 }
205 
206 static void scanMaskDirForDates(const char* dir, vector<TDatime>& dates)
207 {
208  struct dirent** namelist;
209  int n = scandir(dir,&namelist,maskFilter,0);
210  if (n == -1) return;
211  while (n--) {
212  dates.push_back(getMaskDateTime(namelist[n]->d_name));
213  free(namelist[n]);
214  }
215  free(namelist);
216  sort(dates.begin(),dates.end());
217 }
218 
219 void EemcTrigUtil::getFeeOutMask(const char* maskfile, int* highTowerMask, int* patchSumMask)
220 {
221  FILE* fp = fopen(maskfile,"r");
222  if (fp) {
223  char line[100];
224  while (fgets(line,sizeof(line),fp) != NULL) {
225  if (*line == '#') continue;
226  int jetpatch, triggerpatch;
227  char s1[3], s2[3];
228  *s1 = *s2 = 0;
229  int n = sscanf(line,"%d %d %s %s\n",&jetpatch,&triggerpatch,s1,s2);
230  if (n > 2) {
231  int triggerpatch2;
232  getTriggerPatchFromSteveJetPatchAndTriggerPatch(jetpatch,triggerpatch,triggerpatch2);
233  LOG_INFO << Form("JP=%d TP=%d %s %s (%d)",jetpatch,triggerpatch,s1,s2,triggerpatch2) << endm;
234  if (strncmp(s1,"HT",2) == 0 || strncmp(s2,"HT",2) == 0) highTowerMask[triggerpatch2] = 0;
235  if (strncmp(s1,"TP",2) == 0 || strncmp(s2,"TP",2) == 0) patchSumMask[triggerpatch2] = 0;
236  }
237  }
238  }
239  fclose(fp);
240 }
241 
242 void EemcTrigUtil::getDsmAndChannelFromSteveJetPatchAndTriggerPatch(int jetpatch, int triggerpatch, int& dsm, int& chan)
243 {
244  // See Steve Vigdor's EEMC Trigger Patches document
245  static const int dsmMap[6][15] = {
246  { 7,7,8,7,7,8,7,7,8,7,7,8,7,7,8 },
247  { 9,9,8,9,9,8,9,9,8,9,9,8,9,9,8 },
248  { 1,1,2,1,1,2,1,1,2,1,1,2,1,1,2 },
249  { 3,3,2,3,3,2,3,3,2,3,3,2,3,3,2 },
250  { 4,4,5,4,4,5,4,4,5,4,4,5,4,4,5 },
251  { 6,6,5,6,6,5,6,6,5,6,6,5,6,6,5 }
252  };
253  static const int chanMap[6][15] = {
254  { 0,1,0,2,3,1,4,5,2,6,7,3,8,9,4 },
255  { 0,1,5,2,3,6,4,5,7,6,7,8,8,9,9 },
256  { 0,1,0,2,3,1,4,5,2,6,7,3,8,9,4 },
257  { 0,1,5,2,3,6,4,5,7,6,7,8,8,9,9 },
258  { 0,1,0,2,3,1,4,5,2,6,7,3,8,9,4 },
259  { 0,1,5,2,3,6,4,5,7,6,7,8,8,9,9 }
260  };
261  dsm = dsmMap[jetpatch-1][triggerpatch-1];
262  chan = chanMap[jetpatch-1][triggerpatch-1];
263 }
264 
265 void EemcTrigUtil::getTriggerPatchFromDsmAndChannel(int dsm, int chan, int& triggerpatch)
266 {
267  triggerpatch = (dsm-1)*10+chan;
268 }
269 
270 void EemcTrigUtil::getTriggerPatchFromSteveJetPatchAndTriggerPatch(int jetpatch, int triggerpatch, int& triggerpatch2)
271 {
272  int dsm, chan;
273  getDsmAndChannelFromSteveJetPatchAndTriggerPatch(jetpatch,triggerpatch,dsm,chan);
274  getTriggerPatchFromDsmAndChannel(dsm,chan,triggerpatch2);
275 }
276 
277 void EemcTrigUtil::getFeeOutMask(const TDatime& date, int* highTowerMask, int* patchSumMask)
278 {
279  char maskdir[FILENAME_MAX];
280  struct passwd* pw = getpwnam("pibero");
281  sprintf(maskdir,"%s/public/StarTrigSimuSetup/mask",pw->pw_dir);
282  vector<TDatime> dates;
283  scanMaskDirForDates(maskdir,dates);
284  TDatime timestamp = getTimeStampFromDates(date,dates);
285  char maskfile[FILENAME_MAX];
286  sprintf(maskfile,"%s/eec.%02d-%02d-%02d.dat",maskdir,timestamp.GetMonth(),timestamp.GetDay(),timestamp.GetYear()%100);
287  LOG_INFO << "Using mask file " << maskfile << endm;
288  getFeeOutMask(maskfile,highTowerMask,patchSumMask);
289 }
290 
291 void EemcTrigUtil::getFeeBoardFromSteveTriggerPatch(int triggerpatch, int& board)
292 {
293  // See Steve Vigdor's EEMC Trigger Patches document
294  static const int boardMap[15] = { 1,3,1,1,3,1,2,4,2,2,4,2,3,4,3 };
295  board = boardMap[triggerpatch-1];
296 }
297 
298 void EemcTrigUtil::getFeeBoardMask(const TDatime& date, int* highTower)
299 {
300  char maskdir[FILENAME_MAX];
301  struct passwd* pw = getpwnam("pibero");
302  sprintf(maskdir,"%s/public/StarTrigSimuSetup/mask",pw->pw_dir);
303  vector<TDatime> dates;
304  scanDirForDates(maskdir,dates);
305  TDatime timestamp = getTimeStampFromDates(date,dates);
306  char timestampString[12];
307  sprintf(timestampString,"/%02d.%02d.%4d",timestamp.GetMonth(),timestamp.GetDay(),timestamp.GetYear());
308  strcat(maskdir,timestampString);
309  LOG_INFO << "Using mask directory " << maskdir << endm;
310  for (int jetpatch = 1; jetpatch <= 6; ++jetpatch) {
311  int boardmask[5];
312  fill(boardmask,boardmask+5,0);
313  char maskfile[FILENAME_MAX];
314  sprintf(maskfile,"%s/tower-%d-current_beam_config.dat",maskdir,jetpatch);
315  LOG_INFO << "Scanning mask file " << maskfile << endm;
316  FILE* fp = fopen(maskfile,"r");
317  if (fp) {
318  int value;
319  char name[100];
320  while (fscanf(fp,"%x %s\n",&value,name) != EOF) {
321  if (strcmp(name,"board1mask") == 0) boardmask[0] = value;
322  if (strcmp(name,"board2mask") == 0) boardmask[1] = value;
323  if (strcmp(name,"board3mask") == 0) boardmask[2] = value;
324  if (strcmp(name,"board4mask") == 0) boardmask[3] = value;
325  if (strcmp(name,"board5mask") == 0) boardmask[4] = value;
326  }
327  }
328  fclose(fp);
329  for (int triggerpatch = 1; triggerpatch <= 15; ++triggerpatch) {
330  int board = 0;
331  getFeeBoardFromSteveTriggerPatch(triggerpatch,board);
332  if (boardmask[board-1]) {
333  int triggerpatch2;
334  getTriggerPatchFromSteveJetPatchAndTriggerPatch(jetpatch,triggerpatch,triggerpatch2);
335  highTower[triggerpatch2] = 0;
336  LOG_INFO << Form("JP=%d TP=%d Brd=%d (%d)",jetpatch,triggerpatch,board,triggerpatch2) << endm;
337  }
338  }
339  }
340 }
341 
342 //==================================================
343 //==================================================
344 
345 void
346 EemcTrigUtil::genPed4(int ped4val, int mxChan, int *feePed4){
347  for (int crate=1; crate<=6; crate++){
348  for (int board=1; board<=4; board++){
349  for (int chan=0; chan<32; chan++){
350  int rdo = (crate-1)*mxChan+(board-1)*32+chan;
351  feePed4[rdo]=ped4val;
352  LOG_DEBUG << Form("crate=%d board=%d chan=%d rdo=%d ped4=%d",crate,board,chan,rdo,ped4val) << endm;
353  }
354  }
355  }
356 }
357 
358 //==================================================
359 //==================================================
360 
361 void
362 EemcTrigUtil::readPed4(const char *path, int mxChan, int *feePed4) {
363  for (int crate = 1; crate <= 6; ++crate) {
364  for (int board = 1; board <= 4; ++board) {
365  char fname[FILENAME_MAX];
366  sprintf(fname,"%s/crate%dboard%d.ped4",path,crate,board);
367  FILE* fd = fopen(fname,"r");
368  if (!fd) {
369  LOG_ERROR << "Could not open " << fname << " for reading" << endm;
370  continue;
371  }
372  LOG_DEBUG << "Reading " << fname << endm;
373  int chan;
374  for (chan = 0; chan < 32; ++chan) {
375  int ped4;
376  if (fscanf(fd,"%d",&ped4) == EOF) break;
377  int rdo = (crate-1)*mxChan+(board-1)*32+chan;
378  feePed4[rdo] = ped4;
379  LOG_DEBUG << Form("crate=%d board=%d chan=%d rdo=%d ped4=%d",crate,board,chan,rdo,ped4) << endm;
380  }
381  LOG_DEBUG << "Read " << chan << " channels" << endm;
382  fclose(fd);
383  }
384  }
385 }