File indexing completed on 2026-08-28 08:28:40
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 } else if ( type == DetConf::Type::MediumTB){
0334 if (mod%2 == 0){
0335 absChL = mod%2*((nMaxColumn+1) *(nMaxRow+1))+row*(nMaxColumn+1)*2+column+(int)mod/2*16;
0336 } else {
0337 absChL = (mod%2-1)*((nMaxColumn+1) *(nMaxRow+1))+row*(nMaxColumn+1)*2+column+(int)(mod-1)/2*16 + (nMaxColumn+1);
0338 }
0339 } else if ( type == DetConf::Type::Single8M){
0340 absChL = row*(nMaxColumn+1)+column;
0341 } else if ( type == DetConf::Type::FocalH){
0342 absChL = mod*((nMaxColumn+1)*(nMaxRow+1))+row*(nMaxColumn+1)+column;
0343 }
0344 return absChL;
0345 }
0346
0347 int Setup::GetAbsNMaxROChannel() const{
0348 int max = -1;
0349 std::map<int, int>::const_iterator it;
0350 for(it=ROchannel.begin(); it!=ROchannel.end(); ++it){
0351 if( it->second > max ) max = it->second;
0352 }
0353 return max;
0354 }
0355
0356 float Setup::GetMinX() const{
0357 float min = 1e6;
0358 std::map<int, TString>::const_iterator it;
0359 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0360 int cellID = it->first;
0361 if( GetX(cellID) < min ) min = GetX(cellID);
0362 }
0363 return min-(cellW/2);
0364 }
0365
0366 float Setup::GetMaxX() const{
0367 float max = -1e6;
0368 std::map<int, TString>::const_iterator it;
0369 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0370 int cellID = it->first;
0371 if( GetX(cellID) > max ) max = GetX(cellID);
0372 }
0373 return max+(cellW/2);
0374 }
0375
0376 float Setup::GetMinY() const{
0377 float min = 1e6;
0378 std::map<int, TString>::const_iterator it;
0379 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0380 int cellID = it->first;
0381 if( GetY(cellID) < min ) min = GetY(cellID);
0382 }
0383 return min-(cellH/2);
0384 }
0385
0386 float Setup::GetMaxY() const{
0387 float max = -1e6;
0388 std::map<int, TString>::const_iterator it;
0389 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0390 int cellID = it->first;
0391 if( GetY(cellID) > max ) max = GetY(cellID);
0392 }
0393 return max+(cellH/2);
0394 }
0395
0396 float Setup::GetMinZ() const{
0397 float min = 1e6;
0398 std::map<int, TString>::const_iterator it;
0399 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0400 int cellID = it->first;
0401 if( GetZ(cellID) < min ) min = GetZ(cellID)-(GetSegmentDepth(cellID)/2);
0402 }
0403 return min;
0404 }
0405
0406 float Setup::GetMaxZ() const{
0407 float max = -1e6;
0408 std::map<int, TString>::const_iterator it;
0409 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0410 int cellID = it->first;
0411 if( GetZ(cellID) > max ) max = GetZ(cellID)+(GetSegmentDepth(cellID)/2);
0412 }
0413 return max;
0414 }
0415
0416 bool Setup::IsLayerOn(int layer, int mod) const{
0417 bool isOn = false;
0418 for (int r = 0; r< GetNMaxRow(); r++){
0419 for (int c = 0; c < GetNMaxColumn(); c++){
0420 if (mod == -1){
0421 for (int m = 0; m < GetNMaxModule(); m++){
0422 int cellID = GetCellID(r, c, layer, m);
0423 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0424 if (it != assemblyID.end()){
0425 isOn = true;
0426 break;
0427 }
0428 }
0429 } else {
0430 int cellID = GetCellID(r, c, layer, mod);
0431 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0432 if (it != assemblyID.end()){
0433 isOn = true;
0434 break;
0435 }
0436 }
0437 }
0438 }
0439 return isOn;
0440 }
0441
0442 bool Setup::IsAsicOn(int asic) const{
0443 bool isOn = false;
0444 for (int ch = 0; ch< 76; ch++){
0445 int cellID = GetCellID(asic, ch);
0446 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0447 if (it != assemblyID.end()){
0448 isOn = true;
0449 break;
0450 }
0451 }
0452 return isOn;
0453 }
0454
0455
0456 int Setup::GetNActiveLayers() const{
0457 int nActLayer = 0;
0458 for (Int_t l = 0; l < GetNMaxLayer()+1; l++){
0459 if (IsLayerOn(l,-1)) nActLayer++;
0460 }
0461 return nActLayer;
0462 }
0463
0464 int Setup::GetNActiveCells() const{
0465 int nActCells = assemblyID.size();
0466 return nActCells;
0467 }
0468
0469 float Setup::GetCellWidth() const{
0470 return cellW;
0471 }
0472
0473 float Setup::GetCellHeight() const{
0474 return cellH;
0475 }
0476
0477 float Setup::GetCellDepth() const{
0478 return cellD;
0479 }
0480
0481
0482
0483 DetConf::Type Setup::GetDetectorConfig() const{
0484 DetConf::Type type;
0485 if (GetNMaxModule()+1 == 2){
0486 if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0487 type = DetConf::Type::Dual8M;
0488 else
0489 type = DetConf::Type::Undef;
0490 } else if (GetNMaxModule()+1 == 8){
0491 if(GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0492 type = DetConf::Type::LargeTB;
0493 else
0494 type = DetConf::Type::Undef;
0495 } else if (GetNMaxModule()+1 == 6){
0496 if(GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0497 type = DetConf::Type::MediumTB;
0498 else
0499 type = DetConf::Type::Undef;
0500 } else if (GetNMaxModule()+1 == 1){
0501 if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 4)
0502 type = DetConf::Type::Single8M;
0503 else if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 2)
0504 type = DetConf::Type::Single4M;
0505 else if (GetNMaxRow()+1 == 1 && GetNMaxColumn()+1 == 2)
0506 type = DetConf::Type::Single2MH;
0507 else if (GetNMaxRow()+1 == 2 && GetNMaxColumn()+1 == 1)
0508 type = DetConf::Type::Single2MV;
0509 else if (GetNMaxRow()+1 == 1 && GetNMaxColumn()+1 == 1)
0510 type = DetConf::Type::SingleTile;
0511 else
0512 type = DetConf::Type::Undef;
0513 } else {
0514 type = DetConf::Type::Undef;
0515 }
0516 return type;
0517 }
0518
0519 bool Setup::ContainedInSetup(int cellID) const{
0520 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0521 if(it!=assemblyID.end()) return true;
0522 else return false;
0523 }
0524