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