File indexing completed on 2026-07-29 08:25:26
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 for (int l = 0; l < lay; l++){
0239 tempZ = tempZ+GetSegmentDepth(cellID);
0240 }
0241 tempZ = tempZ+GetSegmentDepth(cellID)/2.;
0242 return tempZ;
0243 }
0244 }
0245
0246 int Setup::GetLayersInSegmentFromLayer(int layerNr) const{
0247 auto it = SegmentSum.find(layerNr);
0248 if (it != SegmentSum.end())
0249 return it->second;
0250 return 1;
0251 }
0252
0253 int Setup::GetLayersInSegment(int cellID) const{
0254 int lay=GetLayer(cellID);
0255 auto it = SegmentSum.find(lay);
0256 if (it != SegmentSum.end())
0257 return it->second;
0258 return 1;
0259 }
0260
0261 double Setup::GetSegmentDepth(int cellID) const{
0262 int lay=GetLayer(cellID);
0263 auto it = SegmentSum.find(lay);
0264 if (it != SegmentSum.end())
0265 return it->second*cellD;
0266 return cellD;
0267 }
0268
0269
0270 TString Setup::DecodeCellID(int cellID) const{
0271 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));
0272 return out;
0273 }
0274
0275
0276 int Setup::GetNMaxLayer() const{
0277 return nMaxLayer;
0278 }
0279
0280 int Setup::GetNMaxRow() const{
0281 return nMaxRow;
0282 }
0283
0284 int Setup::GetNMaxColumn() const{
0285 return nMaxColumn;
0286 }
0287
0288 int Setup::GetNMaxModule() const{
0289 return nMaxModule;
0290 }
0291
0292 int Setup::GetNMaxROUnit() const{
0293 return nMaxROUnit;
0294 }
0295
0296 int Setup::GetNMaxKCUs() const{
0297 return (int)(nMaxROUnit/2+1);
0298 }
0299
0300 int Setup::GetMaxCellID() const{
0301 return maxCellID;
0302 }
0303
0304 int Setup::GetMaxChannelInLayerFull() const{
0305 int maxChInLayer = (nMaxColumn+1)*(nMaxRow+1)*(nMaxModule+1);
0306 return maxChInLayer;
0307 }
0308
0309
0310 int Setup::GetChannelInLayer(int cellID) const{
0311 int row = GetRow(cellID);
0312 int column = GetColumn(cellID);
0313 int absChL = row*(nMaxColumn+1)+column;
0314 return absChL;
0315 }
0316
0317 int Setup::GetChannelInLayerFull(int cellID, DetConf::Type type) const{
0318 int row = GetRow(cellID);
0319 int column = GetColumn(cellID);
0320 int mod = GetModule(cellID);
0321 int absChL = -1;
0322
0323 if (type == DetConf::Type::Unset){
0324 type = GetDetectorConfig();
0325 }
0326
0327 if ( type == DetConf::Type::Dual8M){
0328 absChL = mod*((nMaxColumn+1)*(nMaxRow+1))+row*(nMaxColumn+1)+column;
0329 } else if ( type == DetConf::Type::MediumTB){
0330 if (mod%2 == 0){
0331 absChL = mod%2*((nMaxColumn+1) *(nMaxRow+1))+row*(nMaxColumn+1)*2+column+(int)mod/2*16;
0332 } else {
0333 absChL = (mod%2-1)*((nMaxColumn+1) *(nMaxRow+1))+row*(nMaxColumn+1)*2+column+(int)(mod-1)/2*16 + (nMaxColumn+1);
0334 }
0335 } else if ( type == DetConf::Type::Single8M){
0336 absChL = row*(nMaxColumn+1)+column;
0337 } else if ( type == DetConf::Type::FocalH){
0338 absChL = mod*((nMaxColumn+1)*(nMaxRow+1))+row*(nMaxColumn+1)+column;
0339 }
0340 return absChL;
0341 }
0342
0343 int Setup::GetAbsNMaxROChannel() const{
0344 int max = -1;
0345 std::map<int, int>::const_iterator it;
0346 for(it=ROchannel.begin(); it!=ROchannel.end(); ++it){
0347 if( it->second > max ) max = it->second;
0348 }
0349 return max;
0350 }
0351
0352 float Setup::GetMinX() const{
0353 float min = 1e6;
0354 std::map<int, TString>::const_iterator it;
0355 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0356 int cellID = it->first;
0357 if( GetX(cellID) < min ) min = GetX(cellID);
0358 }
0359 return min-(cellW/2);
0360 }
0361
0362 float Setup::GetMaxX() const{
0363 float max = -1e6;
0364 std::map<int, TString>::const_iterator it;
0365 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0366 int cellID = it->first;
0367 if( GetX(cellID) > max ) max = GetX(cellID);
0368 }
0369 return max+(cellW/2);
0370 }
0371
0372 float Setup::GetMinY() const{
0373 float min = 1e6;
0374 std::map<int, TString>::const_iterator it;
0375 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0376 int cellID = it->first;
0377 if( GetY(cellID) < min ) min = GetY(cellID);
0378 }
0379 return min-(cellH/2);
0380 }
0381
0382 float Setup::GetMaxY() const{
0383 float max = -1e6;
0384 std::map<int, TString>::const_iterator it;
0385 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0386 int cellID = it->first;
0387 if( GetY(cellID) > max ) max = GetY(cellID);
0388 }
0389 return max+(cellH/2);
0390 }
0391
0392 float Setup::GetMinZ() const{
0393 float min = 1e6;
0394 std::map<int, TString>::const_iterator it;
0395 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0396 int cellID = it->first;
0397 if( GetZ(cellID) < min ) min = GetZ(cellID)-(GetSegmentDepth(cellID)/2);
0398 }
0399 return min;
0400 }
0401
0402 float Setup::GetMaxZ() const{
0403 float max = -1e6;
0404 std::map<int, TString>::const_iterator it;
0405 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0406 int cellID = it->first;
0407 if( GetZ(cellID) > max ) max = GetZ(cellID)+(GetSegmentDepth(cellID)/2);
0408 }
0409 return max;
0410 }
0411
0412 bool Setup::IsLayerOn(int layer, int mod) const{
0413 bool isOn = false;
0414 for (int r = 0; r< GetNMaxRow(); r++){
0415 for (int c = 0; c < GetNMaxColumn(); c++){
0416 if (mod == -1){
0417 for (int m = 0; m < GetNMaxModule(); m++){
0418 int cellID = GetCellID(r, c, layer, m);
0419 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0420 if (it != assemblyID.end()){
0421 isOn = true;
0422 break;
0423 }
0424 }
0425 } else {
0426 int cellID = GetCellID(r, c, layer, mod);
0427 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0428 if (it != assemblyID.end()){
0429 isOn = true;
0430 break;
0431 }
0432 }
0433 }
0434 }
0435 return isOn;
0436 }
0437
0438 bool Setup::IsAsicOn(int asic) const{
0439 bool isOn = false;
0440 for (int ch = 0; ch< 76; ch++){
0441 int cellID = GetCellID(asic, ch);
0442 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0443 if (it != assemblyID.end()){
0444 isOn = true;
0445 break;
0446 }
0447 }
0448 return isOn;
0449 }
0450
0451
0452 int Setup::GetNActiveLayers() const{
0453 int nActLayer = 0;
0454 for (Int_t l = 0; l < GetNMaxLayer()+1; l++){
0455 if (IsLayerOn(l,-1)) nActLayer++;
0456 }
0457 return nActLayer;
0458 }
0459
0460 int Setup::GetNActiveCells() const{
0461 int nActCells = assemblyID.size();
0462 return nActCells;
0463 }
0464
0465 float Setup::GetCellWidth() const{
0466 return cellW;
0467 }
0468
0469 float Setup::GetCellHeight() const{
0470 return cellH;
0471 }
0472
0473 float Setup::GetCellDepth() const{
0474 return cellD;
0475 }
0476
0477
0478
0479 DetConf::Type Setup::GetDetectorConfig() const{
0480 DetConf::Type type;
0481 if (GetNMaxModule()+1 == 2){
0482 if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0483 type = DetConf::Type::Dual8M;
0484 else
0485 type = DetConf::Type::Undef;
0486 } else if (GetNMaxModule()+1 == 8){
0487 if(GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0488 type = DetConf::Type::LargeTB;
0489 else
0490 type = DetConf::Type::Undef;
0491 } else if (GetNMaxModule()+1 == 6){
0492 if(GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0493 type = DetConf::Type::MediumTB;
0494 else
0495 type = DetConf::Type::Undef;
0496 } else if (GetNMaxModule()+1 == 1){
0497 if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0498 type = DetConf::Type::Single8M;
0499 else if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 2)
0500 type = DetConf::Type::Single4M;
0501 else if (GetNMaxRow()+1 == 1 && GetNMaxColumn()+1 == 2)
0502 type = DetConf::Type::Single2MH;
0503 else if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 1)
0504 type = DetConf::Type::Single2MV;
0505 else if (GetNMaxRow()+1 == 1 && GetNMaxColumn()+1 == 1)
0506 type = DetConf::Type::SingleTile;
0507 else
0508 type = DetConf::Type::Undef;
0509 } else {
0510 type = DetConf::Type::Undef;
0511 }
0512 return type;
0513 }
0514
0515 bool Setup::ContainedInSetup(int cellID) const{
0516 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0517 if(it!=assemblyID.end()) return true;
0518 else return false;
0519 }
0520