File indexing completed on 2026-06-29 07:40:08
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 TObjArray *tempArr = tempLine.Tokenize("\t");
0043 if(tempArr->GetEntries()<1){
0044 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0045 delete tempArr;
0046 continue;
0047 } else if (tempArr->GetEntries() == 1 ){
0048
0049 tempArr = tempLine.Tokenize(" ");
0050 if(tempArr->GetEntries()<1){
0051 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0052 delete tempArr;
0053 continue;
0054 } else if (tempArr->GetEntries() == 1 ) {
0055 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " no correct format detected" << std::endl;
0056 delete tempArr;
0057 continue;
0058 }
0059 }
0060
0061 if (tempLine.BeginsWith("sumOpt")){
0062 sumOpt = ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();
0063 delete tempArr;
0064 continue;
0065 }
0066
0067 if (tempArr->GetEntries()<10){
0068 if (debug > 1) std::cout << "line not conform with mapping format, skipping" << std::endl;
0069 delete tempArr;
0070 continue;
0071 }
0072
0073 AROunit = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0074 AROchannel = ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();
0075 Alayer = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0076 Anassembly = ((TString)((TObjString*)tempArr->At(3))->GetString());
0077 AROlayer = ((TString)((TObjString*)tempArr->At(4))->GetString()).Atoi();
0078 Arow = ((TString)((TObjString*)tempArr->At(5))->GetString()).Atoi();
0079 Acolumn = ((TString)((TObjString*)tempArr->At(6))->GetString()).Atoi();
0080 Amod = ((TString)((TObjString*)tempArr->At(7))->GetString()).Atoi();
0081 float AmodX = ((TString)((TObjString*)tempArr->At(8))->GetString()).Atof();
0082 float AmodY = ((TString)((TObjString*)tempArr->At(9))->GetString()).Atof();
0083 segSize = ((TString)((TObjString*)tempArr->At(10))->GetString()).Atoi();
0084
0085
0086
0087 ModPos[Amod]=std::make_pair(AmodX,AmodY);
0088
0089
0090
0091 SegmentSum[Alayer] = segSize;
0092
0093 Akey=(Amod<<9)+(Arow<<8)+(Acolumn<<6)+(Alayer);
0094 assemblyID[Akey] = Anassembly;
0095 ROunit [Akey] = AROunit;
0096 ROchannel [Akey] = AROchannel;
0097 Board [Akey] = AROlayer;
0098 CellIDfromRO[std::make_pair(AROunit,AROchannel)]=Akey;
0099 if (nMaxLayer < Alayer) nMaxLayer = Alayer;
0100 if (nMaxRow < Arow) nMaxRow = Arow;
0101 if (nMaxColumn < Acolumn) nMaxColumn = Acolumn;
0102 if (nMaxModule < Amod) nMaxModule = Amod;
0103 if (nMaxROUnit < AROunit) nMaxROUnit = AROunit;
0104 if (maxCellID < Akey) maxCellID = Akey;
0105 if (debug > 10)std::cout <<AROunit<< "\t" << AROchannel << "\t"<< Alayer << "\t"<< Anassembly << "\t"<< AROlayer << "\t"<< Arow << "\t"<< Acolumn << "\t"<< Amod << segSize<< std::endl;
0106 if (debug > 1)std::cout << "registered cell: " << DecodeCellID(Akey).Data() << std::endl;
0107 }
0108
0109 input.close();
0110 isInit=true;
0111 return isInit;
0112 }
0113
0114 bool Setup::Initialize(RootSetupWrapper& rsw){
0115 std::cout<<rsw.isInit<<"\t"<<rsw.assemblyID.size()<<std::endl;
0116 isInit =rsw.isInit;
0117 assemblyID =rsw.assemblyID;
0118 ROunit =rsw.ROunit;
0119 ROchannel =rsw.ROchannel;
0120 Board =rsw.Board;
0121 CellIDfromRO =rsw.CellIDfromRO;
0122 nMaxLayer =rsw.nMaxLayer;
0123 nMaxRow =rsw.nMaxRow;
0124 nMaxColumn =rsw.nMaxColumn;
0125 nMaxModule =rsw.nMaxModule;
0126 nMaxROUnit =rsw.nMaxROUnit;
0127 maxCellID =rsw.maxCellID;
0128 ModPos =rsw.ModPos;
0129 SegmentSum =rsw.SegmentSum;
0130 sumOpt =rsw.sumOpt;
0131 return isInit;
0132 }
0133
0134 bool Setup::IsInit() const{
0135 return isInit;
0136 }
0137
0138 TString Setup::GetAssemblyID(int cellID) const{
0139 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0140 if(it!=assemblyID.end()) return it->second;
0141 else return "";
0142 }
0143
0144 TString Setup::GetAssemblyID(int row, int col, int lay, int mod=0) const{
0145 return GetAssemblyID((mod<<9)+(row<<8)+(col<<6)+lay);
0146 }
0147
0148 int Setup::GetCellID(int roboard, int roch) const{
0149 std::map<std::pair<int,int>,int>::const_iterator it=CellIDfromRO.find(std::make_pair(roboard,roch));
0150 if(it!=CellIDfromRO.cend()) return it->second;
0151 else return -1;
0152 }
0153
0154 int Setup::GetCellID(int row, int col, int lay, int mod=0)const {
0155 return (mod<<9)+(row<<8)+(col<<6)+lay;
0156 }
0157
0158 int Setup::GetColumn(int cellID) const{
0159 return (cellID&(3<<6))>>6;
0160 }
0161
0162 int Setup::GetLayer(int cellID) const{
0163 return (cellID&((1<<6)-1));
0164 }
0165
0166 int Setup::GetModule(int cellID) const{
0167 return cellID>>9;
0168 }
0169
0170 int Setup::GetROchannel(int cellID) const{
0171 std::map<int, int>::const_iterator it=ROchannel.find(cellID);
0172 if(it!=ROchannel.end()) return it->second;
0173 else return -999;
0174 }
0175
0176 int Setup::GetROchannel(int row, int col, int lay, int mod=0) const{
0177 return GetROchannel((mod<<9)+(row<<8)+(col<<6)+lay);
0178 }
0179
0180 int Setup::GetROunit(int cellID) const{
0181 std::map<int,int>::const_iterator it=ROunit.find(cellID);
0182 if(it!=ROunit.end()) return it->second;
0183 else return -999;
0184 }
0185
0186 int Setup::GetROunit(int row, int col, int lay, int mod=0) const{
0187 return GetROunit((mod<<9)+(row<<8)+(col<<6)+lay);
0188 }
0189
0190 int Setup::GetRow(int cellID) const{
0191 return (cellID&(1<<8))>>8;
0192 }
0193
0194 double Setup::GetModuleX(int mod)const {
0195 auto it = ModPos.find(mod);
0196 if (it != ModPos.end()) {
0197
0198 return static_cast<double>(it->second.first);
0199 }
0200 else {
0201 std::cerr << "Warning: Module " << mod << " not found in ModPos\n";
0202 return -999.0;
0203 }
0204 }
0205
0206 double Setup::GetModuleY(int mod)const {
0207 auto it = ModPos.find(mod);
0208 if (it != ModPos.end()) {
0209 return static_cast<double>(it->second.second);
0210 }
0211 else {
0212 std::cerr << "Warning: Module " << mod << " not found in ModPos\n";
0213 return -999.0;
0214 }
0215 }
0216
0217 int Setup::GetTotalNbChannels(void) const {
0218 return (int) ROunit.size();
0219 }
0220
0221 double Setup::GetX(int cellID) const{
0222 int col=GetColumn(cellID);
0223 return -7.5+col*cellW + GetModuleX(GetModule(cellID));
0224 }
0225
0226 double Setup::GetY(int cellID) const{
0227 int row=GetRow(cellID);
0228 return -2.5+row*cellH + GetModuleY(GetModule(cellID));
0229 }
0230
0231 double Setup::GetZ(int cellID) const{
0232 int lay=GetLayer(cellID);
0233 if (sumOpt == 0){
0234 return cellD/2 + lay*cellD;
0235 } else {
0236 double tempZ = 0;
0237 for (int l = 0; l < lay; l++){
0238 tempZ = tempZ+GetSegmentDepth(cellID);
0239 }
0240 tempZ = tempZ+GetSegmentDepth(cellID)/2.;
0241 return tempZ;
0242 }
0243 }
0244
0245 int Setup::GetLayersInSegmentFromLayer(int layerNr) const{
0246 auto it = SegmentSum.find(layerNr);
0247 if (it != SegmentSum.end())
0248 return it->second;
0249 return 1;
0250 }
0251
0252 int Setup::GetLayersInSegment(int cellID) const{
0253 int lay=GetLayer(cellID);
0254 auto it = SegmentSum.find(lay);
0255 if (it != SegmentSum.end())
0256 return it->second;
0257 return 1;
0258 }
0259
0260 double Setup::GetSegmentDepth(int cellID) const{
0261 int lay=GetLayer(cellID);
0262 auto it = SegmentSum.find(lay);
0263 if (it != SegmentSum.end())
0264 return it->second*cellD;
0265 return cellD;
0266 }
0267
0268
0269 TString Setup::DecodeCellID(int cellID) const{
0270 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));
0271 return out;
0272 }
0273
0274
0275 int Setup::GetNMaxLayer() const{
0276 return nMaxLayer;
0277 }
0278
0279 int Setup::GetNMaxRow() const{
0280 return nMaxRow;
0281 }
0282
0283 int Setup::GetNMaxColumn() const{
0284 return nMaxColumn;
0285 }
0286
0287 int Setup::GetNMaxModule() const{
0288 return nMaxModule;
0289 }
0290
0291 int Setup::GetNMaxROUnit() const{
0292 return nMaxROUnit;
0293 }
0294
0295 int Setup::GetNMaxKCUs() const{
0296 return (int)(nMaxROUnit/2+1);
0297 }
0298
0299 int Setup::GetMaxCellID() const{
0300 return maxCellID;
0301 }
0302
0303 int Setup::GetMaxChannelInLayerFull() const{
0304 int maxChInLayer = (nMaxColumn+1)*(nMaxRow+1)*(nMaxModule+1);
0305 return maxChInLayer;
0306 }
0307
0308
0309 int Setup::GetChannelInLayer(int cellID) const{
0310 int row = GetRow(cellID);
0311 int column = GetColumn(cellID);
0312 int absChL = row*(nMaxColumn+1)+column;
0313 return absChL;
0314 }
0315
0316 int Setup::GetChannelInLayerFull(int cellID, DetConf::Type type) const{
0317 int row = GetRow(cellID);
0318 int column = GetColumn(cellID);
0319 int mod = GetModule(cellID);
0320 int absChL = -1;
0321
0322 if (type == DetConf::Type::Unset){
0323 type = GetDetectorConfig();
0324 }
0325
0326 if ( type == DetConf::Type::Dual8M){
0327 absChL = mod*((nMaxColumn+1)*(nMaxRow+1))+row*(nMaxColumn+1)+column;
0328 } else if ( type == DetConf::Type::MediumTB){
0329 if (mod%2 == 0){
0330 absChL = mod%2*((nMaxColumn+1) *(nMaxRow+1))+row*(nMaxColumn+1)*2+column+(int)mod/2*16;
0331 } else {
0332 absChL = (mod%2-1)*((nMaxColumn+1) *(nMaxRow+1))+row*(nMaxColumn+1)*2+column+(int)(mod-1)/2*16 + (nMaxColumn+1);
0333 }
0334 } else if ( type == DetConf::Type::Single8M){
0335 absChL = row*(nMaxColumn+1)+column;
0336 }
0337 return absChL;
0338 }
0339
0340 int Setup::GetAbsNMaxROChannel() const{
0341 int max = -1;
0342 std::map<int, int>::const_iterator it;
0343 for(it=ROchannel.begin(); it!=ROchannel.end(); ++it){
0344 if( it->second > max ) max = it->second;
0345 }
0346 return max;
0347 }
0348
0349 float Setup::GetMinX() const{
0350 float min = 1e6;
0351 std::map<int, TString>::const_iterator it;
0352 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0353 int cellID = it->first;
0354 if( GetX(cellID) < min ) min = GetX(cellID);
0355 }
0356 return min-(cellW/2);
0357 }
0358
0359 float Setup::GetMaxX() const{
0360 float max = -1e6;
0361 std::map<int, TString>::const_iterator it;
0362 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0363 int cellID = it->first;
0364 if( GetX(cellID) > max ) max = GetX(cellID);
0365 }
0366 return max+(cellW/2);
0367 }
0368
0369 float Setup::GetMinY() const{
0370 float min = 1e6;
0371 std::map<int, TString>::const_iterator it;
0372 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0373 int cellID = it->first;
0374 if( GetY(cellID) < min ) min = GetY(cellID);
0375 }
0376 return min-(cellH/2);
0377 }
0378
0379 float Setup::GetMaxY() const{
0380 float max = -1e6;
0381 std::map<int, TString>::const_iterator it;
0382 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0383 int cellID = it->first;
0384 if( GetY(cellID) > max ) max = GetY(cellID);
0385 }
0386 return max+(cellH/2);
0387 }
0388
0389 float Setup::GetMinZ() const{
0390 float min = 1e6;
0391 std::map<int, TString>::const_iterator it;
0392 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0393 int cellID = it->first;
0394 if( GetZ(cellID) < min ) min = GetZ(cellID)-(GetSegmentDepth(cellID)/2);
0395 }
0396 return min;
0397 }
0398
0399 float Setup::GetMaxZ() const{
0400 float max = -1e6;
0401 std::map<int, TString>::const_iterator it;
0402 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0403 int cellID = it->first;
0404 if( GetZ(cellID) > max ) max = GetZ(cellID)+(GetSegmentDepth(cellID)/2);
0405 }
0406 return max;
0407 }
0408
0409 bool Setup::IsLayerOn(int layer, int mod) const{
0410 bool isOn = false;
0411 for (int r = 0; r< GetNMaxRow(); r++){
0412 for (int c = 0; c < GetNMaxColumn(); c++){
0413 if (mod == -1){
0414 for (int m = 0; m < GetNMaxModule(); m++){
0415 int cellID = GetCellID(r, c, layer, m);
0416 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0417 if (it != assemblyID.end()){
0418 isOn = true;
0419 break;
0420 }
0421 }
0422 } else {
0423 int cellID = GetCellID(r, c, layer, mod);
0424 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0425 if (it != assemblyID.end()){
0426 isOn = true;
0427 break;
0428 }
0429 }
0430 }
0431 }
0432 return isOn;
0433 }
0434
0435 int Setup::GetNActiveLayers() const{
0436 int nActLayer = 0;
0437 for (Int_t l = 0; l < GetNMaxLayer()+1; l++){
0438 if (IsLayerOn(l,-1)) nActLayer++;
0439 }
0440 return nActLayer;
0441 }
0442
0443 int Setup::GetNActiveCells() const{
0444 int nActCells = assemblyID.size();
0445 return nActCells;
0446 }
0447
0448 float Setup::GetCellWidth() const{
0449 return cellW;
0450 }
0451
0452 float Setup::GetCellHeight() const{
0453 return cellH;
0454 }
0455
0456 float Setup::GetCellDepth() const{
0457 return cellD;
0458 }
0459
0460
0461
0462 DetConf::Type Setup::GetDetectorConfig() const{
0463 DetConf::Type type;
0464 if (GetNMaxModule()+1 == 2){
0465 if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0466 type = DetConf::Type::Dual8M;
0467 else
0468 type = DetConf::Type::Undef;
0469 } else if (GetNMaxModule()+1 == 8){
0470 if(GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0471 type = DetConf::Type::LargeTB;
0472 else
0473 type = DetConf::Type::Undef;
0474 } else if (GetNMaxModule()+1 == 6){
0475 if(GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0476 type = DetConf::Type::MediumTB;
0477 else
0478 type = DetConf::Type::Undef;
0479 } else if (GetNMaxModule()+1 == 1){
0480 if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0481 type = DetConf::Type::Single8M;
0482 else if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 2)
0483 type = DetConf::Type::Single4M;
0484 else if (GetNMaxRow()+1 == 1 && GetNMaxColumn()+1 == 2)
0485 type = DetConf::Type::Single2MH;
0486 else if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 1)
0487 type = DetConf::Type::Single2MV;
0488 else if (GetNMaxRow()+1 == 1 && GetNMaxColumn()+1 == 1)
0489 type = DetConf::Type::SingleTile;
0490 else
0491 type = DetConf::Type::Undef;
0492 } else {
0493 type = DetConf::Type::Undef;
0494 }
0495 return type;
0496 }
0497
0498 bool Setup::ContainedInSetup(int cellID) const{
0499 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0500 if(it!=assemblyID.end()) return true;
0501 else return false;
0502 }
0503