File indexing completed on 2025-12-16 09:28:22
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;
0034 TString Anassembly;
0035 int Akey;
0036 for( TString tempLine; tempLine.ReadLine(input, kTRUE); ) {
0037
0038 if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0039 continue;
0040 }
0041 TObjArray *tempArr = tempLine.Tokenize("\t");
0042 if(tempArr->GetEntries()<1){
0043 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0044 delete tempArr;
0045 continue;
0046 } else if (tempArr->GetEntries() == 1 ){
0047
0048 tempArr = tempLine.Tokenize(" ");
0049 if(tempArr->GetEntries()<1){
0050 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0051 delete tempArr;
0052 continue;
0053 } else if (tempArr->GetEntries() == 1 ) {
0054 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " no correct format detected" << std::endl;
0055 delete tempArr;
0056 continue;
0057 }
0058 }
0059 if (tempArr->GetEntries()<10){
0060 if (debug > 1) std::cout << "line not conform with mapping format, skipping" << std::endl;
0061 delete tempArr;
0062 continue;
0063 }
0064
0065 AROunit = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0066 AROchannel = ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();
0067 Alayer = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0068 Anassembly = ((TString)((TObjString*)tempArr->At(3))->GetString());
0069 AROlayer = ((TString)((TObjString*)tempArr->At(4))->GetString()).Atoi();
0070 Arow = ((TString)((TObjString*)tempArr->At(5))->GetString()).Atoi();
0071 Acolumn = ((TString)((TObjString*)tempArr->At(6))->GetString()).Atoi();
0072 Amod = ((TString)((TObjString*)tempArr->At(7))->GetString()).Atoi();
0073 float AmodX = ((TString)((TObjString*)tempArr->At(8))->GetString()).Atof();
0074 float AmodY = ((TString)((TObjString*)tempArr->At(9))->GetString()).Atof();
0075
0076
0077
0078 ModPos[Amod]=std::make_pair(AmodX,AmodY);
0079
0080
0081 Akey=(Amod<<9)+(Arow<<8)+(Acolumn<<6)+(Alayer);
0082 assemblyID[Akey] = Anassembly;
0083 ROunit [Akey] = AROunit;
0084 ROchannel [Akey] = AROchannel;
0085 Board [Akey] = AROlayer;
0086 CellIDfromRO[std::make_pair(AROunit,AROchannel)]=Akey;
0087 if (nMaxLayer < Alayer) nMaxLayer = Alayer;
0088 if (nMaxRow < Arow) nMaxRow = Arow;
0089 if (nMaxColumn < Acolumn) nMaxColumn = Acolumn;
0090 if (nMaxModule < Amod) nMaxModule = Amod;
0091 if (nMaxROUnit < AROunit) nMaxROUnit = AROunit;
0092 if (maxCellID < Akey) maxCellID = Akey;
0093 if (debug > 10)std::cout <<AROunit<< "\t" << AROchannel << "\t"<< Alayer << "\t"<< Anassembly << "\t"<< AROlayer << "\t"<< Arow << "\t"<< Acolumn << "\t"<< Amod << std::endl;
0094 if (debug > 1)std::cout << "registered cell: " << DecodeCellID(Akey).Data() << std::endl;
0095 }
0096 input.close();
0097 isInit=true;
0098 return isInit;
0099 }
0100
0101 bool Setup::Initialize(RootSetupWrapper& rsw){
0102 std::cout<<rsw.isInit<<"\t"<<rsw.assemblyID.size()<<std::endl;
0103 isInit =rsw.isInit;
0104 assemblyID =rsw.assemblyID;
0105 ROunit =rsw.ROunit;
0106 ROchannel =rsw.ROchannel;
0107 Board =rsw.Board;
0108 CellIDfromRO =rsw.CellIDfromRO;
0109 nMaxLayer =rsw.nMaxLayer;
0110 nMaxRow =rsw.nMaxRow;
0111 nMaxColumn =rsw.nMaxColumn;
0112 nMaxModule =rsw.nMaxModule;
0113 nMaxROUnit =rsw.nMaxROUnit;
0114 maxCellID =rsw.maxCellID;
0115 ModPos =rsw.ModPos;
0116 return isInit;
0117 }
0118
0119 bool Setup::IsInit() const{
0120 return isInit;
0121 }
0122
0123 TString Setup::GetAssemblyID(int cellID) const{
0124 std::map<int, TString>::const_iterator it=assemblyID.find(cellID);
0125 if(it!=assemblyID.end()) return it->second;
0126 else return "";
0127 }
0128
0129 TString Setup::GetAssemblyID(int row, int col, int lay, int mod=0) const{
0130 return GetAssemblyID((mod<<9)+(row<<8)+(col<<6)+lay);
0131 }
0132
0133 int Setup::GetCellID(int roboard, int roch) const{
0134 std::map<std::pair<int,int>,int>::const_iterator it=CellIDfromRO.find(std::make_pair(roboard,roch));
0135 if(it!=CellIDfromRO.cend()) return it->second;
0136 else return -1;
0137 }
0138
0139 int Setup::GetCellID(int row, int col, int lay, int mod=0)const {
0140 return (mod<<9)+(row<<8)+(col<<6)+lay;
0141 }
0142
0143 int Setup::GetColumn(int cellID) const{
0144 return (cellID&(3<<6))>>6;
0145 }
0146
0147 int Setup::GetLayer(int cellID) const{
0148 return (cellID&((1<<6)-1));
0149 }
0150
0151 int Setup::GetModule(int cellID) const{
0152 return cellID>>9;
0153 }
0154
0155 int Setup::GetROchannel(int cellID) const{
0156 std::map<int, int>::const_iterator it=ROchannel.find(cellID);
0157 if(it!=ROchannel.end()) return it->second;
0158 else return -999;
0159 }
0160
0161 int Setup::GetROchannel(int row, int col, int lay, int mod=0) const{
0162 return GetROchannel((mod<<9)+(row<<8)+(col<<6)+lay);
0163 }
0164
0165 int Setup::GetROunit(int cellID) const{
0166 std::map<int,int>::const_iterator it=ROunit.find(cellID);
0167 if(it!=ROunit.end()) return it->second;
0168 else return -999;
0169 }
0170
0171 int Setup::GetROunit(int row, int col, int lay, int mod=0) const{
0172 return GetROunit((mod<<9)+(row<<8)+(col<<6)+lay);
0173 }
0174
0175 int Setup::GetRow(int cellID) const{
0176 return (cellID&(1<<8))>>8;
0177 }
0178
0179 double Setup::GetModuleX(int mod)const {
0180 auto it = ModPos.find(mod);
0181 if (it != ModPos.end()) {
0182
0183 return static_cast<double>(it->second.first);
0184 }
0185 else {
0186 std::cerr << "Warning: Module " << mod << " not found in ModPos\n";
0187 return -999.0;
0188 }
0189 }
0190
0191 double Setup::GetModuleY(int mod)const {
0192 auto it = ModPos.find(mod);
0193 if (it != ModPos.end()) {
0194 return static_cast<double>(it->second.second);
0195 }
0196 else {
0197 std::cerr << "Warning: Module " << mod << " not found in ModPos\n";
0198 return -999.0;
0199 }
0200 }
0201
0202 int Setup::GetTotalNbChannels(void) const {
0203 return (int) ROunit.size();
0204 }
0205
0206 double Setup::GetX(int cellID) const{
0207 int col=GetColumn(cellID);
0208 return -7.5+col*cellW + GetModuleX(GetModule(cellID));
0209 }
0210
0211 double Setup::GetY(int cellID) const{
0212 int row=GetRow(cellID);
0213 return -2.5+row*cellH + GetModuleY(GetModule(cellID));
0214 }
0215
0216 double Setup::GetZ(int cellID) const{
0217 int lay=GetLayer(cellID);
0218 return cellD/2 + lay*cellD;
0219 }
0220
0221 TString Setup::DecodeCellID(int cellID) const{
0222 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));
0223 return out;
0224 }
0225
0226
0227 int Setup::GetNMaxLayer() const{
0228 return nMaxLayer;
0229 }
0230
0231 int Setup::GetNMaxRow() const{
0232 return nMaxRow;
0233 }
0234
0235 int Setup::GetNMaxColumn() const{
0236 return nMaxColumn;
0237 }
0238
0239 int Setup::GetNMaxModule() const{
0240 return nMaxModule;
0241 }
0242
0243 int Setup::GetNMaxROUnit() const{
0244 return nMaxROUnit;
0245 }
0246
0247 int Setup::GetNMaxKCUs() const{
0248 return (int)(nMaxROUnit/2+1);
0249 }
0250
0251 int Setup::GetMaxCellID() const{
0252 return maxCellID;
0253 }
0254
0255 int Setup::GetChannelInLayer(int cellID) const{
0256 int row = GetRow(cellID);
0257 int column = GetColumn(cellID);
0258 int absChL = row*(nMaxColumn+1)+column;
0259 return absChL;
0260 }
0261
0262 float Setup::GetMinX() const{
0263 float min = 1e6;
0264 std::map<int, TString>::const_iterator it;
0265 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0266 int cellID = it->first;
0267 if( GetX(cellID) < min ) min = GetX(cellID);
0268 }
0269 return min-(cellW/2);
0270 }
0271
0272 float Setup::GetMaxX() const{
0273 float max = -1e6;
0274 std::map<int, TString>::const_iterator it;
0275 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0276 int cellID = it->first;
0277 if( GetX(cellID) > max ) max = GetX(cellID);
0278 }
0279 return max+(cellW/2);
0280 }
0281
0282 float Setup::GetMinY() const{
0283 float min = 1e6;
0284 std::map<int, TString>::const_iterator it;
0285 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0286 int cellID = it->first;
0287 if( GetY(cellID) < min ) min = GetY(cellID);
0288 }
0289 return min-(cellH/2);
0290 }
0291
0292 float Setup::GetMaxY() const{
0293 float max = -1e6;
0294 std::map<int, TString>::const_iterator it;
0295 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0296 int cellID = it->first;
0297 if( GetY(cellID) > max ) max = GetY(cellID);
0298 }
0299 return max+(cellH/2);
0300 }
0301
0302 float Setup::GetMinZ() const{
0303 float min = 1e6;
0304 std::map<int, TString>::const_iterator it;
0305 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0306 int cellID = it->first;
0307 if( GetZ(cellID) < min ) min = GetZ(cellID);
0308 }
0309 return min-(cellD/2);
0310 }
0311
0312 float Setup::GetMaxZ() const{
0313 float max = -1e6;
0314 std::map<int, TString>::const_iterator it;
0315 for(it=assemblyID.begin(); it!=assemblyID.end(); ++it){
0316 int cellID = it->first;
0317 if( GetZ(cellID) > max ) max = GetZ(cellID);
0318 }
0319 return max+(cellD/2);
0320 }
0321
0322 float Setup::GetCellWidth() const{
0323 return cellW;
0324 }
0325
0326 float Setup::GetCellHeight() const{
0327 return cellH;
0328 }
0329
0330 float Setup::GetCellDepth() const{
0331 return cellD;
0332 }