Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-28 07:58:04

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 layerInfo{
0012     layerInfo(): layerNrAbs(0), layerLabel(""), rUnit(0), layerUnit(0), moduleNr(0){}
0013     int layerNrAbs;       // absolute layer number
0014     TString layerLabel;   // assembly name
0015     int rUnit;            // readout unit number
0016     int layerUnit;        // layer within readout unit
0017     int moduleNr;
0018 } ;
0019 
0020 struct unitInfo{
0021     unitInfo(): chRU(0), layerUnit(0), chAssembly(0){}
0022     int chRU;         //channel read-out unit
0023     int layerUnit;    // layer within readout unit
0024     int chAssembly;   // channel within one assembly
0025 } ;
0026 
0027 struct channelInfo{
0028     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.) {}
0029     int chRU;
0030     int rUnit;
0031     int ruID;
0032     int nrAssembly;
0033     int chAssembly;
0034     int modNr;
0035     int layer;  
0036     int rowAssembly;
0037     int colAssembly;
0038     int chID;
0039     float posX;
0040     float posY;
0041     float posZ;
0042     float modposX;
0043     float modposY;
0044 };
0045 
0046 
0047 void PrintChannelInfo(channelInfo tempC){
0048   cout  << "RU: " <<tempC.rUnit << "\t"
0049         << "RU channel: "<< tempC.chRU  << "\t"
0050         << "RU ID: "<< tempC.ruID  << "\t"
0051         << "module Nr: "<< tempC.modNr  << "\t"
0052         << "Row in assembly: "<< tempC.rowAssembly  << "\t"
0053         << "Col in assembly: "<< tempC.colAssembly  << "\t"
0054         << "Layer Nr: "<< tempC.layer  << "\t"
0055         << "Channel ID"<< tempC.chID  << "\t"
0056         << "Assembly Nr.: "<< tempC.nrAssembly  << "\t"
0057         << "Ch in assembly: "<< tempC.chAssembly  << "\t"
0058         << "X: " << tempC.posX  << "cm \t"
0059         << "Y: " << tempC.posY  << "cm \t"
0060         << "Z: " << tempC.posZ  << "cm \t"
0061         << "Mod X: " << tempC.modposX << "cm \t"
0062         << "Mod Y: " << tempC.modposY << "cm \t"
0063         << endl;
0064    return;                                
0065 }
0066           
0067 //__________________________________________________________________________________________________________
0068 //__________________________________________________________________________________________________________
0069 //__________________________________________________________________________________________________________
0070 Int_t findChannelInUnit(std::vector<unitInfo> lUnit, int layer, int chA ){
0071     Int_t currEntry = 0;
0072     // cout << "searching for: " << layer << "\t" << chA << endl;
0073     // while (lUnit.at(currEntry).layerUnit != layer && lUnit.at(currEntry).chAssembly != chA &&  currEntry < (Int_t)lUnit.size()){
0074     //   cout << "-> "<< lUnit.at(currEntry).chAssembly << "\t" << lUnit.at(currEntry).layerUnit << endl;
0075     //   currEntry++;
0076     // }
0077     Bool_t found = kFALSE;
0078     while (!found && currEntry < (Int_t)lUnit.size()){
0079       if (lUnit.at(currEntry).layerUnit == layer && lUnit.at(currEntry).chAssembly == chA){
0080         // cout << "-> "<< lUnit.at(currEntry).layerUnit<< "\t" << lUnit.at(currEntry).chAssembly  << endl;
0081         found = kTRUE;
0082       } else {
0083         // cout << currEntry << "/" << lUnit.size() << endl;
0084         if (currEntry < (Int_t)lUnit.size()) currEntry++;
0085       }
0086     }
0087     
0088     if (currEntry == (Int_t)lUnit.size()) return -1;
0089     else{
0090       // cout << "correct: "<< lUnit.at(currEntry).layerUnit<< "\t" << lUnit.at(currEntry).chAssembly  << endl;
0091       return lUnit.at(currEntry).chRU;
0092     }
0093 }
0094 
0095 
0096 //__________________________________________________________________________________________________________
0097 //__________________________________________________________________________________________________________
0098 //__________________________________________________________________________________________________________
0099 Int_t ReturnRowBoard(Int_t ch){
0100     if (ch < 5) return 1;
0101     else return 0;
0102 }
0103 Int_t ReturnColumnBoard(Int_t ch){
0104     if (ch == 1 || ch == 8) return 0;
0105     if (ch == 2 || ch == 7) return 1;
0106     if (ch == 3 || ch == 6) return 2;
0107     if (ch == 4 || ch == 5) return 3;
0108     return -1;
0109 }
0110 float ReturnPosXInLayer(Int_t ch){
0111   switch(ch){
0112     case 1: 
0113     case 8: 
0114       return -7.5; // in cm
0115       break;
0116     case 2: 
0117     case 7: 
0118       return -2.5; // in cm
0119       break;
0120     case 3: 
0121     case 6: 
0122       return 2.5; // in cm
0123       break;
0124     case 4: 
0125     case 5: 
0126       return 7.5; // in cm
0127       break;
0128     default: 
0129       return -10000; // SOMETHING went wrong
0130       break;
0131   }
0132 }
0133 float ReturnPosYInLayer(Int_t ch){
0134   switch(ch){
0135     case 1: 
0136     case 2: 
0137     case 3: 
0138     case 4: 
0139       return 2.5; // in cm
0140       break;
0141     case 5: 
0142     case 6: 
0143     case 7: 
0144     case 8: 
0145       return -2.5; // in cm
0146       break;
0147     default:
0148       return -10000; // SOMETHING went wrong
0149       break;    
0150   }
0151 }
0152 float ReturnPosZAbs(Int_t layer){
0153  return (layer+1)*2.0;
0154 }
0155 
0156 float ReturnAbsX(float XInLayer, float ModX){
0157   return XInLayer+ModX;
0158 }
0159 
0160 float ReturnAbsY(float YInLayer, float ModY){
0161   return YInLayer+ModY;
0162 }
0163 
0164 
0165 void CreateMapping(   TString filenameUnitMapping,
0166                       TString filenameLayerMapping,
0167                       TString filenameModulePositions,
0168                       TString filenameMappingWrite,
0169                       int readout = 0,                // 0 - CAEN readout, 1 - HGCROC readout
0170                       int debug = 0
0171                       
0172                   ){
0173 
0174     // ********************************************************************************************************    
0175     // read folder and name from file
0176     // ********************************************************************************************************
0177     std::vector<layerInfo> layers;
0178     std::vector<unitInfo> uChannels;
0179     std::map<int, std::pair<float,float>> positions;
0180     ifstream inUnit;
0181     inUnit.open(filenameUnitMapping,ios_base::in);
0182     if (!inUnit) {
0183         std::cout << "ERRlayerUniOR: file " << filenameUnitMapping.Data() << " not found!" << std::endl;
0184         return;
0185     }
0186 
0187     ifstream inLayerMap;
0188     inLayerMap.open(filenameLayerMapping,ios_base::in);
0189     if (!inLayerMap) {
0190         std::cout << "ERROR: file " << filenameLayerMapping.Data() << " not found!" << std::endl;
0191         return;
0192     }
0193 
0194     ifstream inModulePositions;
0195     inModulePositions.open(filenameModulePositions,ios_base::in);
0196     if (!inModulePositions) {
0197         std::cout << "ERROR: file " << filenameModulePositions.Data() << " not found!" << std::endl;
0198         return;
0199     }
0200     
0201     
0202     for( TString tempLine; tempLine.ReadLine(inUnit, kTRUE); ) {
0203         // check if line should be considered
0204         if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0205             continue;
0206         }
0207         if (debug > 1) std::cout << tempLine.Data() << std::endl;
0208 
0209         // Separate the string according to tabulators
0210         TObjArray *tempArr  = tempLine.Tokenize("\t");
0211         if(tempArr->GetEntries()<1){
0212             if (debug > 1) std::cout << "nothing to be done" << std::endl;
0213             delete tempArr;
0214             continue;
0215         } else if (tempArr->GetEntries() == 1 ){
0216             // Separate the string according to space
0217             tempArr       = tempLine.Tokenize(" ");
0218             if(tempArr->GetEntries()<1){
0219                 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0220                 delete tempArr;
0221                 continue;
0222             } else if (tempArr->GetEntries() == 1  ) {
0223                 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0224                 delete tempArr;
0225                 continue;
0226             }
0227         }
0228 
0229         // Put them to the correct variables    
0230         unitInfo tempUCh;
0231         tempUCh.chRU        = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0232         tempUCh.layerUnit   = ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();;
0233         tempUCh.chAssembly  = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0234             
0235         if (debug > 0) std::cout << "channel readout unit " << tempUCh.chRU << "\t layer in unit: " << tempUCh.layerUnit << "\t channel within assembly: "  << tempUCh.chAssembly  << std::endl;
0236         uChannels.push_back(tempUCh);
0237     }
0238 
0239     for( TString tempLine; tempLine.ReadLine(inLayerMap, kTRUE); ) {
0240         // check if line should be considered
0241         if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0242             continue;
0243         }
0244         if (debug > 1) std::cout << tempLine.Data() << std::endl;
0245 
0246         // Separate the string according to tabulators
0247         TObjArray *tempArr  = tempLine.Tokenize("\t");
0248         if(tempArr->GetEntries()<1){
0249             if (debug > 1) std::cout << "nothing to be done" << std::endl;
0250             delete tempArr;
0251             continue;
0252         } else if (tempArr->GetEntries() == 1 ){
0253             // Separate the string according to space
0254             tempArr       = tempLine.Tokenize(" ");
0255             if(tempArr->GetEntries()<1){
0256                 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0257                 delete tempArr;
0258                 continue;
0259             } else if (tempArr->GetEntries() == 1  ) {
0260                 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0261                 delete tempArr;
0262                 continue;
0263             }
0264         }
0265 
0266         // Put them to the correct variables    
0267         layerInfo tempLayer;
0268         tempLayer.layerNrAbs    = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0269         tempLayer.layerLabel  = (TString)((TObjString*)tempArr->At(1))->GetString();
0270         tempLayer.rUnit   = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0271         tempLayer.layerUnit      = ((TString)((TObjString*)tempArr->At(3))->GetString()).Atoi();
0272         tempLayer.moduleNr       = ((TString)((TObjString*)tempArr->At(4))->GetString()).Atoi();
0273         std::cerr << "Module Number: " << tempLayer.moduleNr<< std::endl;
0274         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;
0275         layers.push_back(tempLayer);
0276     }
0277 
0278     for( TString tempLine; tempLine.ReadLine(inModulePositions, kTRUE);) {
0279       if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0280           continue;
0281       }
0282       TObjArray *tempArr  = tempLine.Tokenize("\t");
0283       if(tempArr->GetEntries()<1){
0284           if (debug > 1) std::cout << "nothing to be done" << std::endl;
0285           delete tempArr;
0286           continue;
0287       } else if (tempArr->GetEntries() == 1 ){
0288           // Separate the string according to space
0289           tempArr       = tempLine.Tokenize(" ");
0290           if(tempArr->GetEntries()<1){
0291               if (debug > 1) std::cout << "nothing to be done" << std::endl;
0292               delete tempArr;
0293               continue;
0294           } else if (tempArr->GetEntries() == 1  ) {
0295               if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0296               delete tempArr;
0297               continue;
0298           }
0299       }
0300       
0301       // Fill variables
0302       positions[((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi()] = std::make_pair(((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi(),((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi());
0303     }
0304     
0305     std::vector<channelInfo> channels;
0306     fstream fileMappingClassic(filenameMappingWrite.Data(), ios::out);
0307     // fileMappingClassic.open(filenameMappingWrite, ios::out);
0308     if (readout ==0 )fileMappingClassic << "#CAEN board CAEN Ch layer   assembly    board channel   row column modNr modX modY\n" ;
0309     if (readout ==1 )fileMappingClassic << "#HGCROC board   HGCROC Ch   layer   assembly    board channel   row column modNr modX modY\n" ;
0310     TFile* outputRootFile       = new TFile("mappingTree.root","RECREATE");
0311     TTree* mapping_tree           = new TTree("mapping_tree", "mapping_tree");
0312     mapping_tree->SetDirectory(outputRootFile);
0313     channelInfo tempChannel;
0314     
0315     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");
0316     
0317     for (int l = 0; l < (int)layers.size();l++){
0318       for (int chA = 1; chA < 9; chA++){
0319           Int_t channel = findChannelInUnit(uChannels, layers.at(l).layerUnit, chA);
0320           
0321           tempChannel.chRU        = channel;
0322           tempChannel.rUnit       = layers.at(l).rUnit;
0323           tempChannel.ruID        = Int_t(tempChannel.rUnit<<8)+tempChannel.chRU;   // 8 bit read-out unit number, 8 bit channel readout unit
0324           tempChannel.modNr       = layers.at(l).moduleNr;
0325           tempChannel.layer       = layers.at(l).layerNrAbs;
0326           tempChannel.nrAssembly  = (int)(((TString)layers.at(l).layerLabel.ReplaceAll("C","")).Atoi());
0327           tempChannel.chAssembly  = chA;
0328           tempChannel.rowAssembly = ReturnRowBoard(chA);
0329           tempChannel.colAssembly = ReturnColumnBoard(chA);
0330                                     // 11 bit module Nr., 1 bit row in assembly, 2 bit column in assembly, 6 bit layer in stack
0331           tempChannel.chID        = Int_t(tempChannel.modNr<<9)+Int_t(tempChannel.rowAssembly<<8)+Int_t(tempChannel.colAssembly<<6)+tempChannel.layer;
0332           tempChannel.posX        = ReturnPosXInLayer(chA);
0333           tempChannel.posY        = ReturnPosYInLayer(chA);
0334           tempChannel.posZ        = ReturnPosZAbs(layers.at(l).layerNrAbs);
0335           tempChannel.modposX     = positions[layers.at(l).moduleNr].first;
0336           tempChannel.modposY     = positions[layers.at(l).moduleNr].second;
0337           channels.push_back(tempChannel);
0338 
0339           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 << "\t" << tempChannel.modposX << "\t" << tempChannel.modposY <<"\n";
0340           PrintChannelInfo(tempChannel);
0341           
0342           // printf("%b\t%b\t%b\t%b\t%b\n", tempChannel.modNr, tempChannel.rowAssembly, tempChannel.colAssembly, tempChannel.layer, tempChannel.chID);
0343           mapping_tree->Fill();
0344       }
0345     }
0346 
0347     
0348     fileMappingClassic.close();
0349     mapping_tree->Print();
0350     // mapping_tree->Write();
0351     outputRootFile->Write();
0352     outputRootFile->Close();
0353   return;
0354 }