File indexing completed on 2026-11-20 09:33:44
0001 #include <TROOT.h>
0002 #include <TString.h>
0003 #include <TObjString.h>
0004 #include <TSystem.h>
0005 #include <TFile.h>
0006 #include <TTree.h>
0007 #include <iostream>
0008 #include <fstream>
0009 #include <utility>
0010
0011 struct FEBInfo
0012 {
0013 FEBInfo() : FEBNr(0), FEBtype(0), rUnit(0), rHalf(0), modulePosX(0.), modulePosY(0.) {}
0014 int FEBNr;
0015 int FEBtype;
0016 int rUnit;
0017 int rHalf;
0018 int orientation;
0019 float modulePosX;
0020 float modulePosY;
0021 };
0022
0023 struct unitInfo
0024 {
0025 unitInfo() : chRU(0), rowInFEB(0), colInFEB(0) {}
0026 int chRU;
0027 int rowInFEB;
0028 int colInFEB;
0029 };
0030
0031 struct channelInfo
0032 {
0033 channelInfo() : chRU(0), rUnit(0), ruID(0), nrAssembly(0), chAssembly(0), modNr(0), layer(0), rowAssembly(0), colAssembly(0), chID(0), posX(0.), posY(0.), posZ(0.), modposX(0.), modposY(0.) {}
0034 int chRU;
0035 int rUnit;
0036 int ruID;
0037 int nrAssembly;
0038 int chAssembly;
0039 int modNr;
0040 int layer;
0041 int rowAssembly;
0042 int colAssembly;
0043 int segSize;
0044 int chID;
0045 float posX;
0046 float posY;
0047 float posZ;
0048 float modposX;
0049 float modposY;
0050 };
0051
0052 void PrintChannelInfo(channelInfo tempC)
0053 {
0054 cout << "RU: " << tempC.rUnit << "\t"
0055 << "RU channel: " << tempC.chRU << "\t"
0056 << "RU ID: " << tempC.ruID << "\t"
0057 << "module Nr: " << tempC.modNr << "\t"
0058 << "Row in assembly: " << tempC.rowAssembly << "\t"
0059 << "Col in assembly: " << tempC.colAssembly << "\t"
0060 << "Layer Nr: " << tempC.layer << "\t"
0061 << "Channel ID: " << tempC.chID << "\t"
0062 << "Assembly Nr.: " << tempC.nrAssembly << "\t"
0063 << "Ch in assembly: " << tempC.chAssembly << "\t"
0064 << "Segment lenght: " << tempC.segSize << "\t"
0065 << "X: " << tempC.posX << "cm \t"
0066 << "Y: " << tempC.posY << "cm \t"
0067 << "Z: " << tempC.posZ << "cm \t"
0068 << "Mod X: " << tempC.modposX << "cm \t"
0069 << "Mod Y: " << tempC.modposY << "cm \t"
0070 << endl;
0071 return;
0072 }
0073
0074
0075
0076
0077 Int_t findChannelInUnit(std::vector<unitInfo> lUnit, int chA)
0078 {
0079 Int_t currEntry = 0;
0080 Bool_t found = kFALSE;
0081 while (!found && currEntry < (Int_t)lUnit.size())
0082 {
0083 if (lUnit.at(currEntry).chRU == chA)
0084 {
0085 found = kTRUE;
0086 }
0087 else
0088 {
0089 if (currEntry < (Int_t)lUnit.size())
0090 currEntry++;
0091 }
0092 }
0093
0094 if (currEntry == (Int_t)lUnit.size())
0095 return -1;
0096 else
0097 {
0098 return lUnit.at(currEntry).chRU;
0099 }
0100 }
0101
0102
0103
0104
0105
0106 Int_t ReturnRowBoard(Int_t ch, std::vector<unitInfo> fUnit, Int_t inverted)
0107 {
0108 Int_t row = -1;
0109 for (size_t i = 0; i < fUnit.size(); i++){
0110 if (fUnit.at(i).chRU == ch) {
0111 row = fUnit.at(i).rowInFEB;
0112 break;
0113 }
0114 }
0115 if (row == -1)
0116 return row;
0117
0118 if (inverted > 0){
0119 return row;
0120 } else {
0121 return 3 - row;
0122 }
0123 }
0124
0125 Int_t ReturnColumnBoard(Int_t ch, std::vector<unitInfo> fUnit, Int_t inverted)
0126 {
0127 Int_t col = -1;
0128 for (size_t i = 0; i < fUnit.size(); i++) {
0129 if (fUnit.at(i).chRU == ch) {
0130 col = fUnit.at(i).colInFEB;
0131 break;
0132 }
0133 }
0134 if (col == -1)
0135 return col;
0136
0137 if (inverted > 0) {
0138 return col;
0139 } else{
0140 return 7 - col;
0141 }
0142 }
0143
0144 float ReturnPosXInLayer(Int_t col)
0145 {
0146 return 20 / 8 * col + 10 / 8;
0147 }
0148 float ReturnPosYInLayer(Int_t row)
0149 {
0150 return 10 / 4 * row + 5 / 4;
0151 }
0152
0153 float ReturnAbsX(float XInLayer, float ModX)
0154 {
0155 return XInLayer + ModX;
0156 }
0157
0158 float ReturnAbsY(float YInLayer, float ModY)
0159 {
0160 return YInLayer + ModY;
0161 }
0162
0163 void CreateMappingFocal(TString filenameUnitMapping0,
0164 TString filenameUnitMapping1,
0165 TString filenameModulePositions,
0166 TString filenameMappingWrite,
0167 int debug = 0)
0168 {
0169
0170
0171
0172
0173 std::vector<FEBInfo> febs;
0174 std::vector<unitInfo> uChannels[2];
0175 ifstream inUnit0;
0176 inUnit0.open(filenameUnitMapping0, ios_base::in);
0177 if (!inUnit0)
0178 {
0179 std::cout << "ERROR: file " << filenameUnitMapping0.Data() << " not found!" << std::endl;
0180 return;
0181 }
0182
0183 ifstream inUnit1;
0184 inUnit1.open(filenameUnitMapping1, ios_base::in);
0185 if (!inUnit1)
0186 {
0187 std::cout << "ERROR: file " << filenameUnitMapping1.Data() << " not found!" << std::endl;
0188 return;
0189 }
0190
0191 ifstream inModulePositions;
0192 inModulePositions.open(filenameModulePositions, ios_base::in);
0193 if (!inModulePositions)
0194 {
0195 std::cout << "ERROR: file " << filenameModulePositions.Data() << " not found!" << std::endl;
0196 return;
0197 }
0198
0199
0200
0201
0202 for (TString tempLine; tempLine.ReadLine(inUnit0, kTRUE);)
0203 {
0204
0205 if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#"))
0206 {
0207 continue;
0208 }
0209 if (debug > 1)
0210 std::cout << tempLine.Data() << std::endl;
0211
0212
0213 TObjArray *tempArr = tempLine.Tokenize("\t");
0214 if (tempArr->GetEntries() < 1)
0215 {
0216 if (debug > 1)
0217 std::cout << "nothing to be done" << std::endl;
0218 delete tempArr;
0219 continue;
0220 }
0221 else if (tempArr->GetEntries() == 1)
0222 {
0223
0224 tempArr = tempLine.Tokenize(" ");
0225 if (tempArr->GetEntries() < 1)
0226 {
0227 if (debug > 1)
0228 std::cout << "nothing to be done" << std::endl;
0229 delete tempArr;
0230 continue;
0231 }
0232 else if (tempArr->GetEntries() == 1)
0233 {
0234 if (debug > 1)
0235 std::cout << ((TString)((TObjString *)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0236 delete tempArr;
0237 continue;
0238 }
0239 }
0240
0241
0242 unitInfo tempUCh;
0243 tempUCh.chRU = ((TString)((TObjString *)tempArr->At(0))->GetString()).Atoi();
0244 tempUCh.rowInFEB = ((TString)((TObjString *)tempArr->At(1))->GetString()).Atoi();
0245 tempUCh.colInFEB = ((TString)((TObjString *)tempArr->At(2))->GetString()).Atoi();
0246
0247 if (debug > 0)
0248 std::cout << "channel readout unit 0: " << tempUCh.chRU << "\t row in FEB: " << tempUCh.rowInFEB << "\t column in FEB: " << tempUCh.colInFEB << std::endl;
0249 uChannels[0].push_back(tempUCh);
0250 }
0251
0252
0253
0254 for (TString tempLine; tempLine.ReadLine(inUnit1, kTRUE);)
0255 {
0256
0257 if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#"))
0258 {
0259 continue;
0260 }
0261 if (debug > 1)
0262 std::cout << tempLine.Data() << std::endl;
0263
0264
0265 TObjArray *tempArr = tempLine.Tokenize("\t");
0266 if (tempArr->GetEntries() < 1)
0267 {
0268 if (debug > 1)
0269 std::cout << "nothing to be done" << std::endl;
0270 delete tempArr;
0271 continue;
0272 }
0273 else if (tempArr->GetEntries() == 1)
0274 {
0275
0276 tempArr = tempLine.Tokenize(" ");
0277 if (tempArr->GetEntries() < 1)
0278 {
0279 if (debug > 1)
0280 std::cout << "nothing to be done" << std::endl;
0281 delete tempArr;
0282 continue;
0283 }
0284 else if (tempArr->GetEntries() == 1)
0285 {
0286 if (debug > 1)
0287 std::cout << ((TString)((TObjString *)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0288 delete tempArr;
0289 continue;
0290 }
0291 }
0292
0293
0294 unitInfo tempUCh;
0295 tempUCh.chRU = ((TString)((TObjString *)tempArr->At(0))->GetString()).Atoi();
0296 tempUCh.rowInFEB = ((TString)((TObjString *)tempArr->At(1))->GetString()).Atoi();
0297 ;
0298 tempUCh.colInFEB = ((TString)((TObjString *)tempArr->At(2))->GetString()).Atoi();
0299
0300 if (debug > 0)
0301 std::cout << "channel readout unit 1: " << tempUCh.chRU << "\t row in FEB: " << tempUCh.rowInFEB << "\t column in FEB: " << tempUCh.colInFEB << std::endl;
0302 uChannels[1].push_back(tempUCh);
0303 }
0304
0305
0306
0307
0308 for (TString tempLine; tempLine.ReadLine(inModulePositions, kTRUE);)
0309 {
0310 if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#"))
0311 {
0312 continue;
0313 }
0314 TObjArray *tempArr = tempLine.Tokenize("\t");
0315 if (tempArr->GetEntries() < 1)
0316 {
0317 if (debug > 1)
0318 std::cout << "nothing to be done" << std::endl;
0319 delete tempArr;
0320 continue;
0321 }
0322 else if (tempArr->GetEntries() == 1)
0323 {
0324
0325 tempArr = tempLine.Tokenize(" ");
0326 if (tempArr->GetEntries() < 1)
0327 {
0328 if (debug > 1)
0329 std::cout << "nothing to be done" << std::endl;
0330 delete tempArr;
0331 continue;
0332 }
0333 else if (tempArr->GetEntries() == 1)
0334 {
0335 if (debug > 1)
0336 std::cout << ((TString)((TObjString *)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0337 delete tempArr;
0338 continue;
0339 }
0340 }
0341
0342
0343 FEBInfo tempFebI;
0344 tempFebI.FEBNr = ((TString)((TObjString *)tempArr->At(0))->GetString()).Atoi();
0345 tempFebI.FEBtype = ((TString)((TObjString *)tempArr->At(1))->GetString()).Atoi();
0346 tempFebI.rUnit = ((TString)((TObjString *)tempArr->At(2))->GetString()).Atoi();
0347 tempFebI.rHalf = ((TString)((TObjString *)tempArr->At(3))->GetString()).Atoi();
0348 tempFebI.modulePosX = ((TString)((TObjString *)tempArr->At(4))->GetString()).Atof();
0349 tempFebI.modulePosY = ((TString)((TObjString *)tempArr->At(5))->GetString()).Atof();
0350 tempFebI.orientation = ((TString)((TObjString *)tempArr->At(6))->GetString()).Atoi();
0351 febs.push_back(tempFebI);
0352 }
0353
0354 std::vector<channelInfo> channels;
0355 fstream fileMappingClassic(filenameMappingWrite.Data(), ios::out);
0356
0357 fileMappingClassic << "sumOpt\t" << 0 << "\n";
0358 fileMappingClassic << "detector\t" << "FoCal-H" << "\n";
0359 fileMappingClassic << "#HGCROC board HGCROC Ch layer assembly board channel row column modNr modX modY segSize\n";
0360 TFile *outputRootFile = new TFile("mappingTree.root", "RECREATE");
0361 TTree *mapping_tree = new TTree("mapping_tree", "mapping_tree");
0362 mapping_tree->SetDirectory(outputRootFile);
0363 channelInfo tempChannel;
0364
0365 mapping_tree->Branch("channel", &tempChannel, "ch_ru/I:ru/I:ruID/I:nr_ass/I:ch_ass/I:modNr/I:layer/I:row_ass/I:col_ass/I:chID/I:posX/F:posY/F:posZ/F:modposX/F:modposY/F:segSize/I");
0366
0367 for (int f = 0; f < (int)febs.size(); f++){
0368 std::cout << "===============================================" << std::endl;
0369 std::cout << "FEB: " << "\t" << febs.at(f).FEBNr << "\t" << febs.at(f).FEBtype << "\t" <<
0370 febs.at(f).orientation<<std::endl;
0371
0372
0373 for (int chFEB = 0; chFEB < 36; chFEB++){
0374
0375 tempChannel.chRU = chFEB + febs.at(f).rHalf * 36;
0376 tempChannel.rUnit = febs.at(f).rUnit;
0377 tempChannel.ruID = Int_t(tempChannel.rUnit << 9) + tempChannel.chRU;
0378 tempChannel.modNr = febs.at(f).FEBNr;
0379 tempChannel.layer = 0;
0380 tempChannel.nrAssembly = 0;
0381 tempChannel.chAssembly = 0;
0382 tempChannel.rowAssembly = ReturnRowBoard(chFEB, uChannels[febs.at(f).FEBtype], febs.at(f).orientation);
0383 tempChannel.colAssembly = ReturnColumnBoard(chFEB, uChannels[febs.at(f).FEBtype], febs.at(f).orientation);
0384
0385
0386 tempChannel.chID = Int_t(tempChannel.modNr << 5) + Int_t(tempChannel.rowAssembly << 3) + Int_t(tempChannel.colAssembly) ;
0387 tempChannel.posX = ReturnPosXInLayer(tempChannel.colAssembly);
0388 tempChannel.posY = ReturnPosYInLayer(tempChannel.rowAssembly);
0389 tempChannel.posZ = 0;
0390 tempChannel.modposX = febs.at(f).modulePosX;
0391 tempChannel.modposY = febs.at(f).modulePosY;
0392 tempChannel.segSize = 1;
0393 channels.push_back(tempChannel);
0394
0395 if (tempChannel.rowAssembly != -1 && tempChannel.colAssembly != -1){
0396
0397 fileMappingClassic
0398 << tempChannel.rUnit << "\t"
0399 << tempChannel.chRU << "\t"
0400 << tempChannel.layer << "\t"
0401 << 0 << "\t"
0402 << chFEB << "\t"
0403 << tempChannel.rowAssembly << "\t"
0404 << tempChannel.colAssembly << "\t"
0405 << tempChannel.modNr << "\t"
0406 << tempChannel.modposX << "\t"
0407 << tempChannel.modposY << "\t"
0408 << tempChannel.segSize << "\n";
0409 PrintChannelInfo(tempChannel);
0410 mapping_tree->Fill();
0411 }
0412 }
0413 }
0414
0415 fileMappingClassic.close();
0416 mapping_tree->Print();
0417
0418 outputRootFile->Write();
0419 outputRootFile->Close();
0420 return;
0421 }