File indexing completed on 2025-02-22 09:39:21
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
0010 struct layerInfo{
0011 layerInfo(): layerNrAbs(0), layerLabel(""), rUnit(0), layerUnit(0){}
0012 int layerNrAbs;
0013 TString layerLabel;
0014 int rUnit;
0015 int layerUnit;
0016 } ;
0017
0018 struct unitInfo{
0019 unitInfo(): chRU(0), layerUnit(0), chAssembly(0){}
0020 int chRU;
0021 int layerUnit;
0022 int chAssembly;
0023 } ;
0024
0025 struct channelInfo{
0026 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.){}
0027 int chRU;
0028 int rUnit;
0029 int ruID;
0030 int nrAssembly;
0031 int chAssembly;
0032 int modNr;
0033 int layer;
0034 int rowAssembly;
0035 int colAssembly;
0036 int chID;
0037 float posX;
0038 float posY;
0039 float posZ;
0040 };
0041
0042
0043 void PrintChannelInfo(channelInfo tempC){
0044 cout << "RU: " <<tempC.rUnit << "\t"
0045 << "RU channel: "<< tempC.chRU << "\t"
0046 << "RU ID: "<< tempC.ruID << "\t"
0047 << "module Nr: "<< tempC.layer << "\t"
0048 << "Row in assembly: "<< tempC.rowAssembly << "\t"
0049 << "Col in assembly: "<< tempC.colAssembly << "\t"
0050 << "Layer Nr: "<< tempC.layer << "\t"
0051 << "Channel ID"<< tempC.chID << "\t"
0052 << "Assembly Nr.: "<< tempC.nrAssembly << "\t"
0053 << "Ch in assembly: "<< tempC.chAssembly << "\t"
0054 << "X: " << tempC.posX << "cm \t"
0055 << "Y: " << tempC.posY << "cm \t"
0056 << "Z: " << tempC.posZ << "cm \t"
0057 << endl;
0058 return;
0059 }
0060
0061
0062
0063
0064 Int_t findChannelInUnit(std::vector<unitInfo> lUnit, int layer, int chA ){
0065 Int_t currEntry = 0;
0066
0067
0068
0069
0070
0071 Bool_t found = kFALSE;
0072 while (!found && currEntry < (Int_t)lUnit.size()){
0073 if (lUnit.at(currEntry).layerUnit == layer && lUnit.at(currEntry).chAssembly == chA){
0074
0075 found = kTRUE;
0076 } else {
0077
0078 if (currEntry < (Int_t)lUnit.size()) currEntry++;
0079 }
0080 }
0081
0082 if (currEntry == (Int_t)lUnit.size()) return -1;
0083 else{
0084
0085 return lUnit.at(currEntry).chRU;
0086 }
0087 }
0088
0089
0090
0091
0092
0093 Int_t ReturnRowBoard(Int_t ch){
0094 if (ch < 5) return 1;
0095 else return 0;
0096 }
0097 Int_t ReturnColumnBoard(Int_t ch){
0098 if (ch == 1 || ch == 8) return 0;
0099 if (ch == 2 || ch == 7) return 1;
0100 if (ch == 3 || ch == 6) return 2;
0101 if (ch == 4 || ch == 5) return 3;
0102 return -1;
0103 }
0104 float ReturnPosXInLayer(Int_t ch){
0105 switch(ch){
0106 case 1:
0107 case 8:
0108 return -7.5;
0109 break;
0110 case 2:
0111 case 7:
0112 return -2.5;
0113 break;
0114 case 3:
0115 case 6:
0116 return 2.5;
0117 break;
0118 case 4:
0119 case 5:
0120 return 7.5;
0121 break;
0122 default:
0123 return -10000;
0124 break;
0125 }
0126 }
0127 float ReturnPosYInLayer(Int_t ch){
0128 switch(ch){
0129 case 1:
0130 case 2:
0131 case 3:
0132 case 4:
0133 return 2.5;
0134 break;
0135 case 5:
0136 case 6:
0137 case 7:
0138 case 8:
0139 return -2.5;
0140 break;
0141 default:
0142 return -10000;
0143 break;
0144 }
0145 }
0146 float ReturnPosZAbs(Int_t layer){
0147 return (layer+1)*2.0;
0148 }
0149
0150
0151
0152
0153 void CreateMapping( TString filenameUnitMapping,
0154 TString filenameLayerMapping,
0155 TString filenameMappingWrite,
0156 int debug = 0
0157 ){
0158
0159
0160
0161
0162 std::vector<layerInfo> layers;
0163 std::vector<unitInfo> uChannels;
0164 ifstream inUnit;
0165 inUnit.open(filenameUnitMapping,ios_base::in);
0166 if (!inUnit) {
0167 std::cout << "ERROR: file " << filenameUnitMapping.Data() << " not found!" << std::endl;
0168 return;
0169 }
0170
0171 ifstream inLayerMap;
0172 inLayerMap.open(filenameLayerMapping,ios_base::in);
0173 if (!inLayerMap) {
0174 std::cout << "ERROR: file " << filenameLayerMapping.Data() << " not found!" << std::endl;
0175 return;
0176 }
0177
0178
0179
0180 for( TString tempLine; tempLine.ReadLine(inUnit, kTRUE); ) {
0181
0182 if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0183 continue;
0184 }
0185 if (debug > 1) std::cout << tempLine.Data() << std::endl;
0186
0187
0188 TObjArray *tempArr = tempLine.Tokenize("\t");
0189 if(tempArr->GetEntries()<1){
0190 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0191 delete tempArr;
0192 continue;
0193 } else if (tempArr->GetEntries() == 1 ){
0194
0195 tempArr = tempLine.Tokenize(" ");
0196 if(tempArr->GetEntries()<1){
0197 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0198 delete tempArr;
0199 continue;
0200 } else if (tempArr->GetEntries() == 1 ) {
0201 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0202 delete tempArr;
0203 continue;
0204 }
0205 }
0206
0207
0208 unitInfo tempUCh;
0209 tempUCh.chRU = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0210 tempUCh.layerUnit = ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();;
0211 tempUCh.chAssembly = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0212
0213 if (debug > 0) std::cout << "channel readout unit " << tempUCh.chRU << "\t layer in unit: " << tempUCh.layerUnit << "\t channel within assembly: " << tempUCh.chAssembly << std::endl;
0214 uChannels.push_back(tempUCh);
0215 }
0216
0217 for( TString tempLine; tempLine.ReadLine(inLayerMap, kTRUE); ) {
0218
0219 if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0220 continue;
0221 }
0222 if (debug > 1) std::cout << tempLine.Data() << std::endl;
0223
0224
0225 TObjArray *tempArr = tempLine.Tokenize("\t");
0226 if(tempArr->GetEntries()<1){
0227 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0228 delete tempArr;
0229 continue;
0230 } else if (tempArr->GetEntries() == 1 ){
0231
0232 tempArr = tempLine.Tokenize(" ");
0233 if(tempArr->GetEntries()<1){
0234 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0235 delete tempArr;
0236 continue;
0237 } else if (tempArr->GetEntries() == 1 ) {
0238 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0239 delete tempArr;
0240 continue;
0241 }
0242 }
0243
0244
0245 layerInfo tempLayer;
0246 tempLayer.layerNrAbs = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0247 tempLayer.layerLabel = (TString)((TObjString*)tempArr->At(1))->GetString();
0248 tempLayer.rUnit = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0249 tempLayer.layerUnit = ((TString)((TObjString*)tempArr->At(3))->GetString()).Atoi();
0250
0251 if (debug > 0) std::cout << "layer " << tempLayer.layerNrAbs << "\t assembly name: " << tempLayer.layerLabel << "\t CAEN unit Nr.: " << tempLayer.rUnit << "\t layer in unit: " << tempLayer.layerUnit << std::endl;
0252 layers.push_back(tempLayer);
0253 }
0254
0255 std::vector<channelInfo> channels;
0256 fstream fileMappingClassic(filenameMappingWrite.Data(), ios::out);
0257
0258 fileMappingClassic << "#CAEN board CAEN Ch layer assembly board channel row column modNr\n" ;
0259
0260 TFile* outputRootFile = new TFile("mappingTree.root","RECREATE");
0261 TTree* mapping_tree = new TTree("mapping_tree", "mapping_tree");
0262 mapping_tree->SetDirectory(outputRootFile);
0263 channelInfo tempChannel;
0264
0265 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");
0266
0267 for (int l = 0; l < (int)layers.size();l++){
0268 for (int chA = 1; chA < 9; chA++){
0269 Int_t channel = findChannelInUnit(uChannels, layers.at(l).layerUnit, chA);
0270
0271 tempChannel.chRU = channel;
0272 tempChannel.rUnit = layers.at(l).rUnit;
0273 tempChannel.ruID = Int_t(tempChannel.rUnit<<8)+tempChannel.chRU;
0274 tempChannel.modNr = 0;
0275 tempChannel.layer = layers.at(l).layerNrAbs;
0276 tempChannel.nrAssembly = (int)(((TString)layers.at(l).layerLabel.ReplaceAll("C","")).Atoi());
0277 tempChannel.chAssembly = chA;
0278 tempChannel.rowAssembly = ReturnRowBoard(chA);
0279 tempChannel.colAssembly = ReturnColumnBoard(chA);
0280
0281 tempChannel.chID = Int_t(tempChannel.modNr<<9)+Int_t(tempChannel.rowAssembly<<8)+Int_t(tempChannel.colAssembly<<6)+tempChannel.layer;
0282 tempChannel.posX = ReturnPosXInLayer(chA);
0283 tempChannel.posY = ReturnPosYInLayer(chA);
0284 tempChannel.posZ = ReturnPosZAbs(layers.at(l).layerNrAbs);
0285 channels.push_back(tempChannel);
0286
0287 fileMappingClassic << layers.at(l).rUnit << "\t" << channel << "\t" << layers.at(l).layerNrAbs << "\t" << layers.at(l).layerLabel << "\t" << chA << "\t" << ReturnRowBoard(chA) << "\t" << ReturnColumnBoard(chA) << "\t" << tempChannel.modNr <<"\n";
0288 PrintChannelInfo(tempChannel);
0289
0290
0291 mapping_tree->Fill();
0292 }
0293 }
0294
0295
0296 fileMappingClassic.close();
0297 mapping_tree->Print();
0298
0299 outputRootFile->Write();
0300 outputRootFile->Close();
0301 return;
0302 }