1 #include "StOnlineTriggerMonitoring.h"
13 #include "StMessMgr.h"
15 #include "StDbLib/StDbManager.hh"
16 #include "StDbLib/StDbConfigNode.hh"
17 #include "StDbLib/StDbTable.h"
18 #include "StDbLib/StDataBaseI.hh"
20 #include "tables/St_emcTriggerStatus_Table.h"
21 #include "tables/St_emcTriggerPed_Table.h"
22 #include "tables/St_emcTriggerLUT_Table.h"
23 #include "tables/St_bemcTriggerPed4_Table.h"
25 #include "StEmcUtil/database/StEmcDecoder.h"
26 #include "StEmcUtil/database/StBemcTablesWriter.h"
28 char BITMASK[15][130];
29 int PATCH[300],
DSM[300], BIT[300], PA[300], HT[300], TOWER[30][160], PED[30][160], PEDRMS[30][160], BITCONV[30][10];
30 int FORMULATAG[30][10],FORMULAPARAMETER[30][10][6];
36 int unpackbits(
char *
data,
int nbits,
int index)
39 TString v= a(index,nbits);
40 int b = strtol(v.Data(),NULL,2);
43 void swapline(
char*pointer)
46 for(
int i=0;i<16;i++) T[i] = pointer[i];
65 void makeString(
unsigned char* data,
int nb, TString &result)
68 for(
int i=0;i<nb;i++) for (Int_t j = 7;j >= 0;j--) result += (((
short)data[i] & (1 << j)) ?
"1" :
"0");
70 void loadTriggerMasks(
char* file,
int start)
72 FILE *W = fopen(file,
"rb");
73 unsigned char mask[16];
74 for(
int dsm=1;dsm<=15;dsm++)
76 char * pointer = (
char*)mask;
77 fread(pointer,(
int)
sizeof(
unsigned char)*16,1,W);
80 makeString((
unsigned char*) pointer,16, result);
81 sprintf(BITMASK[dsm-1],
"%s",result.Data());
83 cout <<
"Decoding the data "<<endl;
84 for(
int patch = 1; patch<=150;patch++)
86 int dsm =
DSM[patch-1+start];
87 int bit = BIT[patch-1+start];
88 int ht = unpackbits((
char*)BITMASK[dsm-1],6,128-bit-6);
89 int pa = unpackbits((
char*)BITMASK[dsm-1],6,128-bit-12);
90 if(ht==63) HT[patch-1+start] = 1;
else HT[patch-1+start] = 0;
91 if(pa==63) PA[patch-1+start] = 1;
else PA[patch-1+start] = 0;
96 void StOnlineTriggerMonitoring::saveTrigger(
const Char_t *TS, Bool_t status, Bool_t pedestal, Bool_t lut, Bool_t saveDB, Bool_t saveTables,
const Char_t *tables_dir,
const Char_t *saved_dir,
const Char_t *bemcStatusCopy,
const Char_t *bceTable,
const Char_t *bcwTable)
98 cout <<
"tables dir = " << tables_dir << endl;
99 cout <<
"saved dir = " << saved_dir << endl;
100 cout <<
"bemcStatusCopy = " << bemcStatusCopy << endl;
101 cout <<
"bceTable = " << bceTable << endl;
102 cout <<
"bcwTable = " << bcwTable << endl;
106 cout <<
"Saving trigger tables in offline DB for TS = "<< TS <<endl;
108 cout <<
"Creating StEmcDecoder..." << endl;
110 cout <<
"Decoder created " << decoder << endl;
115 ifstream inw(bcwTable);
116 for(
int i=0;i<150;i++) {
117 inw >> PATCH[i]>>
DSM[i]>>BIT[i];
121 ifstream ine(bceTable);
122 for(
int i=0;i<150;i++) {
123 ine >> PATCH[i+150]>>
DSM[i+150]>>BIT[i+150];
128 FILENAME = saved_dir; FILENAME +=
"/bcw.lut.bin";
129 cout <<
"Loading trigger masks from "<<FILENAME.Data()<<endl;
130 loadTriggerMasks((
char*)FILENAME.Data(),0);
131 FILENAME = saved_dir; FILENAME +=
"/bce.lut.bin";
132 cout <<
"Loading trigger masks from "<<FILENAME.Data()<<endl;
133 loadTriggerMasks((
char*)FILENAME.Data(),150);
137 for(
int i=0;i<300;i++){
138 if(HT[i]==0) cout <<
"High Tower "<<i<<
" masked out\n";
139 if(PA[i]==0) cout <<
"Patch sum "<<i<<
" masked out\n";
143 cout <<
"Number of high towers masked out = "<<NHT<<endl;
144 cout <<
"Number of trigger patches masked out = "<<NPA<<endl;
147 for(
int crate = 1;crate<=30;crate++){
148 FILENAME = saved_dir;
149 FILENAME+=
"/daq_pedestal_crate0x";
151 sprintf(str,
"%02x",crate);
157 if(gSystem->GetPathInfo(FILENAME.Data(),&id,&size,&flag,&mod)==0){
158 cout <<FILENAME.Data()<<
" is Ok\n";
159 ifstream in(FILENAME.Data());
160 Char_t lineData[300];
161 Int_t towerid,ped4val;
163 for(
int i=0;i<160;i++){
164 in.getline(lineData,300);
165 Int_t nColumns = sscanf(lineData,
"%d %f %f %d",&towerid,&ped,&rms,&ped4val);
168 Int_t towerSoftId = 0;
170 PED4[towerSoftId-1]=(
short)ped4val;
171 PED[crate-1][towerid] = (int)(ped*100);
172 PEDRMS[crate-1][towerid] = (int)(rms*100);
176 for(
int i=0;i<160;i++) {
178 PEDRMS[crate-1][i]=0;
187 FILENAME = saved_dir; FILENAME+=
"/BemcConfig.dat";
188 ifstream bemcConf(FILENAME.Data());
191 while(!bemcConf.eof()){
192 bemcConf.getline(line,4096);
195 if(L.Index(
"TriggerPedestalShift")>=0){
196 TString b = L(L.Index(
"TriggerPedestalShift")+21,L.Length());
197 if(b.BeginsWith(
"0x")) PEDSHIFT = strtol(b.Data(),NULL,16);
198 else PEDSHIFT = atoi(b.Data());
204 for(
int crate = 1;crate<=30;crate++){
205 FILENAME = saved_dir; FILENAME+=
"/config_crate0x";
207 sprintf(str,
"%02x",crate);
213 if(gSystem->GetPathInfo(FILENAME.Data(),&id,&size,&flag,&mod)==0){
214 cout <<FILENAME.Data()<<
" is Ok\n";
215 ifstream in(FILENAME.Data());
217 short bit6[2][5],mask[2][5],formula[2][5],par[2][5][6];
227 for(
int board = 1; board<=5;board++){
232 for(
int patch = 0;patch<2;patch++){
235 if(A1.BeginsWith(
"0x")) mask[patch][board-1] = strtol(A1.Data(),NULL,16);
236 else mask[patch][board-1] = atoi(A1.Data());
240 if(A11.BeginsWith(
"0x")) formula[patch][board-1] = strtol(A11.Data(),NULL,16);
241 else formula[patch][board-1] = atoi(A11.Data());
243 for(
int f = 0;f<6;f++){
246 if(A12.BeginsWith(
"0x")) par[patch][board-1][f] = strtol(A12.Data(),NULL,16);
247 else par[patch][board-1][f] = atoi(A12.Data());
252 if(A.BeginsWith(
"0x")) bit6[patch][board-1] = strtol(A.Data(),NULL,16);
253 else bit6[patch][board-1] = atoi(A.Data());
258 for(
int board = 0; board<=5;board++){
262 Bool_t boardHT = atoi(tmp);
264 cout <<
"Board " << board <<
" HT peds not configured" << endl;
270 Bool_t boardJP = atoi(tmp);
272 cout <<
"Board " << board <<
" LUT not configured" << endl;
273 formula[0][board-1] = 0;
274 formula[1][board-1] = 0;
275 par[0][board-1][0] = 1;
276 par[1][board-1][0] = 1;
277 par[0][board-1][1] = 0;
278 par[1][board-1][1] = 0;
279 par[0][board-1][2] = 0;
280 par[1][board-1][2] = 0;
281 par[0][board-1][3] = 1;
282 par[1][board-1][3] = 1;
283 par[0][board-1][4] = 0;
284 par[1][board-1][4] = 0;
285 par[0][board-1][5] = 0;
286 par[1][board-1][5] = 0;
294 for(
int i=0;i<160;i++){
297 int patch = channel/16;
298 int m = mask[patch][board-1];
299 int bit = channel-16*patch;
300 FORMULATAG[crate-1][i/16] = formula[patch][board-1];
301 for(
int p = 0;p<6;p++)
302 FORMULAPARAMETER[crate-1][i/16][p] = par[patch][board-1][p];
303 BITCONV[crate-1][i/16] = bit6[patch][board-1];
304 if( ((m>>bit) & 0x1)==1 ) TOWER[crate-1][i] = 1;
else TOWER[crate-1][i] = 0;
305 if(TOWER[crate-1][i] == 0) cout <<
" Masked out channel "<<i<<
" from crate "<<crate<<endl;
309 else for(
int i=0;i<160;i++) { BITCONV[crate-1][i/16]=0; TOWER[crate-1][i]=0;}
315 TString timestamp=TSp(first+1,4);
317 timestamp+=TSp(first+5,2);
319 timestamp+=TSp(first+7,2);
321 timestamp+=TSp(first+10,2);
323 timestamp+=TSp(first+12,2);
325 timestamp+=TSp(first+14,2);
326 cout <<
"TS = "<<TS<<
" TimeStamp = "<<timestamp<<endl;
328 int towerData[4800][8];
329 for (
int i = 0;i < 4800;i++){
330 for (
int j = 0;j < 8;j++){
334 cout <<
"Start writing " << bemcStatusCopy << endl;
335 ofstream bemcStatusStream(bemcStatusCopy);
336 bemcStatusStream <<
"##################################################################################" << endl;
337 bemcStatusStream <<
"# This plain text file contains the complete BEMC trigger configuration" << endl;
338 bemcStatusStream <<
"# Generated by the online BEMC trigger monitoring program on " << gSystem->HostName() <<
":" << gSystem->WorkingDirectory() << endl;
339 bemcStatusStream <<
"# Timestamp: " << timestamp << endl;
342 for(
int crate = 1;crate <= 30;crate++) {
343 for(
int tower = 0;tower < 160;tower++) {
346 towerData[softId - 1][0] = TOWER[crate - 1][tower];
347 towerData[softId - 1][3] = PED[crate - 1][tower];
348 towerData[softId - 1][7] = PEDRMS[crate - 1][tower];
349 towerData[softId - 1][4] = crate;
350 towerData[softId - 1][5] = tower;
354 towerData[softId - 1][6] = triggerPatch;
355 towerData[softId - 1][1] = HT[triggerPatch];
356 towerData[softId - 1][2] = PA[triggerPatch];
360 bemcStatusStream <<
"#" << endl;
361 bemcStatusStream <<
"# SoftId\tCrate\tCrate seq\tTower unmasked?\tPatch unmasked in HT?\tPatch unmasked in sum?\tPedestal\ttriggerPatch" << endl;
362 for (
int i = 0;i < 4800;i++) {
363 bemcStatusStream <<
"SoftId " << (i+1) <<
"\t"
364 << towerData[i][4] <<
"\t"
365 << towerData[i][5] <<
"\t"
366 << towerData[i][0] <<
"\t"
367 << towerData[i][1] <<
"\t"
368 << towerData[i][2] <<
"\t"
369 << (Float_t(towerData[i][3]) / 100.0) <<
"\t"
370 << towerData[i][6] << endl;
372 bemcStatusStream <<
"#" << endl;
373 bemcStatusStream <<
"TriggerPedestalShift " << (float(PEDSHIFT)/100.0) << endl;
374 bemcStatusStream <<
"#" << endl;
375 bemcStatusStream <<
"# triggerPatch\tCrate\tCrate patch\tUnmasked in HT?\tUnmasked in sum?\tBit conversion mode\tLUT formula and parameters" << endl;
376 for(
int crate = 1;crate <= 30;crate++) {
377 for(
int patch = 0;patch < 10;patch++) {
380 bemcStatusStream <<
"triggerPatch " << triggerPatch <<
"\t" << crate <<
"\t" << patch <<
"\t";
381 bemcStatusStream << HT[triggerPatch] <<
"\t";
382 bemcStatusStream << PA[triggerPatch] <<
"\t";
383 bemcStatusStream << BITCONV[crate-1][patch] <<
"\t";
384 bemcStatusStream << FORMULATAG[crate-1][patch] <<
"\t";
385 bemcStatusStream << FORMULAPARAMETER[crate-1][patch][0] <<
"\t";
386 bemcStatusStream << FORMULAPARAMETER[crate-1][patch][1] <<
"\t";
387 bemcStatusStream << FORMULAPARAMETER[crate-1][patch][2] <<
"\t";
388 bemcStatusStream << FORMULAPARAMETER[crate-1][patch][3] <<
"\t";
389 bemcStatusStream << FORMULAPARAMETER[crate-1][patch][4] <<
"\t";
390 bemcStatusStream << FORMULAPARAMETER[crate-1][patch][5];
391 bemcStatusStream << endl;
396 cout <<
"Deleting decoder " << decoder << endl;
397 if (decoder)
delete decoder; decoder = 0;
398 cout <<
"Decoder deleted " << decoder << endl;
400 bemcStatusStream <<
"# End of file" << endl;
401 bemcStatusStream <<
"##################################################################################" << endl;
402 bemcStatusStream.close();
403 cout <<
"Finished writing " << bemcStatusCopy << endl;
405 cout <<
"Started creating tables" << endl;
406 St_emcTriggerPed *pedestals_t =
new St_emcTriggerPed(
"bemcTriggerPed",1);
407 St_bemcTriggerPed4 *ped4_t =
new St_bemcTriggerPed4(
"bemcTriggerPed4",1);
408 St_emcTriggerStatus *status_t =
new St_emcTriggerStatus(
"bemcTriggerStatus",1);
409 St_emcTriggerLUT *lut_t =
new St_emcTriggerLUT(
"bemcTriggerLUT",1);
410 emcTriggerPed_st* pedestals_st = pedestals_t ? pedestals_t->GetTable() : 0;
411 bemcTriggerPed4_st* ped4_st = ped4_t ? ped4_t->GetTable() : 0;
412 emcTriggerStatus_st* status_st = status_t ? status_t->GetTable() : 0;
413 emcTriggerLUT_st* lut_st = lut_t ? lut_t->GetTable() : 0;
414 cout <<
"Finished creating tables" << endl;
416 cout <<
"Started filling tables" << endl;
417 if (pedestals_st) pedestals_st->PedShift = PEDSHIFT;
418 for(
int crate = 1;crate<=30;crate++) {
419 for(
int patch =0;patch<10;patch++) {
420 if (pedestals_st) pedestals_st->BitConversionMode[crate-1][patch] = BITCONV[crate-1][patch];
422 lut_st->FormulaTag[crate-1][patch] = FORMULATAG[crate-1][patch];
423 lut_st->FormulaParameter0[crate-1][patch] = FORMULAPARAMETER[crate-1][patch][0];
424 lut_st->FormulaParameter1[crate-1][patch] = FORMULAPARAMETER[crate-1][patch][1];
425 lut_st->FormulaParameter2[crate-1][patch] = FORMULAPARAMETER[crate-1][patch][2];
426 lut_st->FormulaParameter3[crate-1][patch] = FORMULAPARAMETER[crate-1][patch][3];
427 lut_st->FormulaParameter4[crate-1][patch] = FORMULAPARAMETER[crate-1][patch][4];
428 lut_st->FormulaParameter5[crate-1][patch] = FORMULAPARAMETER[crate-1][patch][5];
431 for(
int tower =0;tower<160;tower++) {
432 if (pedestals_st) pedestals_st->Ped[crate-1][tower]=PED[crate-1][tower];
433 if (status_st) status_st->TowerStatus[crate-1][tower]=TOWER[crate-1][tower];
436 cout <<
"... filling tables" << endl;
437 for(
int patch=0;patch<300;patch++) {
439 status_st->PatchStatus[patch]=PA[patch];
440 status_st->HighTowerStatus[patch]=HT[patch];
444 for (
int i = 0;i < 4800;i++)
445 if (ped4_st) ped4_st->Ped4[i]=PED4[i];
447 cout <<
"Finished filling tables" << endl;
448 cout <<
"Creating StBemcTablesWriter ..." << endl;
450 cout <<
"Created StBemcTablesWriter" << endl;
451 cout <<
"Loading tables ..." << endl;
453 cout <<
"Loaded tables" << endl;
455 if(status && status_st) {
456 cout <<
"Setting bemcTriggerStatus table " << status_st << endl;
457 writer.setTable(
"bemcTriggerStatus", status_st);
459 LOG_INFO <<
"Start uploading table bemcTriggerStatus" << endm;
460 writer.writeToDb(
"bemcTriggerStatus", timestamp.Data());
461 LOG_INFO <<
"Finished uploading table bemcTriggerStatus" << endm;
464 FILENAME = tables_dir; FILENAME +=
"/bemcTriggerStatus."; FILENAME+=TS; FILENAME+=
".root";
465 LOG_INFO <<
"Start saving table " << FILENAME << endm;
466 writer.writeToFile(FILENAME.Data());
467 LOG_INFO <<
"Finished saving table " << FILENAME << endm;
471 if(pedestal && pedestals_st) {
472 cout <<
"Setting bemcTriggerPed table " << pedestals_st << endl;
473 writer.setTable(
"bemcTriggerPed", pedestals_st);
475 LOG_INFO <<
"Start uploading table bemcTriggerPed" << endm;
476 writer.writeToDb(
"bemcTriggerPed", timestamp.Data());
477 LOG_INFO <<
"Finished uploading table bemcTriggerPed" << endm;
480 FILENAME = tables_dir; FILENAME +=
"/bemcTriggerPed."; FILENAME+=TS; FILENAME+=
".root";
481 LOG_INFO <<
"Start saving table " << FILENAME << endm;
482 writer.writeToFile(FILENAME.Data());
483 LOG_INFO <<
"Finished saving table " << FILENAME << endm;
487 if(pedestal && ped4_st) {
488 cout <<
"Setting bemcTriggerPed4 table " << ped4_st << endl;
489 writer.setTable(
"bemcTriggerPed4", ped4_st);
491 LOG_INFO <<
"Start uploading table bemcTriggerPed4" << endm;
492 writer.writeToDb(
"bemcTriggerPed4", timestamp.Data());
493 LOG_INFO <<
"Finished uploading table bemcTriggerPed4" << endm;
496 FILENAME = tables_dir; FILENAME +=
"/bemcTriggerPed4."; FILENAME+=TS; FILENAME+=
".root";
497 LOG_INFO <<
"Start saving table " << FILENAME << endm;
498 writer.writeToFile(FILENAME.Data());
499 LOG_INFO <<
"Finished saving table " << FILENAME << endm;
503 cout <<
"Setting bemcTriggerLUT table " << lut_st << endl;
504 writer.setTable(
"bemcTriggerLUT", lut_st);
506 LOG_INFO <<
"Start uploading table bemcTriggerLUT" << endm;
507 writer.writeToDb(
"bemcTriggerLUT", timestamp.Data());
508 LOG_INFO <<
"Finished uploading table bemcTriggerLUT" << endm;
511 FILENAME = tables_dir; FILENAME +=
"/bemcTriggerLUT."; FILENAME+=TS; FILENAME+=
".root";
512 LOG_INFO <<
"Start saving table " << FILENAME << endm;
513 writer.writeToFile(FILENAME.Data());
514 LOG_INFO <<
"Finished saving table " << FILENAME << endm;
int GetTriggerPatchFromCrate(int crate, int sequence, int &patchId) const
returns the trigger patch from crate and sequence in the crate
void loadTables(const char *sqlTime, const char *flavor="ofl")
load directly from DB, no Maker needed
int GetTowerIdFromCrate(int crate, int sequence, int &softId) const
Get Software Id from Crate number and position in crate for towers.