StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StTriggerSimuMaker.cxx
1 //
3 //
4 // StTriggerSimuMaker R.Fatemi, Adam Kocoloski , Jan Balewski (Fall, 2007)
5 //
6 // Goal: generate trigger response based on ADC
7 // implemented BEMC,EEMC,....
8 // >StTriggerSimu/*SUB*/St*SUB*TriggerSimu.h
9 // >where *SUB* are the subsystems: Eemc, Bemc, Bbc,....
10 // > L2 is served by a separate maker
11 //
13 
14 //<<<<<<< StTriggerSimuMaker.cxx
15 // $Id: StTriggerSimuMaker.cxx,v 1.67 2020/04/12 03:56:24 zchang Exp $
16 //=======
17 // $Id: StTriggerSimuMaker.cxx,v 1.67 2020/04/12 03:56:24 zchang Exp $
18 //>>>>>>> 1.61
19 
20 // MySQL C API
21 //#include "mysql.h"
22 #include <mysql/mysql.h>
23 //search user id for database
24 #include <sys/types.h>
25 #include <pwd.h>
26 
27 // DSM crates
28 #include "RTS/trg/include/trgConfNum.h"
29 #include "StDSMUtilities/StDSM2009Utilities.hh"
30 
31 #include <Stiostream.h>
32 #include "StChain.h"
33 #include "TFile.h"
34 #include <math.h>
35 
36 #include <fstream>
37 #include "StEmcUtil/database/StEmcDecoder.h"
38 
39 #include "St_db_Maker/St_db_Maker.h" // just for time stamp
40 #include "Eemc/EMCdsm2Tree.h"// to access Etot
41 
42 //StEvent
43 #include "StEvent/StEvent.h"
44 
45 //get EEMC
46 #include "Eemc/StEemcTriggerSimu.h"
47 #include "Eemc/EemcHttpInfo.h"
48 #include "Eemc/EEfeeTPTree.h" // for printouts only
49 #include "Eemc/EEfeeTP.h" // for printouts only
50 
51 //get BEMC
52 #include "StTriggerUtilities/Bemc/StBemcTriggerSimu.h"
53 #include "StEmcRawMaker/StBemcTables.h"
54 #include "StEmcUtil/geometry/StEmcGeom.h"
55 
56 //get EMC
57 #include "StTriggerUtilities/Emc/StEmcTriggerSimu.h"
58 
59 //get BBC
60 #include "Bbc/StBbcTriggerSimu.h"
61 
62 //get L2
63 #include "L2Emulator/StL2TriggerSimu.h"
64 
65 //get HEAD Maker
66 #include "StTriggerSimuMaker.h"
67 #include "StTriggerSimuResult.h"
68 
69 // Trigger definition
70 #include "TBufferFile.h"
71 #include "tables/St_triggerDefinition_Table.h"
72 #include "StTriggerDefinition.h"
73 
74 // Trigger threshold
75 #include "tables/St_triggerThreshold_Table.h"
76 #include "StTriggerThreshold.h"
77 
78 ClassImp(StTriggerSimuMaker)
79 
80 StTriggerSimuMaker::StTriggerSimuMaker(const char *name):StMaker(name) {
81  mYear=-1;
82  mMCflag=0;
83  eemc=0;
84  bbc=0;
85  bemc=0;
86  lTwo=0;
87  mHList=0;
88  emc = new StEmcTriggerSimu;
89  for (int a=0; a<numSimulators; a++){
90  mSimulators[a]=0;
91  }
92 
93  //fill(mBarrelJetPatchTh,mBarrelJetPatchTh+3,-1);
94  fill(mBarrelJetPatchTh,mBarrelJetPatchTh+4,-1);
95  fill(mBarrelHighTowerTh,mBarrelHighTowerTh+4,-1);
96 
97  //fill(mEndcapJetPatchTh,mEndcapJetPatchTh+3,-1);
98  fill(mEndcapJetPatchTh,mEndcapJetPatchTh+4,-1);
99  fill(mEndcapHighTowerTh,mEndcapHighTowerTh+2,-1);
100 
101  fill(mOverlapJetPatchTh,mOverlapJetPatchTh+3,-1);
102 
103  mChangeJPThresh = 0;
104 
105  mUseOnlineDB = 0;
106  mUseOfflineDB = 0;
107 }
108 
109 void StTriggerSimuMaker::useEemc(int flag){
110  eemc=new StEemcTriggerSimu();
111  eemc->setConfig(flag);
112  mSimulators[0]=eemc;
113 }
114 
115 void StTriggerSimuMaker::useBbc(){
116  bbc=new StBbcTriggerSimu;
117  mSimulators[1]=bbc;
118 }
119 
120 void StTriggerSimuMaker::useBemc(){
121  bemc=new StBemcTriggerSimu;
122  bemc->setHeadMaker(this);
123  mSimulators[2]=bemc;
124 }
125 
126 void StTriggerSimuMaker::useL2(StGenericL2Emulator* L2Mk){
127  lTwo=new StL2TriggerSimu(L2Mk);
128  mSimulators[4]=lTwo;
129 
130 }
131 
132 void StTriggerSimuMaker::useL2(StGenericL2Emulator2009* L2Mk){
133  lTwo=new StL2TriggerSimu(L2Mk);
134  mSimulators[4]=lTwo;
135 
136 }
137 
138 Int_t StTriggerSimuMaker::Init() {
139  LOG_INFO <<Form("StTriggerSimuMaker::Init(), MCflag=%d",mMCflag)<<endm;
140 
141  if (GetMode() == 10) {
142  // Set parameters necessary for filling PicoDst in BFC
143  setMC(false);
144  useBemc();
145  useEemc();
146  useOfflineDB();
147  bemc->setConfig(StBemcTriggerSimu::kOffline);
148  }
149 
150  if(eemc) {
151  eemc->setHList(mHList);
152  }
153 
154  if(bemc) {
155  bemc->setHList(mHList);
156  }
157 
158  for(Int_t i=0; i<numSimulators; i++) {
159  if(mSimulators[i]){
160  mSimulators[i]->setMC(mMCflag);
161  mSimulators[i]->Init();
162  }
163 
164  }
165 
166  return StMaker::Init();
167 }
168 
169 void StTriggerSimuMaker::Clear(const Option_t*){
170  LOG_DEBUG<<"StTriggerSimuMaker::Clear()"<<endm;
171 
172  for(Int_t i=0; i<numSimulators; i++) {
173  if (mSimulators[i]){
174  mSimulators[i]->Clear();
175  }
176  }
177 
178  mResults.clear();
179 }
180 
181 Int_t StTriggerSimuMaker::InitRun(int runNumber) {
182 
183  assert(runNumber != -1);
184  const TDatime& dbTime = GetDBTime();
185  mYear = dbTime.GetYear();
186  LOG_INFO << "runNumber=" << runNumber << " with DB timestamp " << dbTime.AsSQLString() << endm;
187 
188  for (Int_t i = 0; i < numSimulators; ++i)
189  if (mSimulators[i])
190  mSimulators[i]->InitRun(runNumber);
191  //Use unified EMC trigger for EEMC/BEMC triggers in year 2009 or later
192  if (mYear >= 2009 && (mSimulators[0] || mSimulators[2])) {
193  emc->setHeadMaker(this);
194  emc->setBemc(bemc);
195  emc->setEemc(eemc);
196  emc->setMC(mMCflag);
197  emc->setYear(mYear);
198  emc->InitRun(runNumber); //init run for emc
199  LOG_INFO<<Form("set year %d for emc trigger definition", mYear)<<endm;
200  mSimulators[3] = emc;
201  if (!mUseOnlineDB && !mUseOfflineDB) {
202  LOG_ERROR << "!!! ATTENTION !!! YOU MUST SPECIFY WHICH DATABASE TO USE FOR TRIGGER DEFINITIONS AND THRESHOLDS:" << endm;
203  LOG_ERROR << "StTriggerSimuMaker::useOnlineDB()" << endm;
204  LOG_ERROR << "StTriggerSimuMaker::useOfflineDB()" << endm;
205  assert(mUseOnlineDB || mUseOfflineDB);
206  }
207  assert((mUseOnlineDB && get2009DsmRegistersFromOnlineDatabase (runNumber)) ||
208  (mUseOfflineDB && get2009DsmRegistersFromOfflineDatabase(runNumber)));
209  //LOG_INFO << "Overwriting the following registers:" << endm;
210  //overwrite2009DsmRegisters();
211  if (mChangeJPThresh) {
212  LOG_INFO << "Shift the following registers by " << mChangeJPThresh << ":" << endm;
213  changeJetPatchTh();
214  }
215  if(mYear == 2017){
216  setTriggerThresholds2017();
217  }
218  }
219 
220  return kStOK;
221 }
222 
224 
225  for(Int_t i=0; i<numSimulators; i++) {
226  if (mSimulators[i]){
227  mSimulators[i]->Make();
228  }
229  }
230 
231  vector<int> trigIds = triggerIds();
232  TString line = "Triggers: ";
233  for (size_t i = 0; i < trigIds.size(); ++i) {
234  line += Form("%d ",trigIds[i]);
235  buildDetailedResult(trigIds[i]);
236  }
237  LOG_DEBUG << line << endm;
238 
239  return kStOK;
240 }
241 
242 bool StTriggerSimuMaker::isTrigger(int trigId) {
243 
244  for(Int_t i=0; i<numSimulators; i++) {
245  if (mSimulators[i] && mSimulators[i]->triggerDecision(trigId) == kNo) return false;
246  }
247 
248  return true;
249 }
250 
251 vector<int> StTriggerSimuMaker::triggerIds() const
252 {
253  vector<int> v;
254 
255  if (mYear < 2009) {
256  if (bemc) copy(bemc->triggerIds().begin(),bemc->triggerIds().end(),back_inserter(v));
257  if (eemc) copy(eemc->triggerIds().begin(),eemc->triggerIds().end(),back_inserter(v));
258  }
259  else {
260  set<int> s = emc->triggerIds();
261  copy(s.begin(),s.end(),back_inserter(v));
262  }
263 
264  return v;
265 }
266 
267 bool StTriggerSimuMaker::isTriggerDefined(int trigId)
268 {
269 
270  bool TrigDefined;
271 
272  TrigDefined = false;
273 
274  if (mYear < 2009) {
275  if (bemc) {
276  vector<int> b = bemc->triggerIds();
277  for (size_t i = 0; i < b.size(); ++i) {
278  if (trigId==b[i]) TrigDefined=true;
279  }
280  }
281  if (eemc) {
282  vector<int> e = bemc->triggerIds();
283  for (size_t i = 0; i < e.size(); ++i) {
284  if (trigId==e[i]) TrigDefined=true;
285  }
286  }
287  }
288  else {
289  set<int> s = emc->triggerIds();
290  if (s.count(trigId)>0) TrigDefined = true;
291  }
292 
293 
294  return TrigDefined;
295 }
296 
297 
298 void StTriggerSimuMaker::buildDetailedResult(int trigId) {
299  StTriggerSimuResult& result = mResults[trigId];
300  result.setTriggerId(trigId);
301  if(bbc) {
302  result.setBbcDecision(bbc->triggerDecision(trigId));
303  }
304  if(bemc) {
305  result.setBemcDecision(bemc->triggerDecision(trigId));
306 
307  if(bemc->triggerDecision(trigId)==1) {
308  // Record HTs above Threshold
309  vector< pair<int,int> > Towers=bemc->getTowersAboveThreshold(trigId);
310  for(vector< pair<int,int> >::iterator itr=Towers.begin(); itr!=Towers.end(); itr++){
311  result.addHighTower((*itr).first,(*itr).second);
312  }
313  // Record TPs above Threshold
314  vector< pair<int,int> > tPatches=bemc->getTriggerPatchesAboveThreshold(trigId);
315  for(vector< pair<int,int> >::iterator itr=tPatches.begin(); itr!=tPatches.end(); itr++){
316  result.addTriggerPatch((*itr).first,(*itr).second);
317  }
318  // Record JPs above Threshold
319  vector< pair<int,int> > jPatches=bemc->getJetPatchesAboveThreshold(trigId);
320  for(vector< pair<int,int> >::iterator itr=jPatches.begin(); itr!=jPatches.end(); itr++){
321  result.addJetPatch((*itr).first,(*itr).second);
322  }
323  }
324  }
325  if(eemc) {
326  result.setEemcDecision(eemc->triggerDecision(trigId));
327  }
328  if (emc) {
329  }
330  if(lTwo) {
331  result.setL2Decision(lTwo->triggerDecision(trigId));
332  result.setL2Result(lTwo->result());
333  }
334 }
335 
337  return StMaker::Finish();
338 }
339 
340 bool StTriggerSimuMaker::get2009DsmRegistersFromOfflineDatabase(int runNumber)
341 {
342  return getTriggerDefinitions(runNumber) && getTriggerThresholds(runNumber);
343 }
344 
345 bool StTriggerSimuMaker::getTriggerDefinitions(int runNumber)
346 {
347  TDataSet* DB = GetInputDB("Calibrations/trg");
348  if (DB) {
349  St_triggerDefinition* desc = dynamic_cast<St_triggerDefinition*>(DB->Find("triggerDefinition"));
350  if (desc) {
351  LOG_INFO << "Using BEMC offline database for trigger definitions" << endm;
352  triggerDefinition_st* table = desc->GetTable();
353  LOG_INFO << Form("%s\n", table[0].comments) << endm;
354  LOG_INFO << setw(20) << "triggerIndex"
355  << setw(20) << "name"
356  << setw(20) << "triggerId"
357  << setw(20) << "onbits"
358  << setw(20) << "offbits"
359  << setw(20) << "onbits1"
360  << setw(20) << "onbits2"
361  << setw(20) << "onbits3"
362  << setw(20) << "offbits1"
363  << setw(20) << "offbits2"
364  << setw(20) << "offbits3"
365  << endm;
366  TBufferFile buf(TBuffer::kRead);
367  buf.SetBuffer(table[0].trigdef,sizeof(table[0].trigdef),false);
368  TObjArray* a = 0;
369  buf >> a;
370  for (int i = 0; i < a->GetEntriesFast(); ++i) {
371  StTriggerDefinition* trigdef = dynamic_cast<StTriggerDefinition*>(a->At(i));
372  if(!trigdef) continue;
373  LOG_INFO << setw(20) << trigdef->triggerIndex
374  << setw(20) << trigdef->name
375  << setw(20) << trigdef->triggerId
376  << setw(20) << Form("0x%08x", trigdef->onbits)
377  << setw(20) << Form("0x%08x", trigdef->offbits)
378  << setw(20) << Form("0x%08x", trigdef->onbits1)
379  << setw(20) << Form("0x%08x", trigdef->onbits2)
380  << setw(20) << Form("0x%08x", trigdef->onbits3)
381  << setw(20) << Form("0x%08x", trigdef->offbits1)
382  << setw(20) << Form("0x%08x", trigdef->offbits2)
383  << setw(20) << Form("0x%08x", trigdef->offbits3)
384  << endm;
385  TriggerDefinition trigDef;
386  trigDef.triggerIndex = trigdef->triggerIndex;
387  strcpy(trigDef.name,trigdef->name.Data());
388  trigDef.triggerId = trigdef->triggerId;
389  trigDef.onbits = trigdef->onbits;
390  trigDef.offbits = trigdef->offbits;
391  trigDef.onbits1 = trigdef->onbits1;
392  trigDef.onbits2 = trigdef->onbits2;
393  trigDef.onbits3 = trigdef->onbits3;
394  trigDef.offbits1 = trigdef->offbits1;
395  trigDef.offbits2 = trigdef->offbits2;
396  trigDef.offbits3 = trigdef->offbits3;
397 
398  emc->defineTrigger(trigDef);
399  }
400  a->Delete();
401  return true;
402  }
403  }
404  return false;
405 }
406 void StTriggerSimuMaker::setTriggerThresholds2017()
407 {
408  LOG_INFO<<"Resetting the following BEMC DSM thresholds for year 2017"<<endm;
409  LOG_INFO<<Form("BEMC-EEMC-overlap-JP-th0: %d", 30)<<endm;
410  LOG_INFO<<Form("BEMC-EEMC-overlap-JP-th1: %d", 43)<<endm;
411  LOG_INFO<<Form("BEMC-EEMC-overlap-JP-th2: %d", 66)<<endm;
412  LOG_INFO<<Form("EMC-HTTP-Sel: %d", 0)<<endm;
413 
414  //set BEMC EM201 registers
415  emc->get2009_DSMLayer2_Result()->setRegister(0,30);
416  emc->get2009_DSMLayer2_Result()->setRegister(1,43);
417  emc->get2009_DSMLayer2_Result()->setRegister(2,66);
418  emc->get2009_DSMLayer2_Result()->setRegister(3,0);
419  //crateAt(1).boardAt(20<<24).registerAt((0%100)<<24) = 30;
420  //crateAt(1).boardAt(20<<24).registerAt((1%100)<<24) = 43;
421  //crateAt(1).boardAt(20<<24).registerAt((2%100)<<24) = 66;
422  //set EE101 and EE102 thresholds
423  LOG_INFO<<Form("EEMC-JP-th0: %d", 30)<<endm;
424  LOG_INFO<<Form("EEMC-JP-th1: %d", 43)<<endm;
425  LOG_INFO<<Form("EEMC-JP-th2: %d", 66)<<endm;
426 
427  eemc->get2009_DSMLayer1_Result()->setRegister(0, 30);
428  eemc->get2009_DSMLayer1_Result()->setRegister(1, 43);
429  eemc->get2009_DSMLayer1_Result()->setRegister(2, 66);
430  //crateAt(2).boardAt(21<<24).registerAt((0%100)<<24) = 30;
431  //crateAt(2).boardAt(21<<24).registerAt((1%100)<<24) = 43;
432  //crateAt(2).boardAt(21<<24).registerAt((2%100)<<24) = 66;
433  //crateAt(2).boardAt(22<<24).registerAt((0%100)<<24) = 30;
434  //crateAt(2).boardAt(22<<24).registerAt((1%100)<<24) = 43;
435  //crateAt(2).boardAt(22<<24).registerAt((2%100)<<24) = 66;
436  LOG_INFO<<Form("BEMC-HT-th1: %d", 18)<<endm;
437  LOG_INFO<<Form("BEMC-HT-th2: %d", 25)<<endm;
438  LOG_INFO<<Form("BEMC-HT-th3: %d", 31)<<endm;
439  LOG_INFO<<Form("BEMC-HT-UPC %d", 3)<<endm;
440  LOG_INFO<<Form("BEMC-TP-UPC: %d", 3)<<endm;
441  //BEMC west
442  for (int dsm = 0; dsm < 15; ++dsm){
443  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[0] = 18;
444  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[1] = 25;
445  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[2] = 31;
446  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[3] = 3;
447  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[4] = 3;
448  }
449  //for(int i = 0; i < 15; i++){
450  //crateAt(5).boardAt((16+i)<<24).registerAt((0%100)<<24) = 18;
451  //crateAt(5).boardAt((16+i)<<24).registerAt((1%100)<<24) = 25;
452  //crateAt(5).boardAt((16+i)<<24).registerAt((2%100)<<24) = 31;
453  //crateAt(5).boardAt((16+i)<<24).registerAt((3%100)<<24) = 3;
454  //crateAt(5).boardAt((16+i)<<24).registerAt((4%100)<<24) = 3;
455  //}
456  //BEMC east
457  for (int dsm = 15; dsm < 30; ++dsm){
458  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[0] = 18;
459  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[1] = 25;
460  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[2] = 31;
461  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[3] = 3;
462  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[4] = 3;
463  }
464  //for(int i = 0; i < 15; i++){
465  //crateAt(6).boardAt((16+i)<<24).registerAt((0%100)<<24) = 18;
466  //crateAt(6).boardAt((16+i)<<24).registerAt((1%100)<<24) = 25;
467  //crateAt(6).boardAt((16+i)<<24).registerAt((2%100)<<24) = 31;
468  //crateAt(6).boardAt((16+i)<<24).registerAt((3%100)<<24) = 3;
469  //crateAt(6).boardAt((16+i)<<24).registerAt((4%100)<<24) = 3;
470  //}
471  //eemc->get2009_DSMLayer0_Result()->setRegister(0, 25);
472  //eemc->get2009_DSMLayer0_Result()->setRegister(1, 31);
473  //eemc->get2009_DSMLayer0_Result()->setRegister(2, 5);
474  //eemc->get2009_DSMLayer0_Result()->setRegister(3, 5);
475 }
476 bool StTriggerSimuMaker::getTriggerThresholds(int runNumber)
477 {
478  TDataSet* DB = GetInputDB("Calibrations/trg");
479  if (DB) {
480  St_triggerThreshold* desc = dynamic_cast<St_triggerThreshold*>(DB->Find("triggerThreshold"));
481  if (desc) {
482  LOG_INFO << "Using BEMC offline database for trigger thresholds" << endm;
483  triggerThreshold_st* table = desc->GetTable();
484  LOG_INFO << Form("%s\n", table[0].comments) << endm;
485 
486  LOG_INFO << setw(20) << "object"
487  << setw(20) << "index"
488  << setw(20) << "reg"
489  << setw(20) << "label"
490  << setw(20) << "value"
491  << setw(20) << "defaultvalue"
492  << endm;
493  TBufferFile buf(TBuffer::kRead);
494  buf.SetBuffer(table[0].trigthr,sizeof(table[0].trigthr),false);
495  TObjArray* a = 0;
496  buf >> a;
497  for (int i = 0; i < a->GetEntriesFast(); ++i) {
498  StTriggerThreshold* trigthr = dynamic_cast<StTriggerThreshold*>(a->At(i));
499  if(!trigthr) continue;
500  LOG_INFO << setw(20) << trigthr->object
501  << setw(20) << trigthr->index
502  << setw(20) << trigthr->reg
503  << setw(20) << trigthr->label
504  << setw(20) << trigthr->value
505  << setw(20) << trigthr->defaultvalue
506  << endm;
507  int value = trigthr->value;
508  if (value == -1) value = trigthr->defaultvalue;
509  switch (trigthr->object) {
510  case L1_CONF_NUM:
511  switch (trigthr->index) {
512  case 20: // EM201
513  emc->get2009_DSMLayer2_Result()->setRegister(trigthr->reg,value);
514  break;
515  case 30: // LD301
516  emc->get2009_DSMLayer3_Result()->setRegister(trigthr->reg,value);
517  break;
518  }
519  break;
520  case BC1_CONF_NUM:
521  switch (trigthr->index) {
522  case 21: // EE101
523  eemc->get2009_DSMLayer1_Result()->setRegister(trigthr->reg,value);
524  break;
525  case 23: // EE001
526  eemc->get2009_DSMLayer0_Result()->setRegister(trigthr->reg,value);
527  break;
528  case 33: // BC101
529  bemc->get2009_DSMLayer1_Result()->setRegister(trigthr->reg,value);
530  break;
531  }
532  break;
533  case BCW_CONF_NUM:
534  switch (trigthr->index) {
535  case 16: // BW001
536  for (int dsm = 0; dsm < 15; ++dsm)
537  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[trigthr->reg] = value;
538  break;
539  }
540  break;
541  case BCE_CONF_NUM:
542  switch (trigthr->index) {
543  case 16: // BE001
544  for (int dsm = 15; dsm < 30; ++dsm)
545  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[trigthr->reg] = value;
546  break;
547  }
548  break;
549  }
550  }
551  a->Delete();
552  return true;
553  }
554  }
555  return false;
556 }
557 
558 bool StTriggerSimuMaker::get2009DsmRegistersFromOnlineDatabase(int runNumber)
559 {
560  // Open connection to online database
561 
562  MYSQL mysql;
563  const char* host = "db04.star.bnl.gov";
564  const char* user = "";
565  const char* pass = "";
566 
567  unsigned int port = 3400+mYear%100-1;
568  const char* database = "Conditions_rts";
569  const char* unix_socket = NULL;
570  unsigned long client_flag = 0;
571  char query[1024];
572 
573  struct passwd *login;
574  login = getpwuid(geteuid());
575  user = login->pw_name;
576 
577  LOG_INFO << Form("host=%s user=\"%s\" pass=\"%s\" port=%d database=%s",host,user,pass,port,database) << endm;
578 
579  mysql_init(&mysql);
580 
581  if (!mysql_real_connect(&mysql,host,user,pass,database,port,unix_socket,client_flag)) {
582  LOG_WARN << "Can't connect to database: " << mysql_error(&mysql) << endm;
583  return false;
584  }
585 
586  // For simulation (1) and embedding (2), get run number from DB time stamp
587 
588  if (mMCflag == 1 || mMCflag == 2) {
589  //query = Form("select idx_rn from triggers where beginTime >= '%s' limit 1",GetDBTime().AsSQLString());
590  sprintf(query,"select max(idx_rn) from triggers where beginTime <= '%s'",GetDBTime().AsSQLString());
591  LOG_INFO << query << endm;
592  mysql_query(&mysql,query);
593 
594  if (MYSQL_RES* result = mysql_store_result(&mysql)) {
595  while (MYSQL_ROW row = mysql_fetch_row(result)) {
596  runNumber = atoi(row[0]);
597  }
598  }
599  LOG_INFO << "DB Time = " << GetDBTime().AsSQLString() << endm;
600  LOG_INFO << "Run Number = " << runNumber << endm;
601  }
602 
603  LOG_INFO << "Using BEMC online database" << endm;
604 
605  // object=DSM crate, idx=DSM board
606  sprintf(query,"select object,idx,reg,label,value,defaultvalue from dict where hash=(select dicthash from run where idx_rn = %d)",runNumber);
607  LOG_INFO << query << endm;
608  mysql_query(&mysql,query);
609 
610  if (MYSQL_RES* result = mysql_store_result(&mysql)) {
611  LOG_INFO << setw(10) << "object"
612  << setw(10) << "idx"
613  << setw(10) << "reg"
614  << setw(30) << "label"
615  << setw(10) << "value"
616  << setw(15) << "defaultvalue"
617  << endm;
618 
619  while (MYSQL_ROW row = mysql_fetch_row(result)) {
620  int object = atoi(row[0]);
621  int idx = atoi(row[1]);
622  int reg = atoi(row[2]);
623  TString label = row[3];
624  int value = atoi(row[4]);
625  int defaultvalue = atoi(row[5]);
626 
627  LOG_INFO << setw(10) << object
628  << setw(10) << idx
629  << setw(10) << reg
630  << setw(30) << label
631  << setw(10) << value
632  << setw(15) << defaultvalue
633  << endm;
634 
635  if (value == -1) value = defaultvalue;
636  if(mYear >= 2009 && mYear <= 2013)
637  {
638  switch (object) {
639  case L1_CONF_NUM:
640  switch (idx) {
641  case 20: // EM201
642  emc->get2009_DSMLayer2_Result()->setRegister(reg,value);
643  break;
644  case 30: // LD301
645  emc->get2009_DSMLayer3_Result()->setRegister(reg,value);
646  break;
647  }
648  break;
649  case BC1_CONF_NUM:
650  switch (idx) {
651  case 21: // EE101
652  eemc->get2009_DSMLayer1_Result()->setRegister(reg,value);
653  break;
654  case 23: // EE001
655  eemc->get2009_DSMLayer0_Result()->setRegister(reg,value);
656  break;
657  case 33: // BC101
658  bemc->get2009_DSMLayer1_Result()->setRegister(reg,value);
659  break;
660  }
661  break;
662  case BCW_CONF_NUM:
663  switch (idx) {
664  case 16: // BW001
665  for (int dsm = 0; dsm < 15; ++dsm)
666  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[reg] = value;
667  break;
668  }
669  break;
670  case BCE_CONF_NUM:
671  switch (idx) {
672  case 16: // BE001
673  for (int dsm = 15; dsm < 30; ++dsm)
674  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[reg] = value;
675  break;
676  }
677  break;
678  }
679  }
680 
681  else if(mYear >= 2013)
682  {
683  switch (object) {
684  case L1_CONF_NUM:
685  switch (idx) {
686  case 20: // EM201
687  emc->get2009_DSMLayer2_Result()->setRegister(reg,value);
688  break;
689  case 30: // LD301
690  emc->get2009_DSMLayer3_Result()->setRegister(reg,value);
691  break;
692  }
693  break;
694  case BC1_CONF_NUM:
695  switch (idx) {
696  case 21: // EE101
697  eemc->get2009_DSMLayer1_Result()->setRegister(reg,value);
698  break;
699  case 23: // EE001
700  eemc->get2009_DSMLayer0_Result()->setRegister(reg,value);
701  break;
702  case 33: // BC101
703  bemc->get2009_DSMLayer1_Result()->setRegister(reg,value);
704  break;
705  }
706  break;
707  case BCW_CONF_NUM:
708  switch (idx) {
709  case 16: // BW001
710  for (int dsm = 0; dsm < 15; ++dsm)
711  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[reg] = value;
712  break;
713  }
714  break;
715  case BCE_CONF_NUM:
716  switch (idx) {
717  case 16: // BE001
718  for (int dsm = 15; dsm < 30; ++dsm)
719  (*bemc->get2009_DSMLayer0_Result())[dsm].registers[reg] = value;
720  break;
721  }
722  break;
723  }
724  }
725 
726  }
727  mysql_free_result(result);
728  }
729 
730  // LOG_INFO << "The following registers have new values:" << endm;
731 
732  // Trigger definitions
733  const int MAX_TRIGGERS = 64;
734  TriggerDefinition triggers[MAX_TRIGGERS];
735 
736  sprintf(query,"select idx_trigger,name,offlineBit from triggers where idx_rn = %d",runNumber);
737  LOG_INFO << query << endm;
738  mysql_query(&mysql,query);
739 
740  if (MYSQL_RES* result = mysql_store_result(&mysql)) {
741  while (MYSQL_ROW row = mysql_fetch_row(result)) {
742  int idx_trigger = atoi(row[0]);
743  assert(idx_trigger >= 0 && idx_trigger < MAX_TRIGGERS);
744  triggers[idx_trigger].triggerIndex = idx_trigger;
745  strcpy(triggers[idx_trigger].name,row[1]);
746  triggers[idx_trigger].triggerId = atoi(row[2]);
747  }
748  mysql_free_result(result);
749  }
750 
751  sprintf(query,"select idx_idx,onbits,offbits,onbits1,onbits2,onbits3,offbits1,offbits2,offbits3 from pwc where idx_rn = %d",runNumber);
752  LOG_INFO << query << endm;
753  mysql_query(&mysql,query);
754 
755  if (MYSQL_RES* result = mysql_store_result(&mysql)) {
756  LOG_INFO << setw(20) << "idx_trigger"
757  << setw(20) << "name"
758  << setw(20) << "offlineBit"
759  << setw(20) << "onbits"
760  << setw(20) << "offbits"
761  << setw(20) << "onbits1"
762  << setw(20) << "onbits2"
763  << setw(20) << "onbits3"
764  << setw(20) << "offbits1"
765  << setw(20) << "offbits2"
766  << setw(20) << "offbits3"
767  << endm;
768 
769  int idx_trigger;
770 
771  while (MYSQL_ROW row = mysql_fetch_row(result)) {
772  // int idx_trigger = atoi(row[0]);
773  sscanf(row[0],"%d",&idx_trigger);
774  assert(idx_trigger >= 0 && idx_trigger < MAX_TRIGGERS);
775  //use sscanf(...) converting char* to unsigned integer instead of using atoi(...) Z.Chang
776  // triggers[idx_trigger].onbits = atoi(row[1]);
777  sscanf(row[1],"%ud",&triggers[idx_trigger].onbits);
778  sscanf(row[2],"%ud",&triggers[idx_trigger].offbits);
779  if(row[3]) sscanf(row[3],"%ud",&triggers[idx_trigger].onbits1);
780  if(row[4]) sscanf(row[4],"%ud",&triggers[idx_trigger].onbits2);
781  if(row[5]) sscanf(row[5],"%ud",&triggers[idx_trigger].onbits3);
782  if(row[6]) sscanf(row[6],"%ud",&triggers[idx_trigger].offbits1);
783  if(row[7]) sscanf(row[7],"%ud",&triggers[idx_trigger].offbits2);
784  if(row[8]) sscanf(row[8],"%ud",&triggers[idx_trigger].offbits3);
785 
786  LOG_INFO << setw(20) << idx_trigger
787  << setw(20) << triggers[idx_trigger].name
788  << setw(20) << triggers[idx_trigger].triggerId
789  << setw(20) << Form("0x%08x",triggers[idx_trigger].onbits)
790  << setw(20) << Form("0x%08x",triggers[idx_trigger].offbits)
791  << setw(20) << Form("0x%08x",triggers[idx_trigger].onbits1)
792  << setw(20) << Form("0x%08x",triggers[idx_trigger].onbits2)
793  << setw(20) << Form("0x%08x",triggers[idx_trigger].onbits3)
794  << setw(20) << Form("0x%08x",triggers[idx_trigger].offbits1)
795  << setw(20) << Form("0x%08x",triggers[idx_trigger].offbits2)
796  << setw(20) << Form("0x%08x",triggers[idx_trigger].offbits3)
797  << endm;
798  emc->defineTrigger(triggers[idx_trigger]);
799  }
800  mysql_free_result(result);
801  }
802 
803  mysql_close(&mysql);
804 
805  return true;
806 }
807 
808 void StTriggerSimuMaker::overwrite2009DsmRegisters()
809 {
810  for (int reg = 0; reg < 3; ++reg) {
811  int value = mBarrelJetPatchTh[reg];
812  if (value != -1) {
813  LOG_INFO << setw(20) << reg
814  << setw(30) << "BEMC-JP-th" << reg
815  << setw(20) << value
816  << endm;
817  bemc->get2009_DSMLayer1_Result()->setRegister(reg,value);
818  }
819  }
820 
821  for (int reg = 0; reg < 4; ++reg) {
822  int value = mBarrelHighTowerTh[reg];
823  if (value != -1) {
824  LOG_INFO << setw(20) << reg
825  << setw(30) << "BEMC-HT-th" << reg
826  << setw(20) << value
827  << endm;
828  bemc->get2009_DSMLayer0_Result()->setRegister(reg,value);
829  }
830  }
831 
832  for (int reg = 0; reg < 3; ++reg) {
833  int value = mEndcapJetPatchTh[reg];
834  if (value != -1) {
835  LOG_INFO << setw(20) << reg
836  << setw(30) << "EEMC-JP-th" << reg
837  << setw(20) << value
838  << endm;
839  eemc->get2009_DSMLayer1_Result()->setRegister(reg,value);
840  }
841  }
842 
843  for (int reg = 0; reg < 2; ++reg) {
844  int value = mEndcapHighTowerTh[reg];
845  if (value != -1) {
846  LOG_INFO << setw(20) << reg
847  << setw(30) << "EEMC-HT-th" << reg
848  << setw(20) << value
849  << endm;
850  eemc->get2009_DSMLayer0_Result()->setRegister(reg,value);
851  }
852  }
853 
854  for (int reg = 0; reg < 3; ++reg) {
855  int value = mOverlapJetPatchTh[reg];
856  if (value != -1) {
857  LOG_INFO << setw(20) << reg
858  << setw(30) << "BEMC-EEMC-overlap-JP-th" << reg
859  << setw(20) << value
860  << endm;
861  emc->get2009_DSMLayer2_Result()->setRegister(reg,value);
862  }
863  }
864 }
865 
866 void StTriggerSimuMaker::changeJetPatchTh()
867 {
868  for (int reg = 0; reg < 3; ++reg) {
869  int value = bemc->get2009_DSMLayer1_Result()->getRegister(reg);
870  value += mChangeJPThresh;
871  LOG_INFO << setw(20) << reg
872  << setw(30) << "BEMC-JP-th" << reg
873  << setw(20) << value
874  << endm;
875  bemc->get2009_DSMLayer1_Result()->setRegister(reg,value);
876  }
877 
878  for (int reg = 0; reg < 3; ++reg) {
879  int value = eemc->get2009_DSMLayer1_Result()->getRegister(reg);
880  value += mChangeJPThresh;
881  LOG_INFO << setw(20) << reg
882  << setw(30) << "EEMC-JP-th" << reg
883  << setw(20) << value
884  << endm;
885  eemc->get2009_DSMLayer1_Result()->setRegister(reg,value);
886  }
887 
888  for (int reg = 0; reg < 3; ++reg) {
889  int value = emc->get2009_DSMLayer2_Result()->getRegister(reg);
890  value += mChangeJPThresh;
891  LOG_INFO << setw(20) << reg
892  << setw(30) << "BEMC-EEMC-overlap-JP-th" << reg
893  << setw(20) << value
894  << endm;
895  emc->get2009_DSMLayer2_Result()->setRegister(reg,value);
896  }
897 }
898 
899 void StTriggerSimuMaker::setLastDsmRegister(int reg, int value)
900 {
901  emc->get2009_DSMLayer3_Result()->setRegister(reg,value);
902 }
903 
904 /*****************************************************************************
905  * $Log: StTriggerSimuMaker.cxx,v $
906  * Revision 1.67 2020/04/12 03:56:24 zchang
907  * Add EMC DSM algorithm for the 2017 pp run
908  *
909  * Revision 1.66 2020/01/13 20:45:47 zchang
910  * removing old run13 dsm algo files
911  *
912  * Revision 1.65 2017/12/29 16:54:07 zchang
913  * remove direct query to STAR database server in InitRun(int), users need to make sure that the trigger simulator retrives correct run number
914  *
915  * Revision 1.64 2017/12/28 21:14:45 zchang
916  * switch database server from dbbak.starp.bnl.gov to db04.star.bnl.gov
917  *
918  * Revision 1.63 2017/06/09 20:11:25 zchang
919  * Add pointer guard for trigger definition and trigger threshold tables, the pointers can not be null
920  *
921  * Revision 1.62 2017/01/02 15:31:39 rfatemi
922  * Updated by Danny OLVITT for 2013 dijet analysiss
923  *
924 <<<<<<< StTriggerSimuMaker.cxx
925 =======
926  * Revision 1.61 2016/03/18 22:49:38 zchang
927  * updating trigger simulator for run12 analysis
928  *
929  * Revision 1.60 2014/07/31 19:52:17 zchang
930  * change database server back to dbbak.starp.bnl.gov
931  *
932  * Revision 1.59 2014/07/02 02:11:24 zchang
933  * Add conditions to read none-NULL onbits and offbits in database, compatible with early run9 trigger definitions.CVS: ----------------------------------------------------------------------
934  *
935  * Revision 1.58 2013/12/12 18:20:09 zchang
936  * use new database node "db04.star.bnl.gov", wait till it's back.
937  *
938  * Revision 1.57 2013/11/21 20:52:53 zchang
939  * add getpwuid to get user name to access database
940  *
941 >>>>>>> 1.61
942  * Revision 1.53 2012/07/13 16:47:26 pibero
943  * Users must now specify database to use for trigger definitions and thresholds
944  *
945  * Revision 1.52 2012/07/12 16:06:48 pibero
946  * Added LOG_INFO
947  *
948  * Revision 1.51 2012/03/28 02:05:14 pibero
949  * Fix for embedding
950  *
951  * Revision 1.50 2011/12/11 17:16:43 pibero
952  * Add function to set LD301 registers
953  *
954  * Revision 1.49 2011/10/22 18:12:52 pibero
955  * Print whether using offline/offline database
956  *
957  * Revision 1.48 2011/10/17 06:19:11 pibero
958  * Specify online database for each year
959  *
960  * Revision 1.47 2011/10/04 18:29:16 pibero
961  * *** empty log message ***
962  *
963  * Revision 1.46 2011/10/04 15:13:46 pibero
964  * Use system path for mysql.h include file
965  *
966  * Revision 1.45 2011/06/10 18:56:18 pibero
967  * Updated meaning of mMCflag variable: 0=data, 1=simulation, 2=embedding
968  *
969  * Revision 1.44 2010/10/05 15:49:01 rfatemi
970  * Include ability to test if trigger is defined in trigger code for the database timestamp
971  *
972  * Revision 1.43 2010/08/26 15:28:31 pibero
973  * In the newer trigger code, the trigger DB is accessed with the query
974  * "select idx_rn from triggers where beginTime >= '%s' limit 1". The
975  * problem with this query is that it selects the run following the
976  * current time stamp and, unfortunately, production runs aren't always
977  * followed by production runs. So often the query can pick up a
978  * following pedestal run and emulators give up. A more appropriate
979  * query would be "select max(idx_rn) from triggers where beginTime <=
980  * '%s'", which selects the begin time right before the time stamp and
981  * hence the actual current run. I can validate that this works as
982  * desired.
983  *
984  * Michael Betancourt
985  * PhD Candidate
986  * Hadronic Physics Group
987  * Laboratory for Nuclear Science
988  * Massachusetts Institute of Technology
989  *
990  * Revision 1.42 2010/08/13 22:55:18 pibero
991  * Added onbits1-3 and offbits1-3
992  *
993  * Revision 1.41 2010/08/13 22:21:11 pibero
994  * Move from online to offline DB
995  *
996  * Revision 1.40 2010/08/08 21:58:53 pibero
997  * Back to checking decision of individual simulators.
998  *
999  * Revision 1.39 2010/08/03 16:53:30 pibero
1000  * Function isTrigger() checks vector of trigger IDs instead
1001  * of querying individual detectors.
1002  *
1003  * Revision 1.38 2010/06/24 07:51:14 pibero
1004  * Added hooks to overwrite DSM thresholds from the database.
1005  *
1006  * Revision 1.37 2010/05/14 16:36:49 jeromel
1007  * Wrong include path for mysql
1008  *
1009  * Revision 1.36 2010/05/12 23:42:11 pibero
1010  * Changed absolute path for mysql.h to relative path
1011  *
1012  * Revision 1.35 2010/04/17 17:43:40 pibero
1013  * *** empty log message ***
1014  *
1015  * Revision 1.34 2010/04/16 01:47:38 pibero
1016  * Oops, forgot to include triggers before 2009. Thanks, Liaoyuan.
1017  *
1018  * Revision 1.33 2010/03/01 18:48:36 pibero
1019  * More updates for Run 9
1020  *
1021  * Revision 1.32 2010/02/18 20:07:02 pibero
1022  * Run 9 updates
1023  *
1024  * Revision 1.31 2009/11/16 07:51:19 pibero
1025  * Added LOG_DEBUG messages and triggerIds()
1026  *
1027  * Revision 1.30 2009/11/13 20:00:52 pibero
1028  * Updates for Run 9 to work on simulation
1029  *
1030  * Revision 1.29 2009/10/12 18:04:12 pibero
1031  * Moved StEEmcUtil/EEdsm to StTriggerUtilities/Eemc
1032  *
1033  * Revision 1.28 2009/09/26 18:46:28 pibero
1034  * Migration from ROOT MySQL to STAR DB API
1035  *
1036  * Revision 1.27 2009/09/23 22:35:30 pibero
1037  * Removed dependencies on ROOT MySQL
1038  *
1039  * Revision 1.26 2009/09/20 06:46:29 pibero
1040  * Updates for Run 9
1041  *
1042  * Revision 1.25 2009/02/04 20:00:48 rfatemi
1043  * change includes for StEmcDecoder
1044  *
1045  * Revision 1.24 2009/02/03 15:40:38 rfatemi
1046  * Changed structure of mSimulators to accomodate 2009 EMC simulator update
1047  *
1048  * Revision 1.23 2009/01/17 13:09:02 pibero
1049  * Initial Version of EMC DSM algorithms for 2009
1050  *
1051  * Revision 1.22 2008/01/22 18:06:26 kocolosk
1052  * added detailedResult code for BEMC L0, courtesy Dave Staszak
1053  * fixed two bugs in vector accessors in result class (also thanks to Dave)
1054  *
1055  * Revision 1.21 2008/01/17 17:04:07 kocolosk
1056  * some revisions to StTriggerSimuResult structure to hopefully improve clarity and maintainability
1057  *
1058  * Revision 1.20 2008/01/17 01:58:25 kocolosk
1059  * StTriggerSimuResult makes detailed emulation results persistent
1060  *
1061  *
1062  *****************************************************************************/
StTriggerSimuDecision triggerDecision(int trigId)
like isTrigger(), but returns kDoNotCare if detector isn&#39;t a part of the given trigId ...
StTriggerSimuDecision triggerDecision(int trigId)
like isTrigger(), but returns kDoNotCare if detector isn&#39;t a part of the given trigId ...
const unsigned int * result() const
bag of 64 bytes whose interpretation changes year-by-year
StTriggerSimuDecision triggerDecision(int trigId)
like isTrigger(), but returns kDoNotCare if detector isn&#39;t a part of the given trigId ...
Definition: Stypes.h:40
virtual void setMC(int flag)
0 == real data, 1 or more == Monte Carlo
virtual Int_t Finish()
Definition: StMaker.cxx:776
StTriggerSimuDecision triggerDecision(int trigId)
like isTrigger(), but returns kDoNotCare if detector isn&#39;t a part of the given trigId ...
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362