00001 #include "StEmcDecoder.h"
00002 #include <time.h>
00003 #include <stdlib.h>
00004 #include <Stiostream.h>
00005 #include <stdio.h>
00006 #ifdef IN_PANITKIN
00007 #include <TFile.h>
00008 #endif
00009 #include "StMessMgr.h"
00010
00011 #include "StEmcMappingDb.h"
00012
00013 ClassImp(StEmcDecoder)
00014
00015 StEmcDecoder::StEmcDecoder(unsigned date, unsigned time, bool TowerMapBug) {
00016 mapping = StEmcMappingDb::instance();
00017
00018 mTowerMapBug = TowerMapBug;
00019
00020
00021 for(int i=0;i<4800;i++) TowerBugFixIndex[i] = i+1;
00022 if(date >= 20040101 && date < 20050101) {
00023 #include "TowerBug2004.txt"
00024 }
00025 if(date >= 20050101 && date < 20060101) {
00026 #include "TowerBug2005.txt"
00027 }
00028 for(int i=0;i<4800;i++) {
00029 int id = i+1;
00030 if(TowerBugFixIndex[i]!=id) {
00031 int newId = TowerBugFixIndex[i];
00032 TowerBugFixIndex[newId-1] = id;
00033 }
00034 }
00035
00036
00037 for(int i=0; i<4800; i++) PreshowerBugFixIndex[i] = i+1;
00038 if(date >= 20060101 && date < 20071101) {
00039 #include "PreshowerBug2007.txt"
00040 }
00041 for(int i=0; i<4800; i++) {
00042 int id = i+1;
00043 if(PreshowerBugFixIndex[i] != id) {
00044 int newId = PreshowerBugFixIndex[i];
00045 PreshowerBugFixIndex[newId-1] = id;
00046 }
00047 }
00048
00049
00050 for(int i=0;i<18000;i++) SmdBugFixIndex[i] = i+1;
00051 if(date >= 20100101 && date < 20110101) {
00052 #include "SmdBug2010.txt"
00053 }
00054 }
00055
00056 StEmcDecoder::~StEmcDecoder() { }
00057
00058 void StEmcDecoder::SetDateTime(unsigned int date, unsigned int time) {
00059 if (mapping) mapping->SetDateTime(date, time);
00060 }
00061
00062 bool StEmcDecoder::GetFixTowerMapBug(void) const {
00063 return mTowerMapBug;
00064 }
00065
00066 void StEmcDecoder::SetFixTowerMapBug(bool fix) {
00067 mTowerMapBug = fix;
00068 }
00069
00076 int StEmcDecoder::GetTowerBugCorrectionShift(int id_original,int& id_shift) const
00077 {
00078 int id_new = TowerBugFixIndex[id_original - 1];
00079 id_shift = id_new-id_original;
00080 return 1;
00081 }
00082
00089 int StEmcDecoder::GetPreshowerBugCorrectionShift(int id_original,int& id_shift) const
00090 {
00091 int id_new = PreshowerBugFixIndex[id_original - 1];
00092 id_shift = id_new-id_original;
00093 return 1;
00094 }
00095
00102 int StEmcDecoder::GetSmdBugCorrectionShift(int id_original,int& id_shift) const
00103 {
00104 int id_new = SmdBugFixIndex[id_original - 1];
00105 id_shift = id_new-id_original;
00106 return 1;
00107 }
00108
00116 int StEmcDecoder::
00117 GetTowerBin(const int TowerId,int &module,int &eta,int &sub) const {
00118 if(TowerId<1 || TowerId>4800) return 0;
00119 if (!mapping) return 0;
00120 if (!mapping->bemc()) return 0;
00121 module = mapping->bemc(TowerId).m;
00122 eta = mapping->bemc(TowerId).e;
00123 sub = mapping->bemc(TowerId).s;
00124 return 1;
00125 }
00126
00131 int StEmcDecoder::GetTowerCrateFromTDC(int TDC, int& crate) const {
00132 if(TDC<0 || TDC>29) return 0;
00133 if (!mapping) return 0;
00134 if (!mapping->bemc()) return 0;
00135 short id = mapping->softIdFromTDC(kBarrelEmcTowerId, TDC, 0);
00136 crate = mapping->bemc(id).crate;
00137 return 1;
00138 }
00139
00144 int StEmcDecoder::GetTowerTDCFromCrate(int crate, int& TDC) const {
00145 if(crate<1 || crate>30) return 0;
00146 if (!mapping) return 0;
00147 if (!mapping->bemc()) return 0;
00148 short id = mapping->softIdFromCrate(kBarrelEmcTowerId, crate, 0);
00149 TDC = mapping->bemc(id).TDC;
00150 return 1;
00151 }
00152
00157 int StEmcDecoder::GetTowerTDCFromDaqId(int RDO, int& TDC) const {
00158 if (!mapping) return 0;
00159 if (!mapping->bemc()) return 0;
00160 short id;
00161 if((id = mapping->softIdFromDaqId(kBarrelEmcTowerId, RDO))) {
00162 TDC = mapping->bemc(id).TDC;
00163 return 1;
00164 }
00165 return 0;
00166 }
00167
00173 int StEmcDecoder::
00174 GetTowerCrateFromDaqId(int RDO, int& crate, int& crate_sequency) const {
00175 if (!mapping) return 0;
00176 if (!mapping->bemc()) return 0;
00177 short id;
00178 if((id = mapping->softIdFromDaqId(kBarrelEmcTowerId, RDO))) {
00179 crate = mapping->bemc(id).crate;
00180 crate_sequency = mapping->bemc(id).crateChannel;
00181 return 1;
00182 }
00183 return 0;
00184 }
00185
00190 int StEmcDecoder::GetTowerIdFromDaqId(int RDO,int& TowerId) const {
00191 if (!mapping) return 0;
00192 short id;
00193 if((id = mapping->softIdFromDaqId(kBarrelEmcTowerId, RDO))) {
00194 TowerId = id;
00195 return 1;
00196 }
00197 return 0;
00198 }
00199
00204 int StEmcDecoder::GetDaqIdFromTowerId(int TowerId,int& RDO) const {
00205 if(TowerId<1 || TowerId >4800) return 0;
00206 if (!mapping) return 0;
00207 if (!mapping->bemc()) return 0;
00208 RDO = mapping->bemc(TowerId).daqID;
00209 return 1;
00210 }
00211
00217 int StEmcDecoder::
00218 GetTowerIdFromCrate(int crate,int crate_sequency, int& TowerId) const {
00219 if(crate < 1 || crate > 30) return 0;
00220 if(crate_sequency < 0 || crate_sequency > 159) return 0;
00221 if (!mapping) return 0;
00222 TowerId = mapping->softIdFromCrate(kBarrelEmcTowerId, crate, crate_sequency);
00223 return 1;
00224 }
00225
00231 int StEmcDecoder::
00232 GetTowerIdFromTDC(int TDC,int tdc_sequency, int& TowerId) const {
00233 if(TDC < 0 || TDC > 29) return 0;
00234 if(tdc_sequency < 0 || tdc_sequency > 159) return 0;
00235 if (!mapping) return 0;
00236 TowerId = mapping->softIdFromTDC(kBarrelEmcTowerId, TDC, tdc_sequency);
00237 return 1;
00238 }
00239
00245 int StEmcDecoder::
00246 GetTriggerPatchFromCrate(int CRATE,int crate_seq, int& patchId) const {
00247 if(CRATE < 1 || CRATE > 30) return 0;
00248 if(crate_seq < 0 || crate_seq > 159) return 0;
00249 if (!mapping) return 0;
00250 if (!mapping->bemc()) return 0;
00251 short id = mapping->softIdFromCrate(kBarrelEmcTowerId, CRATE, crate_seq);
00252 patchId = mapping->bemc(id).triggerPatch;
00253 return 1;
00254 }
00255
00261 int StEmcDecoder::
00262 GetCrateAndSequenceFromTriggerPatch(int PATCH,int& CRATE,int& crate_seq) const {
00263 if(PATCH<0 || PATCH>299) return 0;
00264 if (!mapping) return 0;
00265 if (!mapping->bemc()) return 0;
00266 for(int id=1; id<=4800; id++) {
00267 if(mapping->bemc(id).triggerPatch == PATCH) {
00268 CRATE = mapping->bemc(id).crate;
00269 crate_seq = (PATCH%10)*16;
00270 }
00271 }
00272 return 1;
00273 }
00274
00280 int StEmcDecoder::GetTriggerPatchFromJetPatch(int jetPatch, int jetPatch_seq,
00281 int &triggerPatch) const
00282 {
00283 if(jetPatch<0 || jetPatch>11) return 0;
00284 if (!mapping) return 0;
00285 if (!mapping->bemc()) return 0;
00286 for(int id=1; id<=4800; id++) {
00287 if(mapping->bemc(id).jetPatch == jetPatch) {
00288 triggerPatch = mapping->bemc(id).triggerPatch;
00289 }
00290 }
00291
00292
00293
00294 return 1;
00295 }
00296
00302 int StEmcDecoder::GetJetPatchAndSequenceFromTriggerPatch(int triggerPatch,
00303 int &jetPatch, int &jetPatch_seq) const
00304 {
00305 if (triggerPatch<0 || triggerPatch>299) return 0;
00306 if (!mapping) return 0;
00307 if (!mapping->bemc()) return 0;
00308 for(int id=1; id<=4800; id++) {
00309 if(mapping->bemc(id).triggerPatch == triggerPatch) {
00310 jetPatch = mapping->bemc(id).jetPatch;
00311 }
00312 }
00313
00314 return 1;
00315 }
00316
00317 int StEmcDecoder::GetDSMFromTriggerPatch(int patchId, int &dsmModule) const {
00318 if(patchId < 0 || patchId >= 300) return 0;
00319 dsmModule = patchId/10;
00320 return 1;
00321 }
00322
00323 int StEmcDecoder::
00324 GetTriggerPatchesFromDSM(int dsmModule, int *triggerPatches) const {
00325 if(dsmModule < 0 || dsmModule >= 30) return 0;
00326 for(int i=0; i<10; i++) {
00327 triggerPatches[i] = dsmModule*10 + i;
00328 }
00329 return 1;
00330 }
00331
00332
00341 int StEmcDecoder::GetSmdRDO(int detector, int module, int eta, int sub,
00342 int& RDO, int& index) const
00343 {
00344 if (!mapping) return 0;
00345 if(detector == 3) {
00346 if (!mapping->bsmde()) return 0;
00347 int id = mapping->softIdFromMES(kBarrelSmdEtaStripId, module, eta, sub);
00348 RDO = mapping->bsmde(id).rdo;
00349 index = mapping->bsmde(id).rdoChannel;
00350 return 1;
00351 }
00352 else if(detector == 4) {
00353 if (!mapping->bsmdp()) return 0;
00354 int id = mapping->softIdFromMES(kBarrelSmdPhiStripId, module, eta, sub);
00355 RDO = mapping->bsmdp(id).rdo;
00356 index = mapping->bsmdp(id).rdoChannel;
00357 return 1;
00358 }
00359 return 0;
00360 }
00361
00371 int StEmcDecoder::GetSmdCoord(int RDO, int index, int& detector, int& module,
00372 int& eta, int& sub, bool print) const
00373 {
00374 int wire, A_value;
00375 return GetSmdCoord(RDO,index,detector,module,eta,sub,wire,A_value,print);
00376 }
00377
00389 int StEmcDecoder::GetSmdCoord(int RDO, int index, int& det, int& m, int& e,
00390 int& s, int& wire, int& A_value, bool print) const
00391 {
00392 if (!mapping) return 0;
00393 short id;
00394 if((id = mapping->softIdFromRDO(kBarrelSmdEtaStripId, RDO, index))) {
00395 if (!mapping->bsmde()) return 0;
00396 det = 3;
00397 m = mapping->bsmde(id).m;
00398 e = mapping->bsmde(id).e;
00399 s = mapping->bsmde(id).s;
00400 wire = mapping->bsmde(id).wire;
00401 A_value = mapping->bsmde(id).feeA;
00402 return 1;
00403 }
00404 else if((id = mapping->softIdFromRDO(kBarrelSmdPhiStripId, RDO, index))) {
00405 if (!mapping->bsmdp()) return 0;
00406 det = 4;
00407 m = mapping->bsmdp(id).m;
00408 e = mapping->bsmdp(id).e;
00409 s = mapping->bsmdp(id).s;
00410 wire = mapping->bsmdp(id).wire;
00411 A_value = mapping->bsmdp(id).feeA;
00412 return 1;
00413 }
00414 return 0;
00415 }
00416
00426 int StEmcDecoder::GetPsdId(int RDO, int index, int& id, bool print) const {
00427 int wire, A_value, PMT;
00428 return GetPsdId(RDO, index, id, PMT, wire, A_value, print);
00429 }
00430
00443 int StEmcDecoder::GetPsdId(int RDO,int index, int& softId, int& PMTBox,
00444 int& wire, int& A_value, bool print) const
00445 {
00446 if (!mapping) return 0;
00447 if (!mapping->bprs()) return 0;
00448 if((softId = mapping->softIdFromRDO(kBarrelEmcPreShowerId, RDO, index))) {
00449 PMTBox = mapping->bprs(softId).PMTbox;
00450 wire = mapping->bprs(softId).wire;
00451 A_value = mapping->bprs(softId).feeA;
00452 return 1;
00453 }
00454 return 0;
00455 }
00456
00462 int StEmcDecoder::GetPsdRDO(int id, int& RDO,int& index) const {
00463 if(id<1 || id>4800) return 0;
00464 if (!mapping) return 0;
00465 if (!mapping->bprs()) return 0;
00466 RDO = mapping->bprs(id).rdo;
00467 index = mapping->bprs(id).rdoChannel;
00468 return 1;
00469 }
00470
00471 void StEmcDecoder::PrintTowerMap(ofstream *out) const {
00472 *out <<"TDC channels connections\n";
00473 *out <<"-----------------------------------------------------------\n";
00474
00475
00476 *out <<endl;
00477 *out <<"Tower MAP\n";
00478 *out <<"-----------------------------------------------------------\n";
00479 for(int daq =0;daq<4800;daq++)
00480 {
00481 int towerId,tdc,crate,position,m,e,s;
00482 GetTowerIdFromDaqId(daq,towerId);
00483 GetTowerTDCFromDaqId(daq,tdc);
00484 GetTowerCrateFromDaqId(daq,crate,position);
00485 GetTowerBin(towerId,m,e,s);
00486 *out <<" daq id = "<<daq<<" TDC channel = "<<tdc<<" Crate = "<<crate
00487 <<" position in crate = "<<position<<" software id = "<<towerId
00488 <<" m = "<<m<<" e = "<<e<<" s = "<<s<<endl;
00489 }
00490 *out <<endl;
00491 }
00492
00493 void StEmcDecoder::PrintSmdMap(ofstream *out) const {
00494 *out <<"Modules connected to SMD crate\n";
00495 *out <<"-----------------------------------------------------------\n";
00496
00497 for(int i=0;i<8;i++)
00498 {
00499 *out <<"SMD CRATE number "<<i+1<<endl;
00500
00501
00502
00503 }
00504 *out <<"\nSMD MAP\n";
00505 *out <<"-----------------------------------------------------------\n";
00506
00507 for(int i=0;i<8;i++)
00508 {
00509 *out <<"SMD CRATE number "<<i+1<<endl;
00510 for(int index =0;index<4800;index++)
00511 {
00512 int det,m,e,s;
00513 int status = GetSmdCoord(i,index,det,m,e,s);
00514 *out <<" RDO = "<<i<<" index = "<<index;
00515 if(status == 1)
00516 *out <<" detector = "<<det<<" mod = "<<m<<" eta = "<<e
00517 <<" sub = "<<s<<endl;
00518 else
00519 *out <<" dummy connection\n";
00520 }
00521 }
00522 *out <<endl;
00523 }
00524
00525 void StEmcDecoder::PrintPsdMap(ofstream *out) const {
00526 *out <<"PMT Boxes connected to PSD crate\n";
00527 *out <<"-----------------------------------------------------------\n";
00528
00529 for(int i=0;i<4;i++)
00530 {
00531 *out <<"PSD CRATE number "<<i+1<<endl;
00532
00533
00534
00535 }
00536 *out <<"\nPSD MAP\n";
00537 *out <<"-----------------------------------------------------------\n";
00538
00539 for(int i=0;i<4;i++)
00540 {
00541 *out <<"PSD CRATE number "<<i+1<<endl;
00542 for(int index =0;index<4800;index++)
00543 {
00544 int id;
00545 int status = GetPsdId(i,index,id);
00546 *out <<" RDO = "<<i<<" index = "<<index;
00547 if(status == 1)
00548 *out <<" id = "<<id<<endl;
00549 else
00550 *out <<" dummy connection\n";
00551 }
00552 }
00553 *out <<endl;
00554 }
00555
00556 int StEmcDecoder::GetCrateFromTowerId(int softId, int &crate, int &seq) const {
00557 if(softId<1 || softId>4800) return 0;
00558 if (!mapping) return 0;
00559 if (!mapping->bemc()) return 0;
00560 crate = mapping->bemc(softId).crate;
00561 seq = mapping->bemc(softId).crateChannel;
00562 return 1;
00563 }
00564
00565 int StEmcDecoder::GetTDCFromTowerId(int softId, int &TDC) const {
00566 if(softId<1 || softId>4800) return 0;
00567 if (!mapping) return 0;
00568 if (!mapping->bemc()) return 0;
00569 TDC = mapping->bemc(softId).TDC;
00570 return 1;
00571 }
00572
00573 int StEmcDecoder::GetTriggerPatchFromTowerId(int softId, int &patchId) const {
00574 if(softId<1 || softId>4800) return 0;
00575 if (!mapping) return 0;
00576 if (!mapping->bemc()) return 0;
00577 patchId = mapping->bemc(softId).triggerPatch;
00578 return 1;
00579 }
00580
00581 int StEmcDecoder::GetJetPatchFromTowerId(int softId, int &jetPatch) const {
00582 if(softId<1 || softId>4800) return 0;
00583 if (!mapping) return 0;
00584 if (!mapping->bemc()) return 0;
00585 jetPatch = mapping->bemc(softId).jetPatch;
00586 return 1;
00587 }
00588
00589 int StEmcDecoder::GetTowerIdFromBin(int m, int e, int s, int &softId) const {
00590 if( (m<1) || (m>120) ) return 0;
00591 if( (e<1) || (e>20) ) return 0;
00592 if( (s<1) || (s>2) ) return 0;
00593 if (!mapping) return 0;
00594 softId = mapping->softIdFromMES(kBarrelEmcTowerId, m, e, s);
00595 return 1;
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666