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