00001 #include <dirent.h>
00002 #include <pwd.h>
00003 #include <cstdio>
00004 #include <algorithm>
00005
00006 using namespace std;
00007
00008 #include "StMessMgr.h"
00009 #include "TString.h"
00010
00011 #include "EemcTrigUtil.h"
00012
00013
00014
00015
00016
00017
00018 DsmThreshold _pp2006Long1a = {20060323,181204,20060406,210325,{6,12,18},{1,17,31},{32,49,69},2,2,71,21,0,3,109};
00019 DsmThreshold _pp2006Trans1a = {20060406,210325,20060509, 10815,{6,12,22},{1,17,31},{32,49,69},2,2,71,21,0,3,109};
00020 DsmThreshold _pp2006Trans1b = {20060509, 10815,20060509, 24937,{6,13,22},{1,21,31},{32,49,69},2,2,71,21,0,3,109};
00021 DsmThreshold _pp2006Trans1c = {20060509, 24937,20060509, 30315,{6,12,22},{1,17,31},{36,49,69},2,2,71,21,0,3,109};
00022 DsmThreshold _pp2006Trans1d = {20060509, 30315,20060510,204714,{6,13,22},{1,21,31},{32,49,69},2,2,71,21,0,3,109};
00023 DsmThreshold _pp2006Long2a = {20060510,204714,20060512,192554,{6,12,22},{1,20,31},{38,49,69},2,2,71,21,0,3,109};
00024 DsmThreshold _pp2006Long2b = {20060512,192554,20060513,215249,{6,17,22},{1,20,31},{39,50,69},2,2,71,21,0,3,109};
00025 DsmThreshold _pp2006Long2c = {20060513,215249,20060607,234011,{6,16,22},{1,20,31},{39,52,69},2,2,71,21,0,3,109};
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 DsmThreshold _pp2009_500GeV = {20090217, 0,20090415, 0,{20,40,-1},{-1,-1,-1},{28,35,52},-1,2,71,21,0,3,109};
00036 DsmThreshold _pp2009_200GeV = {20090415, 1,20090515, 0,{17,23,-1},{-1,-1,-1},{20,28,35},-1,2,71,21,0,3,109};
00037
00038
00039
00040
00041 DsmThreshold _thresholds[] = {
00042 _pp2006Long1a, _pp2006Trans1a, _pp2006Trans1b, _pp2006Trans1c, _pp2006Trans1d, _pp2006Long2a, _pp2006Long2b, _pp2006Long2c,
00043 _pp2009_500GeV, _pp2009_200GeV
00044 };
00045
00046
00047
00048
00049
00050
00051 void
00052 EemcTrigUtil::getDsmThresholds(int yyyymmdd, int hhmmss, DsmThreshold &thresholds) {
00053 thresholds.date_start = 0;
00054 thresholds.time_start = 0;
00055 thresholds.date_finish = 0;
00056 thresholds.time_finish = 0;
00057 thresholds.HT[0] = -1;
00058 thresholds.HT[1] = -1;
00059 thresholds.HT[2] = -1;
00060 thresholds.TP[0] = -1;
00061 thresholds.TP[1] = -1;
00062 thresholds.TP[2] = -1;
00063 thresholds.JP[0] = -1;
00064 thresholds.JP[1] = -1;
00065 thresholds.JP[2] = -1;
00066 thresholds.HTTPselect = 2;
00067
00068 double wallTime= yyyymmdd+ hhmmss/1e6;
00069 Bool_t go = false;
00070 for (UInt_t i = 0;(i < sizeof(_thresholds) / sizeof(_thresholds[0])) && !go;i++)
00071 {
00072 Double_t wallStart = _thresholds[i].date_start + _thresholds[i].time_start / 1.0E6;
00073 Double_t wallEnd = _thresholds[i].date_finish + _thresholds[i].time_finish / 1.0E6;
00074 if ( wallTime >= wallStart && wallTime < wallEnd ) {
00075 go = true;
00076 thresholds = _thresholds[i];
00077 LOG_INFO<<Form( "Load DSM thresholds for %8i %06i: HT %02i %02i %02i TP %02i %02i %02i JP %02i %02i %02i",
00078 yyyymmdd,hhmmss,
00079 thresholds.HT[0], thresholds.HT[1], thresholds.HT[2],
00080 thresholds.TP[0], thresholds.TP[1], thresholds.TP[2],
00081 thresholds.JP[0], thresholds.JP[1], thresholds.JP[2]
00082 ) << endm;
00083 }
00084 }
00085 if (!go) {
00086 LOG_ERROR << "timestamp out of range of lookup table in EemcTrigUtil" << endm;
00087 }
00088 }
00089
00090
00091
00092
00093 void
00094 EemcTrigUtil::getFeePed4(const char *path,int yyyyyymmdd, int hhmmss, int mxChan, int *feePed4){
00095 if (yyyyyymmdd<20060307) {
00096 LOG_ERROR << "not implemented before 2006" << endm;
00097 }
00098 if (yyyyyymmdd<=20060620) {
00099 const char *dataSet = "03.07.2006";
00100 if (yyyyyymmdd>=20060406) dataSet = "04.06.2006";
00101 if (yyyyyymmdd>=20060428) dataSet = "04.28.2006";
00102 LOG_DEBUG << Form("getEndcap FeePed4 input set=%s\n",dataSet) << endm;
00103 char dir[FILENAME_MAX];
00104 sprintf(dir,"%s/%s",path,dataSet);
00105 readPed4(dir,mxChan,feePed4);
00106 } else {
00107
00108 int ped4val=5;
00109 LOG_DEBUG << Form("set Endcap FeePed4 to val=%d\n",ped4val) << endm;
00110 genPed4(ped4val,mxChan, feePed4);
00111 }
00112 }
00113
00114
00115
00116
00117 static int filter(const struct dirent* d)
00118 {
00119 int month, day, year;
00120 return strlen(d->d_name) == 10 && sscanf(d->d_name,"%d.%d.%d",&month,&day,&year) == 3;
00121 }
00122
00123 static TDatime getDateTime(const char* dirname)
00124 {
00125 int month, day, year;
00126 sscanf(dirname,"%d.%d.%d",&month,&day,&year);
00127 return TDatime(year,month,day,0,0,0);
00128 }
00129
00130 static void scanDirForDates(const char* dir, vector<TDatime>& dates)
00131 {
00132 struct dirent** namelist;
00133 int n = scandir(dir,&namelist,filter,0);
00134 if (n == -1) return;
00135 while (n--) {
00136 dates.push_back(getDateTime(namelist[n]->d_name));
00137 free(namelist[n]);
00138 }
00139 free(namelist);
00140 sort(dates.begin(),dates.end());
00141 }
00142
00143 static TDatime getTimeStampFromDates(const TDatime& date, const vector<TDatime>& dates)
00144 {
00145 for (vector<TDatime>::const_reverse_iterator i = dates.rbegin(); i != dates.rend(); ++i)
00146 if (date > *i) return *i;
00147 return dates.front();
00148 }
00149
00150 void EemcTrigUtil::getFeePed4(const TDatime& date, int mxChan, int *feePed4)
00151 {
00152 char dir[FILENAME_MAX];
00153 struct passwd* pw = getpwnam("pibero");
00154 sprintf(dir,"%s/public/StarTrigSimuSetup/ped",pw->pw_dir);
00155 vector<TDatime> dates;
00156 scanDirForDates(dir,dates);
00157
00158 for (size_t i = 0; i < dates.size(); ++i) {
00159 if (dates[i].GetDate() == 20090506) dates[i].Set("2009-05-06 10:00:00");
00160 if (dates[i].GetDate() == 20090513) dates[i].Set("2009-05-13 17:00:00");
00161 if (dates[i].GetDate() == 20090517) dates[i].Set("2009-05-17 23:30:00");
00162 if (dates[i].GetDate() == 20090520) dates[i].Set("2009-05-20 07:00:00");
00163 if (dates[i].GetDate() == 20090606) dates[i].Set("2009-06-06 18:00:00");
00164 }
00165 TDatime timeStamp = getTimeStampFromDates(date,dates);
00166 TString timeStampString = Form("%02d.%02d.%4d",timeStamp.GetMonth(),timeStamp.GetDay(),timeStamp.GetYear());
00167 char pathname[FILENAME_MAX];
00168 sprintf(pathname,"%s/%s",dir,timeStampString.Data());
00169 LOG_INFO << "Using ped4 directory " << pathname << endm;
00170 readPed4(pathname,mxChan,feePed4);
00171
00172
00173 for (int crate = 1; crate <= 6; ++crate) {
00174 for (int board = 1; board <= 4; ++board) {
00175 sprintf(pathname,"%s/%s_crate%dboard%d.ped4",dir,timeStampString.Data(),crate,board);
00176 if (FILE* fp = fopen(pathname,"r")) {
00177 LOG_INFO << "Using ped4 file " << pathname << endm;
00178 int ped4val;
00179 for (int chan = 0; fscanf(fp,"%d",&ped4val)!= EOF; ++chan) {
00180 int rdo = (crate-1)*mxChan+(board-1)*32+chan;
00181 feePed4[rdo] = ped4val;
00182 LOG_DEBUG << Form("crate=%d board=%d chan=%d rdo=%d ped4=%d",crate,board,chan,rdo,ped4val) << endm;
00183 }
00184 fclose(fp);
00185 }
00186 }
00187 }
00188 }
00189
00190
00191
00192
00193 static int maskFilter(const struct dirent* d)
00194 {
00195 int month, day, year;
00196 return strlen(d->d_name) == 16 && sscanf(d->d_name,"eec.%d-%d-%d.dat",&month,&day,&year) == 3;
00197 }
00198
00199 static TDatime getMaskDateTime(const char* maskfile)
00200 {
00201 int month, day, year;
00202 sscanf(maskfile,"eec.%d-%d-%d.dat",&month,&day,&year);
00203 return TDatime(2000+year,month,day,0,0,0);
00204 }
00205
00206 static void scanMaskDirForDates(const char* dir, vector<TDatime>& dates)
00207 {
00208 struct dirent** namelist;
00209 int n = scandir(dir,&namelist,maskFilter,0);
00210 if (n == -1) return;
00211 while (n--) {
00212 dates.push_back(getMaskDateTime(namelist[n]->d_name));
00213 free(namelist[n]);
00214 }
00215 free(namelist);
00216 sort(dates.begin(),dates.end());
00217 }
00218
00219 void EemcTrigUtil::getFeeOutMask(const char* maskfile, int* highTowerMask, int* patchSumMask)
00220 {
00221 FILE* fp = fopen(maskfile,"r");
00222 if (fp) {
00223 char line[100];
00224 while (fgets(line,sizeof(line),fp) != NULL) {
00225 if (*line == '#') continue;
00226 int jetpatch, triggerpatch;
00227 char s1[3], s2[3];
00228 *s1 = *s2 = 0;
00229 int n = sscanf(line,"%d %d %s %s\n",&jetpatch,&triggerpatch,s1,s2);
00230 if (n > 2) {
00231 int triggerpatch2;
00232 getTriggerPatchFromSteveJetPatchAndTriggerPatch(jetpatch,triggerpatch,triggerpatch2);
00233 LOG_INFO << Form("JP=%d TP=%d %s %s (%d)",jetpatch,triggerpatch,s1,s2,triggerpatch2) << endm;
00234 if (strncmp(s1,"HT",2) == 0 || strncmp(s2,"HT",2) == 0) highTowerMask[triggerpatch2] = 0;
00235 if (strncmp(s1,"TP",2) == 0 || strncmp(s2,"TP",2) == 0) patchSumMask[triggerpatch2] = 0;
00236 }
00237 }
00238 }
00239 fclose(fp);
00240 }
00241
00242 void EemcTrigUtil::getDsmAndChannelFromSteveJetPatchAndTriggerPatch(int jetpatch, int triggerpatch, int& dsm, int& chan)
00243 {
00244
00245 static const int dsmMap[6][15] = {
00246 { 7,7,8,7,7,8,7,7,8,7,7,8,7,7,8 },
00247 { 9,9,8,9,9,8,9,9,8,9,9,8,9,9,8 },
00248 { 1,1,2,1,1,2,1,1,2,1,1,2,1,1,2 },
00249 { 3,3,2,3,3,2,3,3,2,3,3,2,3,3,2 },
00250 { 4,4,5,4,4,5,4,4,5,4,4,5,4,4,5 },
00251 { 6,6,5,6,6,5,6,6,5,6,6,5,6,6,5 }
00252 };
00253 static const int chanMap[6][15] = {
00254 { 0,1,0,2,3,1,4,5,2,6,7,3,8,9,4 },
00255 { 0,1,5,2,3,6,4,5,7,6,7,8,8,9,9 },
00256 { 0,1,0,2,3,1,4,5,2,6,7,3,8,9,4 },
00257 { 0,1,5,2,3,6,4,5,7,6,7,8,8,9,9 },
00258 { 0,1,0,2,3,1,4,5,2,6,7,3,8,9,4 },
00259 { 0,1,5,2,3,6,4,5,7,6,7,8,8,9,9 }
00260 };
00261 dsm = dsmMap[jetpatch-1][triggerpatch-1];
00262 chan = chanMap[jetpatch-1][triggerpatch-1];
00263 }
00264
00265 void EemcTrigUtil::getTriggerPatchFromDsmAndChannel(int dsm, int chan, int& triggerpatch)
00266 {
00267 triggerpatch = (dsm-1)*10+chan;
00268 }
00269
00270 void EemcTrigUtil::getTriggerPatchFromSteveJetPatchAndTriggerPatch(int jetpatch, int triggerpatch, int& triggerpatch2)
00271 {
00272 int dsm, chan;
00273 getDsmAndChannelFromSteveJetPatchAndTriggerPatch(jetpatch,triggerpatch,dsm,chan);
00274 getTriggerPatchFromDsmAndChannel(dsm,chan,triggerpatch2);
00275 }
00276
00277 void EemcTrigUtil::getFeeOutMask(const TDatime& date, int* highTowerMask, int* patchSumMask)
00278 {
00279 char maskdir[FILENAME_MAX];
00280 struct passwd* pw = getpwnam("pibero");
00281 sprintf(maskdir,"%s/public/StarTrigSimuSetup/mask",pw->pw_dir);
00282 vector<TDatime> dates;
00283 scanMaskDirForDates(maskdir,dates);
00284 TDatime timestamp = getTimeStampFromDates(date,dates);
00285 char maskfile[FILENAME_MAX];
00286 sprintf(maskfile,"%s/eec.%02d-%02d-%02d.dat",maskdir,timestamp.GetMonth(),timestamp.GetDay(),timestamp.GetYear()%100);
00287 LOG_INFO << "Using mask file " << maskfile << endm;
00288 getFeeOutMask(maskfile,highTowerMask,patchSumMask);
00289 }
00290
00291 void EemcTrigUtil::getFeeBoardFromSteveTriggerPatch(int triggerpatch, int& board)
00292 {
00293
00294 static const int boardMap[15] = { 1,3,1,1,3,1,2,4,2,2,4,2,3,4,3 };
00295 board = boardMap[triggerpatch-1];
00296 }
00297
00298 void EemcTrigUtil::getFeeBoardMask(const TDatime& date, int* highTower)
00299 {
00300 char maskdir[FILENAME_MAX];
00301 struct passwd* pw = getpwnam("pibero");
00302 sprintf(maskdir,"%s/public/StarTrigSimuSetup/mask",pw->pw_dir);
00303 vector<TDatime> dates;
00304 scanDirForDates(maskdir,dates);
00305 TDatime timestamp = getTimeStampFromDates(date,dates);
00306 char timestampString[12];
00307 sprintf(timestampString,"/%02d.%02d.%4d",timestamp.GetMonth(),timestamp.GetDay(),timestamp.GetYear());
00308 strcat(maskdir,timestampString);
00309 LOG_INFO << "Using mask directory " << maskdir << endm;
00310 for (int jetpatch = 1; jetpatch <= 6; ++jetpatch) {
00311 int boardmask[5];
00312 fill(boardmask,boardmask+5,0);
00313 char maskfile[FILENAME_MAX];
00314 sprintf(maskfile,"%s/tower-%d-current_beam_config.dat",maskdir,jetpatch);
00315 LOG_INFO << "Scanning mask file " << maskfile << endm;
00316 FILE* fp = fopen(maskfile,"r");
00317 if (fp) {
00318 int value;
00319 char name[100];
00320 while (fscanf(fp,"%x %s\n",&value,name) != EOF) {
00321 if (strcmp(name,"board1mask") == 0) boardmask[0] = value;
00322 if (strcmp(name,"board2mask") == 0) boardmask[1] = value;
00323 if (strcmp(name,"board3mask") == 0) boardmask[2] = value;
00324 if (strcmp(name,"board4mask") == 0) boardmask[3] = value;
00325 if (strcmp(name,"board5mask") == 0) boardmask[4] = value;
00326 }
00327 }
00328 fclose(fp);
00329 for (int triggerpatch = 1; triggerpatch <= 15; ++triggerpatch) {
00330 int board = 0;
00331 getFeeBoardFromSteveTriggerPatch(triggerpatch,board);
00332 if (boardmask[board-1]) {
00333 int triggerpatch2;
00334 getTriggerPatchFromSteveJetPatchAndTriggerPatch(jetpatch,triggerpatch,triggerpatch2);
00335 highTower[triggerpatch2] = 0;
00336 LOG_INFO << Form("JP=%d TP=%d Brd=%d (%d)",jetpatch,triggerpatch,board,triggerpatch2) << endm;
00337 }
00338 }
00339 }
00340 }
00341
00342
00343
00344
00345 void
00346 EemcTrigUtil::genPed4(int ped4val, int mxChan, int *feePed4){
00347 for (int crate=1; crate<=6; crate++){
00348 for (int board=1; board<=4; board++){
00349 for (int chan=0; chan<32; chan++){
00350 int rdo = (crate-1)*mxChan+(board-1)*32+chan;
00351 feePed4[rdo]=ped4val;
00352 LOG_DEBUG << Form("crate=%d board=%d chan=%d rdo=%d ped4=%d",crate,board,chan,rdo,ped4val) << endm;
00353 }
00354 }
00355 }
00356 }
00357
00358
00359
00360
00361 void
00362 EemcTrigUtil::readPed4(const char *path, int mxChan, int *feePed4) {
00363 for (int crate = 1; crate <= 6; ++crate) {
00364 for (int board = 1; board <= 4; ++board) {
00365 char fname[FILENAME_MAX];
00366 sprintf(fname,"%s/crate%dboard%d.ped4",path,crate,board);
00367 FILE* fd = fopen(fname,"r");
00368 if (!fd) {
00369 LOG_ERROR << "Could not open " << fname << " for reading" << endm;
00370 continue;
00371 }
00372 LOG_DEBUG << "Reading " << fname << endm;
00373 int chan;
00374 for (chan = 0; chan < 32; ++chan) {
00375 int ped4;
00376 if (fscanf(fd,"%d",&ped4) == EOF) break;
00377 int rdo = (crate-1)*mxChan+(board-1)*32+chan;
00378 feePed4[rdo] = ped4;
00379 LOG_DEBUG << Form("crate=%d board=%d chan=%d rdo=%d ped4=%d",crate,board,chan,rdo,ped4) << endm;
00380 }
00381 LOG_DEBUG << "Read " << chan << " channels" << endm;
00382 fclose(fd);
00383 }
00384 }
00385 }