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