StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StEmcMappingDb.cxx
1 // $Id: StEmcMappingDb.cxx,v 1.9 2010/01/28 13:45:06 mattheww Exp $
2 
3 #include "StEmcMappingDb.h"
4 
5 #include "TUnixTime.h"
6 #include "StMessMgr.h"
7 #include "StMaker.h"
8 #include "St_db_Maker/St_db_Maker.h"
9 
10 #include "StDbLib/StDbManager.hh"
11 #include "StDbLib/StDbConfigNode.hh"
12 #include "StDbLib/StDbTable.h"
13 
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"
18 
19 ClassImp(StEmcMappingDb);
20 
21 StEmcMappingDb* StEmcMappingDb::mInstance = NULL;
22 
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)
28 {
29  mChain = StMaker::GetChain();
30 
31  reset_bemc_cache();
32  reset_bprs_cache();
33  reset_smde_cache();
34  reset_smdp_cache();
35 
36  if(!mChain) {
38  StDbConfigNode *db = mgr ? mgr->initConfig("Calibrations_emc") : 0;
39  if (db) {
40  mBemcTable = db->addDbTable("bemcMap");
41  mSmdeTable = db->addDbTable("bsmdeMap");
42  mSmdpTable = db->addDbTable("bsmdpMap");
43  mBprsTable = db->addDbTable("bprsMap");
44 
45  int elements[18000]; for(int i=0; i<18000; i++) elements[i] = i+1;
46 
47  mBemcTable->resizeElementID(4800);
48  mBemcTable->setElementID(elements, 4800);
49 
50  mBprsTable->resizeElementID(4800);
51  mBprsTable->setElementID(elements, 4800);
52 
53  mSmdeTable->resizeElementID(18000);
54  mSmdeTable->setElementID(elements, 18000);
55 
56  mSmdpTable->resizeElementID(18000);
57  mSmdpTable->setElementID(elements, 18000);
58 
59  mBemcTTable = new St_bemcMap();
60  mBemcTTable->Adopt(4800, mBemcTable->GetTable());
61 
62  mBprsTTable = new St_bprsMap();
63  mBprsTTable->Adopt(4800, mBprsTable->GetTable());
64 
65  mSmdeTTable = new St_bsmdeMap();
66  mSmdeTTable->Adopt(18000, mSmdeTable->GetTable());
67 
68  mSmdpTTable = new St_bsmdpMap();
69  mSmdpTTable->Adopt(18000, mSmdpTable->GetTable());
70  }
71  SetDateTime(date, time);
72  }
73 }
74 
75 StEmcMappingDb::~StEmcMappingDb() {
76  // I'm intentionally not deleting the TTables created on the heap above. If
77  // I tried to delete them I'd end up with a double-delete of the mapping
78  // memory, since the StDbTables already own it. I don't want to copy the
79  // data because it's ~6 MB or so. Each TTable only has 2 Long_ts and an
80  // Int_t; I think this should be OK.
81 }
82 
83 StEmcMappingDb* StEmcMappingDb::instance() {
84  if(!mInstance) mInstance = new StEmcMappingDb();
85  return mInstance;
86 }
87 
88 void StEmcMappingDb::SetDateTime(int date, int time) {
89  if(mChain) {
90  LOG_DEBUG << "StEmcMappingDb::SetDateTime is illegal in a chain" << endm;
91  }
92  else {
93  mBeginTime.Set(date, time);
94  unsigned unix = TUnixTime::Convert(mBeginTime, true);
95  if(mBemcTable && !(mBemcTable->getBeginTime() < unix && unix < mBemcTable->getEndTime()) ) {
96  mBemcDirty = true;
97  reset_bemc_cache();
98  }
99  if(mBprsTable && !(mBprsTable->getBeginTime() < unix && unix < mBprsTable->getEndTime()) ) {
100  mBprsDirty = true;
101  reset_bprs_cache();
102  }
103  if( mSmdeTable && !(mSmdeTable->getBeginTime() < unix && unix < mSmdeTable->getEndTime()) ) {
104  mSmdeDirty = true;
105  reset_smde_cache();
106  }
107  if(mSmdpTable && !(mSmdpTable->getBeginTime() < unix && unix < mSmdpTable->getEndTime()) ) {
108  mSmdpDirty = true;
109  reset_smdp_cache();
110  }
111  }
112 }
113 
114 void StEmcMappingDb::SetFlavor(const char *flavor, const char *tablename) {
115  if(mChain) {
116  LOG_DEBUG << "StEmcMappingDb::SetFlavor is illegal in a chain" << endm;
117  }
118  else {
119  if(!tablename || !strcmp(tablename, "bemcMap")) {
120  if(mBemcTable && strcmp(mBemcTable->getFlavor(), flavor)) {
121  mBemcTable->setFlavor(flavor);
122  mBemcDirty = true;
123  reset_bemc_cache();
124  }
125  }
126  if(!tablename || !strcmp(tablename, "bprsMap")) {
127  if(mBprsTable && strcmp(mBprsTable->getFlavor(), flavor)) {
128  mBprsTable->setFlavor(flavor);
129  mBprsDirty = true;
130  reset_bprs_cache();
131  }
132  }
133  if(!tablename || !strcmp(tablename, "bsmdeMap")) {
134  if(mSmdeTable && strcmp(mSmdeTable->getFlavor(), flavor)) {
135  mSmdeTable->setFlavor(flavor);
136  mSmdeDirty = true;
137  reset_smde_cache();
138  }
139  }
140  if(!tablename || !strcmp(tablename, "bsmdpMap")) {
141  if(mSmdpTable && strcmp(mSmdpTable->getFlavor(), flavor)) {
142  mSmdpTable->setFlavor(flavor);
143  mSmdpDirty = true;
144  reset_smdp_cache();
145  }
146  }
147  }
148 }
149 
150 void StEmcMappingDb::SetMaxEntryTime(int date, int time) {
151  if(mChain) {
152  LOG_DEBUG << "StEmcMappingDb::SetMaxEntryTime is illegal in a chain" << endm;
153  }
154  else {
155  unsigned unixMax = TUnixTime::Convert(TDatime(date,time), true);
156  if(mBemcTable && (mBemcTable->getProdTime() != unixMax)) {
157  mBemcTable->setProdTime(unixMax);
158  mBemcDirty = true;
159  reset_bemc_cache();
160  }
161  if(mBprsTable && (mBprsTable->getProdTime() != unixMax)) {
162  mBprsTable->setProdTime(unixMax);
163  mBprsDirty = true;
164  reset_bprs_cache();
165  }
166  if(mSmdeTable && (mSmdeTable->getProdTime() != unixMax)) {
167  mSmdeTable->setProdTime(unixMax);
168  mSmdeDirty = true;
169  reset_smde_cache();
170  }
171  if(mSmdpTable && (mSmdpTable->getProdTime() != unixMax)) {
172  mSmdpTable->setProdTime(unixMax);
173  mSmdpDirty = true;
174  reset_smdp_cache();
175  }
176  }
177 }
178 
179 // struct declaration auto-generated at $STAR/include/bemcMap.h
180 const bemcMap_st* StEmcMappingDb::bemc() const {
181  maybe_reload(kBarrelEmcTowerId);
182  return mBemcTTable ? mBemcTTable->GetTable() : 0;
183 }
184 
185 // struct declaration auto-generated at $STAR/include/bprsMap.h
186 const bprsMap_st* StEmcMappingDb::bprs() const {
187  maybe_reload(kBarrelEmcPreShowerId);
188  return mBprsTTable ? mBprsTTable->GetTable() : 0;
189 }
190 
191 // struct declaration auto-generated at $STAR/include/bsmdeMap.h
192 const bsmdeMap_st* StEmcMappingDb::bsmde() const {
193  maybe_reload(kBarrelSmdEtaStripId);
194  return mSmdeTTable ? mSmdeTTable->GetTable() : 0;
195 }
196 
197 // struct declaration auto-generated at $STAR/include/bsmdpMap.h
198 const bsmdpMap_st* StEmcMappingDb::bsmdp() const {
199  maybe_reload(kBarrelSmdPhiStripId);
200  return mSmdpTTable ? mSmdpTTable->GetTable() : 0;
201 }
202 
203 int
204 StEmcMappingDb::softIdFromMES(StDetectorId det, int m, int e, int s) const {
205  switch(det) {
206  case kBarrelEmcTowerId:
207  case kBarrelEmcPreShowerId:
208  return 40*(m-1) + 20*(s-1) + e;
209 
210  case kBarrelSmdEtaStripId:
211  return 150*(m-1) + 150*(s-1) + e;
212 
213  case kBarrelSmdPhiStripId:
214  return 150*(m-1) + 10*(s-1) + e;
215 
216  default: break;
217  }
218  return 0;
219 }
220 
221 int
222 StEmcMappingDb::softIdFromCrate(StDetectorId det, int crate, int channel) const {
223  if(det == kBarrelEmcTowerId) {
224  const bemcMap_st* map = bemc();
225  if (!map) return 0;
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;
229  }
230  }
231 
232  // confirm that cache is still valid
233  short id = mCacheCrate[crate-1][channel];
234  if(map[id-1].crate == crate && map[id-1].crateChannel == channel) {
235  return id;
236  } else {
237  reset_bemc_cache();
238  return softIdFromCrate(det, crate, channel);
239  }
240  }
241  return 0;
242 }
243 
244 int
245 StEmcMappingDb::softIdFromDaqId(StDetectorId det, int daqID) const {
246  if(det == kBarrelEmcTowerId) {
247  const bemcMap_st* map = bemc();
248  if (!map) return 0;
249  if(!mCacheDaqId[daqID]) {
250  for(int i=0; i<4800; ++i) {
251  mCacheDaqId[map[i].daqID] = i+1;
252  }
253  }
254 
255  // confirm that cache is still valid
256  if(map[mCacheDaqId[daqID]-1].daqID == daqID) {
257  return mCacheDaqId[daqID];
258  } else {
259  reset_bemc_cache();
260  return softIdFromDaqId(det, daqID);
261  }
262  }
263  return 0;
264 }
265 
266 int
267 StEmcMappingDb::softIdFromTDC(StDetectorId det, int TDC, int channel) const {
268  if(det == kBarrelEmcTowerId) {
269  const bemcMap_st* map = bemc();
270  if (!map) return 0;
271  if(!mCacheTDC[TDC][channel]) {
272  for(int i=0; i<4800; ++i) {
273  mCacheTDC[map[i].TDC][map[i].crateChannel] = i+1;
274  }
275  }
276 
277  // confirm that cache is still valid
278  short id = mCacheTDC[TDC][channel];
279  if(map[id-1].TDC == TDC && map[id-1].crateChannel == channel) {
280  return id;
281  } else {
282  reset_bemc_cache();
283  return softIdFromTDC(det, TDC, channel);
284  }
285  }
286  return 0;
287 }
288 
289 int
290 StEmcMappingDb::softIdFromRDO(StDetectorId det, int rdo, int channel) const {
291  short id;
292  switch(det) {
293  case kBarrelEmcPreShowerId:
294  {
295  const bprsMap_st* prs = bprs();
296  if (!prs) return 0;
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;
301  return i+1;
302  }
303  }
304  }
305 
306  switch((id = mCacheBprsRdo[rdo][channel])) {
307  // didn't find it on a linear search, must be empty channel
308  case -1:
309  mCacheBprsRdo[rdo][channel] = 0;
310  return 0;
311 
312  // check cache validity before accepting an empty channel
313  case 0:
314  if(maybe_reset_cache(kBarrelEmcPreShowerId))
315  return softIdFromRDO(det, rdo, channel);
316  else return 0;
317 
318  default:
319  if(prs[id-1].rdo == rdo && prs[id-1].rdoChannel == channel){
320  return id;
321  } else {
322  reset_bprs_cache();
323  return softIdFromRDO(det, rdo, channel);
324  }
325  }
326  }
327 
328  case kBarrelSmdEtaStripId: case kBarrelSmdPhiStripId:
329  {
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;
338  }
339  else if(smdp[i].rdo == rdo && smdp[i].rdoChannel == channel){
340  mCacheSmdRdo[rdo][channel] = i+1;
341  if(det == kBarrelSmdPhiStripId) return i+1;
342  }
343  }
344  }
345 
346  switch((id = mCacheSmdRdo[rdo][channel])) {
347  // didn't find it on a linear search, must be empty channel
348  case -1:
349  mCacheSmdRdo[rdo][channel] = 0;
350  return 0;
351 
352  // check cache validity before accepting an empty channel
353  case 0:
354  if(maybe_reset_cache(det))
355  return softIdFromRDO(det, rdo, channel);
356  else return 0;
357 
358  // confirm result
359  default:
360  if(smde[id-1].rdo == rdo && smde[id-1].rdoChannel == channel){
361  if(det == kBarrelSmdEtaStripId) return id;
362  return 0;
363  }
364  else if(smdp[id-1].rdo == rdo && smdp[id-1].rdoChannel == channel){
365  if(det == kBarrelSmdPhiStripId) return id;
366  return 0;
367  }
368  else{
369  reset_smde_cache(); // also resets smdp cache
370  return softIdFromRDO(det, rdo, channel);
371  }
372  }
373  }
374  default: break;
375  }
376  return 0;
377 }
378 
379 /* Private methods used for caching SQL query results */
380 void StEmcMappingDb::maybe_reload(StDetectorId det) const {
381  switch(det) {
382  case kBarrelEmcTowerId:
383  if(mChain) {
384  if(!mBemcTTable) {
385  TDataSet *DB = mChain->GetInputDB("Calibrations/emc/map");
386  if(DB) mBemcTTable = static_cast<St_bemcMap*>(DB->Find("bemcMap"));
387  }
388  } else {
389  if(mBemcDirty) reload_dbtable(mBemcTable);
390  mBemcDirty = false;
391  }
392  break;
393 
394  case kBarrelEmcPreShowerId:
395  if(mChain) {
396  if(!mBprsTTable) {
397  TDataSet *DB = mChain->GetInputDB("Calibrations/emc/map");
398  if(DB) mBprsTTable = static_cast<St_bprsMap*>(DB->Find("bprsMap"));
399  }
400  } else {
401  if(mBprsDirty) reload_dbtable(mBprsTable);
402  mBprsDirty = false;
403  }
404  break;
405 
406  case kBarrelSmdEtaStripId:
407  if(mChain) {
408  if(!mSmdeTTable) {
409  TDataSet *DB = mChain->GetInputDB("Calibrations/emc/map");
410  if(DB) mSmdeTTable = static_cast<St_bsmdeMap*>(DB->Find("bsmdeMap"));
411  }
412  } else {
413  if(mSmdeDirty) reload_dbtable(mSmdeTable);
414  mSmdeDirty = false;
415  }
416  break;
417 
418  case kBarrelSmdPhiStripId:
419  if(mChain) {
420  if(!mSmdpTTable) {
421  TDataSet *DB = mChain->GetInputDB("Calibrations/emc/map");
422  if(DB) mSmdpTTable = static_cast<St_bsmdpMap*>(DB->Find("bsmdpMap"));
423  }
424  } else {
425  if(mSmdpDirty) reload_dbtable(mSmdpTable);
426  mSmdpDirty = false;
427  }
428  default: break;
429  }
430 }
431 
432 void StEmcMappingDb::reload_dbtable(StDbTable* table) const {
433  if (!table) return;
434  LOG_DEBUG << "(re)loading mapping table using StDbManager" << endm;
436  if (mgr) {
437  mgr->setVerbose(false);
438  mgr->setRequestTime(mBeginTime.AsSQLString());
439  mgr->fetchDbTable(table);
440  }
441 }
442 
443 bool StEmcMappingDb::maybe_reset_cache(StDetectorId det) const {
444  // caches are proactively reset when flipping dirty flags, so ...
445  if(!mChain) return false;
446 
447  Int_t version;
448  switch(det) {
449  case kBarrelEmcTowerId:
450  if((version = St_db_Maker::GetValidity(mBemcTTable,NULL)) != mBemcValidity) {
451  mBemcValidity = version;
452  reset_bemc_cache();
453  return true;
454  }
455  break;
456 
457  case kBarrelEmcPreShowerId:
458  if((version = St_db_Maker::GetValidity(mBprsTTable,NULL)) != mBprsValidity) {
459  mBprsValidity = version;
460  reset_bprs_cache();
461  return true;
462  }
463  break;
464 
465  case kBarrelSmdEtaStripId:
466  if((version = St_db_Maker::GetValidity(mSmdeTTable,NULL)) != mSmdeValidity) {
467  mSmdeValidity = version;
468  reset_smde_cache();
469  return true;
470  }
471  break;
472 
473  case kBarrelSmdPhiStripId:
474  if((version = St_db_Maker::GetValidity(mSmdpTTable,NULL)) != mSmdpValidity) {
475  mSmdpValidity = version;
476  reset_smdp_cache();
477  return true;
478  }
479  default: break;
480  }
481  return false;
482 }
483 
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));
488 }
489 
490 void StEmcMappingDb::reset_bprs_cache() const {
491  memset(mCacheBprsRdo, -1, sizeof(mCacheBprsRdo));
492 }
493 
494 void StEmcMappingDb::reset_smde_cache() const {
495  memset(mCacheSmdRdo, -1, sizeof(mCacheSmdRdo));
496 }
497 
498 void StEmcMappingDb::reset_smdp_cache() const {
499  memset(mCacheSmdRdo, -1, sizeof(mCacheSmdRdo));
500 }
501 
502 /*****************************************************************************
503  * $Log: StEmcMappingDb.cxx,v $
504  * Revision 1.9 2010/01/28 13:45:06 mattheww
505  * update from Oleksandr to protect against NULL pointers
506  *
507  * Revision 1.7 2009/11/17 14:19:28 mattheww
508  * fixed bug in some if statements
509  *
510  * Revision 1.6 2009/04/20 16:38:57 mattheww
511  * fixed case statements so that we can compile SL305
512  *
513  * Revision 1.5 2009/02/01 17:34:52 kocolosk
514  * more caching and optimization.
515  *
516  * Last StEmcMapping commit was bad and left header, implementation
517  * inconsistent. This commit fixes the AutoBuild.
518  *
519  * Revision 1.1 2009/01/08 02:16:18 kocolosk
520  * move StEmcMappingDb/StEmcDecoder to StEmcUtil/database
521  *
522  * Revision 2.1 2008/12/05 19:05:32 kocolosk
523  * new DB-backed implementation of StEmcDecoder
524  *
525  *****************************************************************************/
void version(std::ostream &os=std::cout)
print HepMC version
Definition: Version.h:27
void SetDateTime(int date, int time)
St_db_Maker-compatible interface.
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362