Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-27 07:31:39

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 segSize;
0039     int chID;
0040     float posX;
0041     float posY;
0042     float posZ;
0043     float modposX;
0044     float modposY;
0045 };
0046 
0047 
0048 void PrintChannelInfo(channelInfo tempC){
0049   cout  << "RU: " <<tempC.rUnit << "\t"
0050         << "RU channel: "<< tempC.chRU  << "\t"
0051         << "RU ID: "<< tempC.ruID  << "\t"
0052         << "module Nr: "<< tempC.modNr  << "\t"
0053         << "Row in assembly: "<< tempC.rowAssembly  << "\t"
0054         << "Col in assembly: "<< tempC.colAssembly  << "\t"
0055         << "Layer Nr: "<< tempC.layer  << "\t"
0056         << "Channel ID: "<< tempC.chID  << "\t"
0057         << "Assembly Nr.: "<< tempC.nrAssembly  << "\t"
0058         << "Ch in assembly: "<< tempC.chAssembly  << "\t"
0059         << "Segment lenght: "<< tempC.segSize  << "\t"
0060         << "X: " << tempC.posX  << "cm \t"
0061         << "Y: " << tempC.posY  << "cm \t"
0062         << "Z: " << tempC.posZ  << "cm \t"
0063         << "Mod X: " << tempC.modposX << "cm \t"
0064         << "Mod Y: " << tempC.modposY << "cm \t"
0065         << endl;
0066    return;                                
0067 }
0068           
0069 //__________________________________________________________________________________________________________
0070 //__________________________________________________________________________________________________________
0071 //__________________________________________________________________________________________________________
0072 Int_t findChannelInUnit(std::vector<unitInfo> lUnit, int layer, int chA ){
0073     Int_t currEntry = 0;
0074     // cout << "searching for: " << layer << "\t" << chA << endl;
0075     // while (lUnit.at(currEntry).layerUnit != layer && lUnit.at(currEntry).chAssembly != chA &&  currEntry < (Int_t)lUnit.size()){
0076     //   cout << "-> "<< lUnit.at(currEntry).chAssembly << "\t" << lUnit.at(currEntry).layerUnit << endl;
0077     //   currEntry++;
0078     // }
0079     Bool_t found = kFALSE;
0080     while (!found && currEntry < (Int_t)lUnit.size()){
0081       if (lUnit.at(currEntry).layerUnit == layer && lUnit.at(currEntry).chAssembly == chA){
0082         // cout << "-> "<< lUnit.at(currEntry).layerUnit<< "\t" << lUnit.at(currEntry).chAssembly  << endl;
0083         found = kTRUE;
0084       } else {
0085         // cout << currEntry << "/" << lUnit.size() << endl;
0086         if (currEntry < (Int_t)lUnit.size()) currEntry++;
0087       }
0088     }
0089     
0090     if (currEntry == (Int_t)lUnit.size()) return -1;
0091     else{
0092       // cout << "correct: "<< lUnit.at(currEntry).layerUnit<< "\t" << lUnit.at(currEntry).chAssembly  << endl;
0093       return lUnit.at(currEntry).chRU;
0094     }
0095 }
0096 
0097 
0098 //__________________________________________________________________________________________________________
0099 //__________________________________________________________________________________________________________
0100 //__________________________________________________________________________________________________________
0101 Int_t ReturnRowBoard(Int_t ch, Int_t inverted){
0102   if (inverted > 0){
0103     if (ch < 5) return 1;
0104     else return 0;
0105   } else {
0106     if (ch < 5) return 0;
0107     else return 1;    
0108   }
0109 }
0110 Int_t ReturnColumnBoard(Int_t ch, Int_t inverted){
0111   if (inverted > 0){
0112     if (ch == 1 || ch == 8) return 0;
0113     if (ch == 2 || ch == 7) return 1;
0114     if (ch == 3 || ch == 6) return 2;
0115     if (ch == 4 || ch == 5) return 3;
0116     return -1;
0117   } else {
0118     if (ch == 1 || ch == 8) return 3;
0119     if (ch == 2 || ch == 7) return 2;
0120     if (ch == 3 || ch == 6) return 1;
0121     if (ch == 4 || ch == 5) return 0;    
0122     return -1;
0123   }
0124 }
0125 float ReturnPosXInLayer(Int_t ch){
0126   switch(ch){
0127     case 1: 
0128     case 8: 
0129       return -7.5; // in cm
0130       break;
0131     case 2: 
0132     case 7: 
0133       return -2.5; // in cm
0134       break;
0135     case 3: 
0136     case 6: 
0137       return 2.5; // in cm
0138       break;
0139     case 4: 
0140     case 5: 
0141       return 7.5; // in cm
0142       break;
0143     default: 
0144       return -10000; // SOMETHING went wrong
0145       break;
0146   }
0147 }
0148 float ReturnPosYInLayer(Int_t ch){
0149   switch(ch){
0150     case 1: 
0151     case 2: 
0152     case 3: 
0153     case 4: 
0154       return 2.5; // in cm
0155       break;
0156     case 5: 
0157     case 6: 
0158     case 7: 
0159     case 8: 
0160       return -2.5; // in cm
0161       break;
0162     default:
0163       return -10000; // SOMETHING went wrong
0164       break;    
0165   }
0166 }
0167 
0168 float ReturnPosZAbs(Int_t layer, int opt){
0169   if (opt == 0){
0170     return (layer+1)*2.0;
0171   } else if (opt == 1){
0172     switch (layer){
0173       case 0:         // summing 5 layers each 2cm
0174         return 5.0;
0175       case 1:         // summing 5 layers each 2cm
0176         return 5.0+10.;
0177       case 2:         // summing 10 layers each 2cm
0178         return 30.;
0179       case 3:         // summing 10 layers each 2cm
0180         return 30.+20.;
0181       case 4:         // summing 10 layers each 2cm
0182         return 30.+2*20.;
0183       case 5:         // summing 10 layers each 2cm
0184         return 30.+3*20.;
0185       case 6:         // summing 5 layers each 2cm
0186         return 105.;
0187       case 7:         // summing 5 layers each 2cm
0188         return 105.+10.;
0189       default:
0190         return -1.;
0191     }
0192   } else if (opt == 2){
0193     switch (layer){
0194       case 0:         // summing 5 layers each 2cm
0195         return 5.0;
0196       case 1:         // summing 5 layers each 2cm
0197         return 5.0+10.;
0198       case 2:         // summing 5 layers each 2cm
0199         return 5.0+2*10.;
0200       case 3:         // summing 5 layers each 2cm
0201         return 5.0+3*10.;
0202       case 4:         // summing 10 layers each 2cm
0203         return 50.;
0204       case 5:         // summing 10 layers each 2cm
0205         return 50.+1*20.;
0206       case 6:         // summing 10 layers each 2cm
0207         return 50.+2*20.;
0208       case 7:         // summing 10 layers each 2cm
0209         return 50.+3*20.;
0210       default:
0211         return -1.;
0212     }
0213   }
0214   return -1.;
0215 }
0216 
0217 float ReturnAbsX(float XInLayer, float ModX){
0218   return XInLayer+ModX;
0219 }
0220 
0221 float ReturnAbsY(float YInLayer, float ModY){
0222   return YInLayer+ModY;
0223 }
0224 
0225 int ReturnLayerInSegment(Int_t layer, int opt){
0226     if (opt == 0){
0227     return 1;
0228   } else if (opt == 1){
0229     switch (layer){
0230       // summing 5 layers
0231       case 0:    
0232       case 1:         
0233       case 6:         
0234       case 7:         
0235         return 5;     
0236       // summing 10 layers
0237       case 2:         
0238       case 3:
0239       case 4:
0240       case 5:
0241         return 10;
0242       default:
0243         return 1;
0244     }
0245   } else if (opt == 2){
0246     switch (layer){
0247       // summing 5 layers
0248       case 0:    
0249       case 1:         
0250       case 2:         
0251       case 3:
0252         return 5;     
0253       // summing 10 layers
0254       case 4:
0255       case 5:
0256       case 6:         
0257       case 7:         
0258         return 10;
0259       default:
0260         return 1;
0261     }
0262   }
0263   return -1.;
0264 }
0265 
0266 void CreateMapping(   TString filenameUnitMapping,
0267                       TString filenameLayerMapping,
0268                       TString filenameModulePositions,
0269                       TString filenameMappingWrite,
0270                       int readout = 0,                // 0 - CAEN readout, 1 - HGCROC readout
0271                       int summingOpt = 0,
0272                       int debug = 0
0273                       
0274                   ){
0275 
0276     // ********************************************************************************************************    
0277     // read folder and name from file
0278     // ********************************************************************************************************
0279     std::vector<layerInfo> layers;
0280     std::vector<unitInfo> uChannels;
0281     std::map<int, std::pair<float,float>> positions;
0282     std::map<int, int> moddirections;
0283     ifstream inUnit;
0284     inUnit.open(filenameUnitMapping,ios_base::in);
0285     if (!inUnit) {
0286         std::cout << "ERRlayerUniOR: file " << filenameUnitMapping.Data() << " not found!" << std::endl;
0287         return;
0288     }
0289 
0290     ifstream inLayerMap;
0291     inLayerMap.open(filenameLayerMapping,ios_base::in);
0292     if (!inLayerMap) {
0293         std::cout << "ERROR: file " << filenameLayerMapping.Data() << " not found!" << std::endl;
0294         return;
0295     }
0296 
0297     ifstream inModulePositions;
0298     inModulePositions.open(filenameModulePositions,ios_base::in);
0299     if (!inModulePositions) {
0300         std::cout << "ERROR: file " << filenameModulePositions.Data() << " not found!" << std::endl;
0301         return;
0302     }
0303     
0304     
0305     for( TString tempLine; tempLine.ReadLine(inUnit, kTRUE); ) {
0306         // check if line should be considered
0307         if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0308             continue;
0309         }
0310         if (debug > 1) std::cout << tempLine.Data() << std::endl;
0311 
0312         // Separate the string according to tabulators
0313         TObjArray *tempArr  = tempLine.Tokenize("\t");
0314         if(tempArr->GetEntries()<1){
0315             if (debug > 1) std::cout << "nothing to be done" << std::endl;
0316             delete tempArr;
0317             continue;
0318         } else if (tempArr->GetEntries() == 1 ){
0319             // Separate the string according to space
0320             tempArr       = tempLine.Tokenize(" ");
0321             if(tempArr->GetEntries()<1){
0322                 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0323                 delete tempArr;
0324                 continue;
0325             } else if (tempArr->GetEntries() == 1  ) {
0326                 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0327                 delete tempArr;
0328                 continue;
0329             }
0330         }
0331 
0332         // Put them to the correct variables    
0333         unitInfo tempUCh;
0334         tempUCh.chRU        = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0335         tempUCh.layerUnit   = ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();;
0336         tempUCh.chAssembly  = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0337             
0338         if (debug > 0) std::cout << "channel readout unit " << tempUCh.chRU << "\t layer in unit: " << tempUCh.layerUnit << "\t channel within assembly: "  << tempUCh.chAssembly  << std::endl;
0339         uChannels.push_back(tempUCh);
0340     }
0341 
0342     for( TString tempLine; tempLine.ReadLine(inLayerMap, kTRUE); ) {
0343         // check if line should be considered
0344         if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0345             continue;
0346         }
0347         if (debug > 1) std::cout << tempLine.Data() << std::endl;
0348 
0349         // Separate the string according to tabulators
0350         TObjArray *tempArr  = tempLine.Tokenize("\t");
0351         if(tempArr->GetEntries()<1){
0352             if (debug > 1) std::cout << "nothing to be done" << std::endl;
0353             delete tempArr;
0354             continue;
0355         } else if (tempArr->GetEntries() == 1 ){
0356             // Separate the string according to space
0357             tempArr       = tempLine.Tokenize(" ");
0358             if(tempArr->GetEntries()<1){
0359                 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0360                 delete tempArr;
0361                 continue;
0362             } else if (tempArr->GetEntries() == 1  ) {
0363                 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0364                 delete tempArr;
0365                 continue;
0366             }
0367         }
0368 
0369         // Put them to the correct variables    
0370         layerInfo tempLayer;
0371         tempLayer.layerNrAbs    = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0372         tempLayer.layerLabel  = (TString)((TObjString*)tempArr->At(1))->GetString();
0373         tempLayer.rUnit   = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0374         tempLayer.layerUnit      = ((TString)((TObjString*)tempArr->At(3))->GetString()).Atoi();
0375         tempLayer.moduleNr       = ((TString)((TObjString*)tempArr->At(4))->GetString()).Atoi();
0376         std::cerr << "Module Number: " << tempLayer.moduleNr<< std::endl;
0377         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;
0378         layers.push_back(tempLayer);
0379     }
0380 
0381     for( TString tempLine; tempLine.ReadLine(inModulePositions, kTRUE);) {
0382       if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0383           continue;
0384       }
0385       TObjArray *tempArr  = tempLine.Tokenize("\t");
0386       if(tempArr->GetEntries()<1){
0387           if (debug > 1) std::cout << "nothing to be done" << std::endl;
0388           delete tempArr;
0389           continue;
0390       } else if (tempArr->GetEntries() == 1 ){
0391           // Separate the string according to space
0392           tempArr       = tempLine.Tokenize(" ");
0393           if(tempArr->GetEntries()<1){
0394               if (debug > 1) std::cout << "nothing to be done" << std::endl;
0395               delete tempArr;
0396               continue;
0397           } else if (tempArr->GetEntries() == 1  ) {
0398               if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0399               delete tempArr;
0400               continue;
0401           }
0402       }
0403       
0404       // Fill variables
0405       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());
0406       moddirections[((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi()] = ((TString)((TObjString*)tempArr->At(3))->GetString()).Atoi();
0407     }
0408     
0409     std::vector<channelInfo> channels;
0410     fstream fileMappingClassic(filenameMappingWrite.Data(), ios::out);
0411     // fileMappingClassic.open(filenameMappingWrite, ios::out);
0412     fileMappingClassic<<"sumOpt\t" << summingOpt << "\n";
0413     if (readout ==0 )fileMappingClassic << "#CAEN board CAEN Ch layer   assembly    board channel   row column modNr modX modY\n" ;
0414     if (readout ==1 )fileMappingClassic << "#HGCROC board   HGCROC Ch   layer   assembly    board channel   row column modNr modX modY\n" ;
0415     TFile* outputRootFile       = new TFile("mappingTree.root","RECREATE");
0416     TTree* mapping_tree           = new TTree("mapping_tree", "mapping_tree");
0417     mapping_tree->SetDirectory(outputRootFile);
0418     channelInfo tempChannel;
0419     
0420     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");
0421     
0422     for (int l = 0; l < (int)layers.size();l++){
0423       for (int chA = 1; chA < 9; chA++){
0424           Int_t channel = findChannelInUnit(uChannels, layers.at(l).layerUnit, chA);
0425           
0426           tempChannel.chRU        = channel;
0427           tempChannel.rUnit       = layers.at(l).rUnit;
0428           tempChannel.ruID        = Int_t(tempChannel.rUnit<<8)+tempChannel.chRU;   // 8 bit read-out unit number, 8 bit channel readout unit
0429           tempChannel.modNr       = layers.at(l).moduleNr;
0430           tempChannel.layer       = layers.at(l).layerNrAbs;
0431                     tempChannel.nrAssembly  = (int)(((TString)layers.at(l).layerLabel).Atoi());  
0432           tempChannel.chAssembly  = chA;
0433           tempChannel.rowAssembly = ReturnRowBoard(chA,moddirections[layers.at(l).moduleNr]);
0434           tempChannel.colAssembly = ReturnColumnBoard(chA,moddirections[layers.at(l).moduleNr]);
0435                                     // 11 bit module Nr., 1 bit row in assembly, 2 bit column in assembly, 6 bit layer in stack
0436           tempChannel.chID        = Int_t(tempChannel.modNr<<9)+Int_t(tempChannel.rowAssembly<<8)+Int_t(tempChannel.colAssembly<<6)+tempChannel.layer;
0437           tempChannel.posX        = ReturnPosXInLayer(chA);
0438           tempChannel.posY        = ReturnPosYInLayer(chA);
0439           tempChannel.posZ        = ReturnPosZAbs(layers.at(l).layerNrAbs, summingOpt);
0440           tempChannel.modposX     = positions[layers.at(l).moduleNr].first;
0441           tempChannel.modposY     = positions[layers.at(l).moduleNr].second;
0442           tempChannel.segSize     = ReturnLayerInSegment(layers.at(l).layerNrAbs, summingOpt);
0443           channels.push_back(tempChannel);
0444 
0445           fileMappingClassic << layers.at(l).rUnit << "\t" << channel << "\t" << layers.at(l).layerNrAbs << "\t" << layers.at(l).layerLabel << "\t" <<  chA << "\t" << ReturnRowBoard(chA,moddirections[layers.at(l).moduleNr]) << "\t" << ReturnColumnBoard(chA,moddirections[layers.at(l).moduleNr]) << "\t" << tempChannel.modNr << "\t" << tempChannel.modposX << "\t" << tempChannel.modposY << "\t" << tempChannel.segSize << "\n";
0446           PrintChannelInfo(tempChannel);
0447           
0448           // printf("%b\t%b\t%b\t%b\t%b\n", tempChannel.modNr, tempChannel.rowAssembly, tempChannel.colAssembly, tempChannel.layer, tempChannel.chID);
0449           mapping_tree->Fill();
0450       }
0451     }
0452 
0453     
0454     fileMappingClassic.close();
0455     mapping_tree->Print();
0456     // mapping_tree->Write();
0457     outputRootFile->Write();
0458     outputRootFile->Close();
0459   return;
0460 }