File indexing completed on 2026-09-27 08:21:54
0001 #include "Setup.h"
0002 #include "RootSetupWrapper.h"
0003 #include <iostream>
0004 #include <fstream>
0005 #include "TObjArray.h"
0006 #include "TObjString.h"
0007 #include "utility"
0008
0009 Setup* Setup::instancePtr = nullptr;
0010
0011 ClassImp(Setup);
0012
0013 bool Setup::Initialize(TString file, int debug){
0014
0015 std::cout << "entered setup initialize" << std::endl;
0016 if(isInit){
0017 std::cout<<"Already initialized, bailing out without action"<<std::endl;
0018 return true;
0019 }
0020 std::fstream input;
0021 input.open(file.Data(),std::ios::in);
0022 if(!input.is_open()){
0023 std::cout<<"Could not open "<<file<<std::endl;
0024 std::cout<<"Leaving uninitialized"<<std::endl;
0025 return false;
0026 }
0027 nMaxLayer = -1;
0028 nMaxRow = -1;
0029 nMaxColumn = -1;
0030 nMaxModule = -1;
0031 nMaxROUnit = -1;
0032 maxCellID = -1;
0033 int AROunit,AROchannel,Alayer,AROlayer,Arow,Acolumn,Amod,segSize;
0034 TString Anassembly;
0035 int Akey;
0036
0037 for( TString tempLine; tempLine.ReadLine(input, kTRUE); ) {
0038
0039 if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0040 continue;
0041 }
0042 if (debug > 10) std::cout << tempLine.Data() << std::endl;
0043 TObjArray *tempArr = tempLine.Tokenize("\t");
0044 if(tempArr->GetEntries()<1){
0045 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0046 delete tempArr;
0047 continue;
0048 } else if (tempArr->GetEntries() == 1 ){
0049
0050 tempArr = tempLine.Tokenize(" ");
0051 if(tempArr->GetEntries()<1){
0052 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0053 delete tempArr;
0054 continue;
0055 } else if (tempArr->GetEntries() == 1 ) {
0056 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " no correct format detected" << std::endl;
0057 delete tempArr;
0058 continue;
0059 }
0060 }
0061
0062 if (tempLine.BeginsWith("sumOpt")){
0063 sumOpt = ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();
0064 delete tempArr;
0065 continue;
0066 }
0067
0068 if (tempArr->GetEntries()<10){
0069 if (debug > 1) std::cout << "line not conform with mapping format, skipping" << std::endl;
0070 delete tempArr;
0071 continue;
0072 }
0073
0074 AROunit = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0075 AROchannel = ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();
0076 Alayer = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0077 Anassembly = ((TString)((TObjString*)tempArr->At(3))->GetString());
0078 AROlayer = ((TString)((TObjString*)tempArr->At(4))->GetString()).Atoi();
0079 Arow = ((TString)((TObjString*)tempArr->At(5))->GetString()).Atoi();
0080 Acolumn = ((TString)((TObjString*)tempArr->At(6))->GetString()).Atoi();
0081 Amod = ((TString)((TObjString*)tempArr->At(7))->GetString()).Atoi();
0082 float AmodX = ((TString)((TObjString*)tempArr->At(8))->GetString()).Atof();
0083 float AmodY = ((TString)((TObjString*)tempArr->At(9))->GetString()).Atof();
0084 segSize = ((TString)((TObjString*)tempArr->At(10))->GetString()).Atoi();
0085
0086
0087
0088 ModPos[Amod]=std::make_pair(AmodX,AmodY);
0089
0090
0091
0092 SegmentSum[Alayer] = segSize;
0093
0094 Akey=(Amod<<9)+(Arow<<8)+(Acolumn<<6)+(Alayer);
0095 assemblyID[Akey] = Anassembly;
0096 ROunit [Akey] = AROunit;
0097 ROchannel [Akey] = AROchannel;
0098 Board [Akey] = AROlayer;
0099 CellIDfromRO[std::make_pair(AROunit,AROchannel)]=Akey;
0100 if (nMaxLayer < Alayer) nMaxLayer = Alayer;
0101 if (nMaxRow < Arow) nMaxRow = Arow;
0102 if (nMaxColumn < Acolumn) nMaxColumn = Acolumn;
0103 if (nMaxModule < Amod) nMaxModule = Amod;
0104 if (nMaxROUnit < AROunit) nMaxROUnit = AROunit;
0105 if (maxCellID < Akey) maxCellID = Akey;
0106 if (debug > 10)std::cout <<AROunit<< "\t" << AROchannel << "\t"<< Alayer << "\t"<< Anassembly << "\t"<< AROlayer << "\t row: "<< Arow << "\t col: "<< Acolumn << "\t mod: "<< Amod << "\t"<< AmodX<< "\t"<< AmodY<< "\t"<< segSize<< std::endl;
0107 if (debug > 1)std::cout << "registered cell: " << DecodeCellID(Akey).Data() << std::endl;
0108 }
0109
0110 input.close();
0111 isInit=true;
0112 return isInit;
0113 }
0114
0115 bool Setup::Initialize(RootSetupWrapper& rsw){
0116 std::cout<<rsw.isInit<<"\t"<<rsw.assemblyID.size()<<std::endl;
0117 isInit =rsw.isInit;
0118 assemblyID =rsw.assemblyID;
0119 ROunit =rsw.ROunit;
0120 ROchannel =rsw.ROchannel;
0121 Board =rsw.Board;
0122 CellIDfromRO =rsw.CellIDfromRO;
0123 nMaxLayer =rsw.nMaxLayer;
0124 nMaxRow =rsw.nMaxRow;
0125 nMaxColumn =rsw.nMaxColumn;
0126 nMaxModule =rsw.nMaxModule;
0127 nMaxROUnit =rsw.nMaxROUnit;
0128 maxCellID =rsw.maxCellID;
0129 ModPos =rsw.ModPos;
0130 SegmentSum =rsw.SegmentSum;
0131 sumOpt =rsw.sumOpt;
0132 return isInit;
0133 }
0134
0135 bool Setup::IsInit() const{
0136 return isInit;
0137 }
0138
0139 TString Setup::GetAssemblyID(int cellID) const{
0140 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0141 if(it!=assemblyID.end()) return it->second;
0142 else return "";
0143 }
0144
0145 TString Setup::GetAssemblyID(int row, int col, int lay, int mod=0) const{
0146 return GetAssemblyID((mod<<9)+(row<<8)+(col<<6)+lay);
0147 }
0148
0149 int Setup::GetCellID(int roboard, int roch) const{
0150 std::map<std::pair<int,int>,int>::const_iterator it=CellIDfromRO.find(std::make_pair(roboard,roch));
0151 if(it!=CellIDfromRO.cend()) return it->second;
0152 else return -1;
0153 }
0154
0155 int Setup::GetCellID(int row, int col, int lay, int mod=0)const {
0156 return (mod<<9)+(row<<8)+(col<<6)+lay;
0157 }
0158
0159 int Setup::GetColumn(int cellID) const{
0160 return (cellID&(3<<6))>>6;
0161 }
0162
0163 int Setup::GetLayer(int cellID) const{
0164 return (cellID&((1<<6)-1));
0165 }
0166
0167 int Setup::GetModule(int cellID) const{
0168 return cellID>>9;
0169 }
0170
0171 int Setup::GetROchannel(int cellID) const{
0172 std::map<int, int>::const_iterator it=ROchannel.find(cellID);
0173 if(it!=ROchannel.end()) return it->second;
0174 else return -999;
0175 }
0176
0177 int Setup::GetROchannel(int row, int col, int lay, int mod=0) const{
0178 return GetROchannel((mod<<9)+(row<<8)+(col<<6)+lay);
0179 }
0180
0181 int Setup::GetROunit(int cellID) const{
0182 std::map<int,int>::const_iterator it=ROunit.find(cellID);
0183 if(it!=ROunit.end()) return it->second;
0184 else return -999;
0185 }
0186
0187 int Setup::GetROunit(int row, int col, int lay, int mod=0) const{
0188 return GetROunit((mod<<9)+(row<<8)+(col<<6)+lay);
0189 }
0190
0191 int Setup::GetRow(int cellID) const{
0192 return (cellID&(1<<8))>>8;
0193 }
0194
0195 double Setup::GetModuleX(int mod)const {
0196 auto it = ModPos.find(mod);
0197 if (it != ModPos.end()) {
0198
0199 return static_cast<double>(it->second.first);
0200 }
0201 else {
0202 std::cerr << "Warning: Module " << mod << " not found in ModPos\n";
0203 return -999.0;
0204 }
0205 }
0206
0207 double Setup::GetModuleY(int mod)const {
0208 auto it = ModPos.find(mod);
0209 if (it != ModPos.end()) {
0210 return static_cast<double>(it->second.second);
0211 }
0212 else {
0213 std::cerr << "Warning: Module " << mod << " not found in ModPos\n";
0214 return -999.0;
0215 }
0216 }
0217
0218 int Setup::GetTotalNbChannels(void) const {
0219 return (int) ROunit.size();
0220 }
0221
0222 double Setup::GetX(int cellID) const{
0223 int col=GetColumn(cellID);
0224 return -7.5+col*cellW + GetModuleX(GetModule(cellID));
0225 }
0226
0227 double Setup::GetY(int cellID) const{
0228 int row=GetRow(cellID);
0229 return -2.5+row*cellH + GetModuleY(GetModule(cellID));
0230 }
0231
0232 double Setup::GetZ(int cellID) const{
0233 int lay=GetLayer(cellID);
0234 if (sumOpt == 0){
0235 return cellD/2 + lay*cellD;
0236 } else {
0237 double tempZ = 0;
0238 int row = GetRow(cellID);
0239 int col = GetColumn(cellID);
0240 int mod = GetModule(cellID);
0241 for (int l = 0; l < lay; l++){
0242 long tempCellID = GetCellID(row, col,l, mod);
0243 tempZ = tempZ+GetSegmentDepth(tempCellID);
0244 }
0245 tempZ = tempZ+GetSegmentDepth(cellID)/2.;
0246 return tempZ;
0247 }
0248 }
0249
0250 int Setup::GetLayersInSegmentFromLayer(int layerNr) const{
0251 auto it = SegmentSum.find(layerNr);
0252 if (it != SegmentSum.end())
0253 return it->second;
0254 return 1;
0255 }
0256
0257 int Setup::GetLayersInSegment(int cellID) const{
0258 int lay=GetLayer(cellID);
0259 auto it = SegmentSum.find(lay);
0260 if (it != SegmentSum.end())
0261 return it->second;
0262 return 1;
0263 }
0264
0265 double Setup::GetSegmentDepth(int cellID) const{
0266 int lay=GetLayer(cellID);
0267 auto it = SegmentSum.find(lay);
0268 if (it != SegmentSum.end())
0269 return it->second*cellD;
0270 return cellD;
0271 }
0272
0273
0274 TString Setup::DecodeCellID(int cellID) const{
0275 TString out = Form("cell ID: %d ==> RO unit %d RO channel %d module %d layer %d column %d row %d", cellID, GetROunit(cellID), GetROchannel(cellID), GetModule(cellID), GetLayer(cellID), GetColumn(cellID), GetRow(cellID));
0276 return out;
0277 }
0278
0279
0280 int Setup::GetNMaxLayer() const{
0281 return nMaxLayer;
0282 }
0283
0284 int Setup::GetNMaxRow() const{
0285 return nMaxRow;
0286 }
0287
0288 int Setup::GetNMaxColumn() const{
0289 return nMaxColumn;
0290 }
0291
0292 int Setup::GetNMaxModule() const{
0293 return nMaxModule;
0294 }
0295
0296 int Setup::GetNMaxROUnit() const{
0297 return nMaxROUnit;
0298 }
0299
0300 int Setup::GetNMaxKCUs() const{
0301 return (int)(nMaxROUnit/2+1);
0302 }
0303
0304 int Setup::GetMaxCellID() const{
0305 return maxCellID;
0306 }
0307
0308 int Setup::GetMaxChannelInLayerFull() const{
0309 int maxChInLayer = (nMaxColumn+1)*(nMaxRow+1)*(nMaxModule+1);
0310 return maxChInLayer;
0311 }
0312
0313
0314 int Setup::GetChannelInLayer(int cellID) const{
0315 int row = GetRow(cellID);
0316 int column = GetColumn(cellID);
0317 int absChL = row*(nMaxColumn+1)+column;
0318 return absChL;
0319 }
0320
0321 int Setup::GetChannelInLayerFull(int cellID, DetConf::Type type) const{
0322 int row = GetRow(cellID);
0323 int column = GetColumn(cellID);
0324 int mod = GetModule(cellID);
0325 int absChL = -1;
0326
0327 if (type == DetConf::Type::Unset){
0328 type = GetDetectorConfig();
0329 }
0330
0331 if ( type == DetConf::Type::Dual8M){
0332 absChL = mod*((nMaxColumn+1)*(nMaxRow+1))+row*(nMaxColumn+1)+column;
0333
0334 } else if ( type == DetConf::Type::MediumTB){
0335 if (mod%2 == 0){
0336 absChL = mod%2*((nMaxColumn+1) *(nMaxRow+1))+row*(nMaxColumn+1)*2+column+(int)mod/2*16;
0337 } else {
0338 absChL = (mod%2-1)*((nMaxColumn+1) *(nMaxRow+1))+row*(nMaxColumn+1)*2+column+(int)(mod-1)/2*16 + (nMaxColumn+1);
0339 }
0340
0341 } else if ( type == DetConf::Type::Single8M){
0342 absChL = row*(nMaxColumn+1)+column;
0343
0344 } else if ( type == DetConf::Type::FocalH || type == DetConf::Type::Asic){
0345 absChL = mod*((nMaxColumn+1)*(nMaxRow+1))+row*(nMaxColumn+1)+column;
0346 }
0347 return absChL;
0348 }
0349
0350 int Setup::GetAbsRow(int cellID, DetConf::Type type) const{
0351 int row = GetRow(cellID);
0352 int mod = GetModule(cellID);
0353 int absRow = row;
0354
0355 if (type == DetConf::Type::Unset){
0356 type = GetDetectorConfig();
0357 }
0358
0359 if ( type == DetConf::Type::Dual8M){
0360 absRow = mod * (nMaxRow+1) + row;
0361
0362
0363 } else if ( type == DetConf::Type::MediumTB){
0364 if (mod%2 == 0){
0365 absRow = int(mod/2) * (nMaxRow+1) + row;
0366 } else {
0367 absRow = int((mod-1)/2)* (nMaxRow+1) + row;
0368 }
0369
0370 } else if ( type == DetConf::Type::Single8M){
0371 absRow = row;
0372
0373 } else if ( type == DetConf::Type::FocalH || type == DetConf::Type::Asic){
0374 absRow = row;
0375 }
0376 return absRow;
0377 }
0378
0379 int Setup::GetAbsColumn(int cellID, DetConf::Type type) const{
0380 int col = GetColumn(cellID);
0381 int mod = GetModule(cellID);
0382 int absCol = col;
0383
0384 if (type == DetConf::Type::Unset){
0385 type = GetDetectorConfig();
0386 }
0387
0388 if ( type == DetConf::Type::Dual8M){
0389 absCol = col;
0390
0391 } else if ( type == DetConf::Type::MediumTB){
0392
0393 absCol = mod%2 * (nMaxColumn+1) + col;
0394
0395
0396 } else if ( type == DetConf::Type::Single8M){
0397 absCol = col;
0398
0399 } else if ( type == DetConf::Type::FocalH || type == DetConf::Type::Asic){
0400 absCol = col;
0401 }
0402 return absCol;
0403 }
0404
0405
0406
0407 int Setup::GetAbsNMaxROChannel() const{
0408 int max = -1;
0409 std::map<int, int>::const_iterator it;
0410 for(it=ROchannel.begin(); it!=ROchannel.end(); ++it){
0411 if( it->second > max ) max = it->second;
0412 }
0413 return max;
0414 }
0415
0416 float Setup::GetMinX() const{
0417 float min = 1e6;
0418 std::map<int, TString>::const_iterator it;
0419 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0420 int cellID = it->first;
0421 if( GetX(cellID) < min ) min = GetX(cellID);
0422 }
0423 return min-(cellW/2);
0424 }
0425
0426 float Setup::GetMaxX() const{
0427 float max = -1e6;
0428 std::map<int, TString>::const_iterator it;
0429 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0430 int cellID = it->first;
0431 if( GetX(cellID) > max ) max = GetX(cellID);
0432 }
0433 return max+(cellW/2);
0434 }
0435
0436 float Setup::GetMinY() const{
0437 float min = 1e6;
0438 std::map<int, TString>::const_iterator it;
0439 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0440 int cellID = it->first;
0441 if( GetY(cellID) < min ) min = GetY(cellID);
0442 }
0443 return min-(cellH/2);
0444 }
0445
0446 float Setup::GetMaxY() const{
0447 float max = -1e6;
0448 std::map<int, TString>::const_iterator it;
0449 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0450 int cellID = it->first;
0451 if( GetY(cellID) > max ) max = GetY(cellID);
0452 }
0453 return max+(cellH/2);
0454 }
0455
0456 float Setup::GetMinZ() const{
0457 float min = 1e6;
0458 std::map<int, TString>::const_iterator it;
0459 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0460 int cellID = it->first;
0461 if( GetZ(cellID) < min ) min = GetZ(cellID)-(GetSegmentDepth(cellID)/2);
0462 }
0463 return min;
0464 }
0465
0466 float Setup::GetMaxZ() const{
0467 float max = -1e6;
0468 std::map<int, TString>::const_iterator it;
0469 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0470 int cellID = it->first;
0471 if( GetZ(cellID) > max ) max = GetZ(cellID)+(GetSegmentDepth(cellID)/2);
0472 }
0473 return max;
0474 }
0475
0476 bool Setup::IsLayerOn(int layer, int mod) const{
0477 bool isOn = false;
0478 for (int r = 0; r< GetNMaxRow(); r++){
0479 for (int c = 0; c < GetNMaxColumn(); c++){
0480 if (mod == -1){
0481 for (int m = 0; m < GetNMaxModule(); m++){
0482 int cellID = GetCellID(r, c, layer, m);
0483 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0484 if (it != assemblyID.end()){
0485 isOn = true;
0486 break;
0487 }
0488 }
0489 } else {
0490 int cellID = GetCellID(r, c, layer, mod);
0491 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0492 if (it != assemblyID.end()){
0493 isOn = true;
0494 break;
0495 }
0496 }
0497 }
0498 }
0499 return isOn;
0500 }
0501
0502 bool Setup::IsAsicOn(int asic) const{
0503 bool isOn = false;
0504 for (int ch = 0; ch< 76; ch++){
0505 int cellID = GetCellID(asic, ch);
0506 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0507 if (it != assemblyID.end()){
0508 isOn = true;
0509 break;
0510 }
0511 }
0512 return isOn;
0513 }
0514
0515
0516 int Setup::GetNActiveLayers() const{
0517 int nActLayer = 0;
0518 for (Int_t l = 0; l < GetNMaxLayer()+1; l++){
0519 if (IsLayerOn(l,-1)) nActLayer++;
0520 }
0521 return nActLayer;
0522 }
0523
0524 int Setup::GetNActiveCells() const{
0525 int nActCells = assemblyID.size();
0526 return nActCells;
0527 }
0528
0529 float Setup::GetCellWidth() const{
0530 return cellW;
0531 }
0532
0533 float Setup::GetCellHeight() const{
0534 return cellH;
0535 }
0536
0537 float Setup::GetCellDepth() const{
0538 return cellD;
0539 }
0540
0541 DetConf::Type Setup::GetDetectorConfig() const{
0542 DetConf::Type type;
0543 if (GetNMaxModule()+1 == 2){
0544 if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0545 type = DetConf::Type::Dual8M;
0546 else
0547 type = DetConf::Type::Undef;
0548 } else if (GetNMaxModule()+1 == 8){
0549 if(GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0550 type = DetConf::Type::LargeTB;
0551 else
0552 type = DetConf::Type::Undef;
0553 } else if (GetNMaxModule()+1 == 6){
0554 if(GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0555 type = DetConf::Type::MediumTB;
0556 else
0557 type = DetConf::Type::Undef;
0558 } else if (GetNMaxModule()+1 == 1){
0559 if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0560 type = DetConf::Type::Single8M;
0561 else if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 2)
0562 type = DetConf::Type::Single4M;
0563 else if (GetNMaxRow()+1 == 1 && GetNMaxColumn()+1 == 2)
0564 type = DetConf::Type::Single2MH;
0565 else if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 1)
0566 type = DetConf::Type::Single2MV;
0567 else if (GetNMaxRow()+1 == 1 && GetNMaxColumn()+1 == 1)
0568 type = DetConf::Type::SingleTile;
0569 else
0570 type = DetConf::Type::Undef;
0571 } else {
0572 type = DetConf::Type::Undef;
0573 }
0574 return type;
0575 }
0576
0577 int Setup::GetAbsMaxColumnsSetup( DetConf::Type type){
0578 int maxCol = nMaxColumn+1;
0579
0580 if ( type == DetConf::Type::Dual8M){
0581 maxCol = nMaxColumn+1;
0582
0583 } else if ( type == DetConf::Type::MediumTB){
0584 maxCol = (nMaxColumn+1)*2;
0585
0586 } else if ( type == DetConf::Type::Single8M){
0587 maxCol = nMaxColumn+1;
0588
0589 } else if ( type == DetConf::Type::FocalH || type == DetConf::Type::Asic){
0590 maxCol = 8;
0591 }
0592 return maxCol;
0593 }
0594
0595 int Setup::GetAbsMaxRowsSetup( DetConf::Type type){
0596 int maxRow = nMaxRow+1;
0597
0598 if ( type == DetConf::Type::Dual8M){
0599 maxRow = (nMaxRow+1)*2;
0600
0601 } else if ( type == DetConf::Type::MediumTB){
0602 maxRow = (nMaxRow+1)*3;
0603
0604 } else if ( type == DetConf::Type::Single8M){
0605 maxRow = nMaxRow+1;
0606
0607 } else if ( type == DetConf::Type::FocalH || type == DetConf::Type::Asic){
0608 maxRow = 8;
0609 }
0610 return maxRow;
0611 }
0612
0613
0614 bool Setup::ContainedInSetup(int cellID) const{
0615 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0616 if(it!=assemblyID.end()) return true;
0617 else return false;
0618 }
0619