3 #include "StEmcMappingDb.h"
8 #include "St_db_Maker/St_db_Maker.h"
10 #include "StDbLib/StDbManager.hh"
11 #include "StDbLib/StDbConfigNode.hh"
12 #include "StDbLib/StDbTable.h"
14 #include "tables/St_bemcMap_Table.h"
15 #include "tables/St_bprsMap_Table.h"
16 #include "tables/St_bsmdeMap_Table.h"
17 #include "tables/St_bsmdpMap_Table.h"
23 StEmcMappingDb::StEmcMappingDb(
int date,
int time) : mBemcTTable(NULL),
24 mBprsTTable(NULL), mSmdeTTable(NULL), mSmdpTTable(NULL), mBemcValidity(-2),
25 mBprsValidity(-2), mSmdeValidity(-2), mSmdpValidity(-2), mBemcTable(NULL),
26 mBprsTable(NULL), mSmdeTable(NULL), mSmdpTable(NULL), mBemcDirty(true),
27 mBprsDirty(true), mSmdeDirty(true), mSmdpDirty(true)
29 mChain = StMaker::GetChain();
38 StDbConfigNode *db = mgr ? mgr->initConfig(
"Calibrations_emc") : 0;
40 mBemcTable = db->addDbTable(
"bemcMap");
41 mSmdeTable = db->addDbTable(
"bsmdeMap");
42 mSmdpTable = db->addDbTable(
"bsmdpMap");
43 mBprsTable = db->addDbTable(
"bprsMap");
45 int elements[18000];
for(
int i=0; i<18000; i++) elements[i] = i+1;
47 mBemcTable->resizeElementID(4800);
48 mBemcTable->setElementID(elements, 4800);
50 mBprsTable->resizeElementID(4800);
51 mBprsTable->setElementID(elements, 4800);
53 mSmdeTable->resizeElementID(18000);
54 mSmdeTable->setElementID(elements, 18000);
56 mSmdpTable->resizeElementID(18000);
57 mSmdpTable->setElementID(elements, 18000);
59 mBemcTTable =
new St_bemcMap();
60 mBemcTTable->Adopt(4800, mBemcTable->GetTable());
62 mBprsTTable =
new St_bprsMap();
63 mBprsTTable->Adopt(4800, mBprsTable->GetTable());
65 mSmdeTTable =
new St_bsmdeMap();
66 mSmdeTTable->Adopt(18000, mSmdeTable->GetTable());
68 mSmdpTTable =
new St_bsmdpMap();
69 mSmdpTTable->Adopt(18000, mSmdpTable->GetTable());
71 SetDateTime(date, time);
75 StEmcMappingDb::~StEmcMappingDb() {
90 LOG_DEBUG <<
"StEmcMappingDb::SetDateTime is illegal in a chain" << endm;
93 mBeginTime.Set(date, time);
94 unsigned unix = TUnixTime::Convert(mBeginTime,
true);
95 if(mBemcTable && !(mBemcTable->getBeginTime() < unix && unix < mBemcTable->getEndTime()) ) {
99 if(mBprsTable && !(mBprsTable->getBeginTime() < unix && unix < mBprsTable->getEndTime()) ) {
103 if( mSmdeTable && !(mSmdeTable->getBeginTime() < unix && unix < mSmdeTable->getEndTime()) ) {
107 if(mSmdpTable && !(mSmdpTable->getBeginTime() < unix && unix < mSmdpTable->getEndTime()) ) {
114 void StEmcMappingDb::SetFlavor(
const char *flavor,
const char *tablename) {
116 LOG_DEBUG <<
"StEmcMappingDb::SetFlavor is illegal in a chain" << endm;
119 if(!tablename || !strcmp(tablename,
"bemcMap")) {
120 if(mBemcTable && strcmp(mBemcTable->getFlavor(), flavor)) {
121 mBemcTable->setFlavor(flavor);
126 if(!tablename || !strcmp(tablename,
"bprsMap")) {
127 if(mBprsTable && strcmp(mBprsTable->getFlavor(), flavor)) {
128 mBprsTable->setFlavor(flavor);
133 if(!tablename || !strcmp(tablename,
"bsmdeMap")) {
134 if(mSmdeTable && strcmp(mSmdeTable->getFlavor(), flavor)) {
135 mSmdeTable->setFlavor(flavor);
140 if(!tablename || !strcmp(tablename,
"bsmdpMap")) {
141 if(mSmdpTable && strcmp(mSmdpTable->getFlavor(), flavor)) {
142 mSmdpTable->setFlavor(flavor);
150 void StEmcMappingDb::SetMaxEntryTime(
int date,
int time) {
152 LOG_DEBUG <<
"StEmcMappingDb::SetMaxEntryTime is illegal in a chain" << endm;
155 unsigned unixMax = TUnixTime::Convert(TDatime(date,time),
true);
156 if(mBemcTable && (mBemcTable->getProdTime() != unixMax)) {
157 mBemcTable->setProdTime(unixMax);
161 if(mBprsTable && (mBprsTable->getProdTime() != unixMax)) {
162 mBprsTable->setProdTime(unixMax);
166 if(mSmdeTable && (mSmdeTable->getProdTime() != unixMax)) {
167 mSmdeTable->setProdTime(unixMax);
171 if(mSmdpTable && (mSmdpTable->getProdTime() != unixMax)) {
172 mSmdpTable->setProdTime(unixMax);
180 const bemcMap_st* StEmcMappingDb::bemc()
const {
181 maybe_reload(kBarrelEmcTowerId);
182 return mBemcTTable ? mBemcTTable->GetTable() : 0;
186 const bprsMap_st* StEmcMappingDb::bprs()
const {
187 maybe_reload(kBarrelEmcPreShowerId);
188 return mBprsTTable ? mBprsTTable->GetTable() : 0;
192 const bsmdeMap_st* StEmcMappingDb::bsmde()
const {
193 maybe_reload(kBarrelSmdEtaStripId);
194 return mSmdeTTable ? mSmdeTTable->GetTable() : 0;
198 const bsmdpMap_st* StEmcMappingDb::bsmdp()
const {
199 maybe_reload(kBarrelSmdPhiStripId);
200 return mSmdpTTable ? mSmdpTTable->GetTable() : 0;
204 StEmcMappingDb::softIdFromMES(StDetectorId det,
int m,
int e,
int s)
const {
206 case kBarrelEmcTowerId:
207 case kBarrelEmcPreShowerId:
208 return 40*(m-1) + 20*(s-1) + e;
210 case kBarrelSmdEtaStripId:
211 return 150*(m-1) + 150*(s-1) + e;
213 case kBarrelSmdPhiStripId:
214 return 150*(m-1) + 10*(s-1) + e;
222 StEmcMappingDb::softIdFromCrate(StDetectorId det,
int crate,
int channel)
const {
223 if(det == kBarrelEmcTowerId) {
224 const bemcMap_st* map = bemc();
226 if(!mCacheCrate[crate-1][channel]) {
227 for(
int i=0; i<4800; ++i) {
228 mCacheCrate[map[i].crate-1][map[i].crateChannel] = i+1;
233 short id = mCacheCrate[crate-1][channel];
234 if(map[
id-1].crate == crate && map[
id-1].crateChannel == channel) {
238 return softIdFromCrate(det, crate, channel);
245 StEmcMappingDb::softIdFromDaqId(StDetectorId det,
int daqID)
const {
246 if(det == kBarrelEmcTowerId) {
247 const bemcMap_st* map = bemc();
249 if(!mCacheDaqId[daqID]) {
250 for(
int i=0; i<4800; ++i) {
251 mCacheDaqId[map[i].daqID] = i+1;
256 if(map[mCacheDaqId[daqID]-1].daqID == daqID) {
257 return mCacheDaqId[daqID];
260 return softIdFromDaqId(det, daqID);
267 StEmcMappingDb::softIdFromTDC(StDetectorId det,
int TDC,
int channel)
const {
268 if(det == kBarrelEmcTowerId) {
269 const bemcMap_st* map = bemc();
271 if(!mCacheTDC[TDC][channel]) {
272 for(
int i=0; i<4800; ++i) {
273 mCacheTDC[map[i].TDC][map[i].crateChannel] = i+1;
278 short id = mCacheTDC[TDC][channel];
279 if(map[
id-1].TDC == TDC && map[
id-1].crateChannel == channel) {
283 return softIdFromTDC(det, TDC, channel);
290 StEmcMappingDb::softIdFromRDO(StDetectorId det,
int rdo,
int channel)
const {
293 case kBarrelEmcPreShowerId:
295 const bprsMap_st* prs = bprs();
297 if(mCacheBprsRdo[rdo][channel] == -1) {
298 for(
int i=0; i<4800; i++) {
299 if(prs[i].rdo == rdo && prs[i].rdoChannel == channel) {
300 mCacheBprsRdo[rdo][channel] = i+1;
306 switch((
id = mCacheBprsRdo[rdo][channel])) {
309 mCacheBprsRdo[rdo][channel] = 0;
314 if(maybe_reset_cache(kBarrelEmcPreShowerId))
315 return softIdFromRDO(det, rdo, channel);
319 if(prs[
id-1].rdo == rdo && prs[
id-1].rdoChannel == channel){
323 return softIdFromRDO(det, rdo, channel);
328 case kBarrelSmdEtaStripId:
case kBarrelSmdPhiStripId:
330 const bsmdeMap_st *smde = bsmde();
331 const bsmdpMap_st *smdp = bsmdp();
332 if (!smde || !smdp)
return 0;
333 if(mCacheSmdRdo[rdo][channel] == -1) {
334 for(
int i=0; i<18000; i++) {
335 if(smde[i].rdo == rdo && smde[i].rdoChannel == channel) {
336 mCacheSmdRdo[rdo][channel] = i+1;
337 if(det == kBarrelSmdEtaStripId)
return i+1;
339 else if(smdp[i].rdo == rdo && smdp[i].rdoChannel == channel){
340 mCacheSmdRdo[rdo][channel] = i+1;
341 if(det == kBarrelSmdPhiStripId)
return i+1;
346 switch((
id = mCacheSmdRdo[rdo][channel])) {
349 mCacheSmdRdo[rdo][channel] = 0;
354 if(maybe_reset_cache(det))
355 return softIdFromRDO(det, rdo, channel);
360 if(smde[
id-1].rdo == rdo && smde[
id-1].rdoChannel == channel){
361 if(det == kBarrelSmdEtaStripId)
return id;
364 else if(smdp[
id-1].rdo == rdo && smdp[
id-1].rdoChannel == channel){
365 if(det == kBarrelSmdPhiStripId)
return id;
370 return softIdFromRDO(det, rdo, channel);
380 void StEmcMappingDb::maybe_reload(StDetectorId det)
const {
382 case kBarrelEmcTowerId:
385 TDataSet *DB = mChain->GetInputDB(
"Calibrations/emc/map");
386 if(DB) mBemcTTable =
static_cast<St_bemcMap*
>(DB->
Find(
"bemcMap"));
389 if(mBemcDirty) reload_dbtable(mBemcTable);
394 case kBarrelEmcPreShowerId:
397 TDataSet *DB = mChain->GetInputDB(
"Calibrations/emc/map");
398 if(DB) mBprsTTable =
static_cast<St_bprsMap*
>(DB->
Find(
"bprsMap"));
401 if(mBprsDirty) reload_dbtable(mBprsTable);
406 case kBarrelSmdEtaStripId:
409 TDataSet *DB = mChain->GetInputDB(
"Calibrations/emc/map");
410 if(DB) mSmdeTTable =
static_cast<St_bsmdeMap*
>(DB->
Find(
"bsmdeMap"));
413 if(mSmdeDirty) reload_dbtable(mSmdeTable);
418 case kBarrelSmdPhiStripId:
421 TDataSet *DB = mChain->GetInputDB(
"Calibrations/emc/map");
422 if(DB) mSmdpTTable =
static_cast<St_bsmdpMap*
>(DB->
Find(
"bsmdpMap"));
425 if(mSmdpDirty) reload_dbtable(mSmdpTable);
432 void StEmcMappingDb::reload_dbtable(
StDbTable* table)
const {
434 LOG_DEBUG <<
"(re)loading mapping table using StDbManager" << endm;
437 mgr->setVerbose(
false);
438 mgr->setRequestTime(mBeginTime.AsSQLString());
439 mgr->fetchDbTable(table);
443 bool StEmcMappingDb::maybe_reset_cache(StDetectorId det)
const {
445 if(!mChain)
return false;
449 case kBarrelEmcTowerId:
450 if((version = St_db_Maker::GetValidity(mBemcTTable,NULL)) != mBemcValidity) {
451 mBemcValidity = version;
457 case kBarrelEmcPreShowerId:
458 if((version = St_db_Maker::GetValidity(mBprsTTable,NULL)) != mBprsValidity) {
459 mBprsValidity = version;
465 case kBarrelSmdEtaStripId:
466 if((version = St_db_Maker::GetValidity(mSmdeTTable,NULL)) != mSmdeValidity) {
467 mSmdeValidity = version;
473 case kBarrelSmdPhiStripId:
474 if((version = St_db_Maker::GetValidity(mSmdpTTable,NULL)) != mSmdpValidity) {
475 mSmdpValidity = version;
484 void StEmcMappingDb::reset_bemc_cache()
const {
485 memset(mCacheCrate, 0,
sizeof(mCacheCrate));
486 memset(mCacheDaqId, 0,
sizeof(mCacheDaqId));
487 memset(mCacheTDC, 0,
sizeof(mCacheTDC));
490 void StEmcMappingDb::reset_bprs_cache()
const {
491 memset(mCacheBprsRdo, -1,
sizeof(mCacheBprsRdo));
494 void StEmcMappingDb::reset_smde_cache()
const {
495 memset(mCacheSmdRdo, -1,
sizeof(mCacheSmdRdo));
498 void StEmcMappingDb::reset_smdp_cache()
const {
499 memset(mCacheSmdRdo, -1,
sizeof(mCacheSmdRdo));
void version(std::ostream &os=std::cout)
print HepMC version
void SetDateTime(int date, int time)
St_db_Maker-compatible interface.
static StDbManager * Instance()
strdup(..) is not ANSI
virtual TDataSet * Find(const char *path) const