Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 07:54:45

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;       // absolute layer number
0013     TString layerLabel;   // assembly name
0014     int rUnit;            // readout unit number
0015     int layerUnit;        // layer within readout unit
0016 } ;
0017 
0018 struct unitInfo{
0019     unitInfo(): chRU(0), layerUnit(0), chAssembly(0){}
0020     int chRU;         //channel read-out unit
0021     int layerUnit;    // layer within readout unit
0022     int chAssembly;   // channel within one assembly
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     // cout << "searching for: " << layer << "\t" << chA << endl;
0067     // while (lUnit.at(currEntry).layerUnit != layer && lUnit.at(currEntry).chAssembly != chA &&  currEntry < (Int_t)lUnit.size()){
0068     //   cout << "-> "<< lUnit.at(currEntry).chAssembly << "\t" << lUnit.at(currEntry).layerUnit << endl;
0069     //   currEntry++;
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         // cout << "-> "<< lUnit.at(currEntry).layerUnit<< "\t" << lUnit.at(currEntry).chAssembly  << endl;
0075         found = kTRUE;
0076       } else {
0077         // cout << currEntry << "/" << lUnit.size() << endl;
0078         if (currEntry < (Int_t)lUnit.size()) currEntry++;
0079       }
0080     }
0081     
0082     if (currEntry == (Int_t)lUnit.size()) return -1;
0083     else{
0084       // cout << "correct: "<< lUnit.at(currEntry).layerUnit<< "\t" << lUnit.at(currEntry).chAssembly  << endl;
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; // in cm
0109       break;
0110     case 2: 
0111     case 7: 
0112       return -2.5; // in cm
0113       break;
0114     case 3: 
0115     case 6: 
0116       return 2.5; // in cm
0117       break;
0118     case 4: 
0119     case 5: 
0120       return 7.5; // in cm
0121       break;
0122     default: 
0123       return -10000; // SOMETHING went wrong
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; // in cm
0134       break;
0135     case 5: 
0136     case 6: 
0137     case 7: 
0138     case 8: 
0139       return -2.5; // in cm
0140       break;
0141     default:
0142       return -10000; // SOMETHING went wrong
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 readout = 0,                // 0 - CAEN readout, 1 - HGCROC readout
0157                       int debug = 0
0158                       
0159                   ){
0160 
0161     // ********************************************************************************************************    
0162     // read folder and name from file
0163     // ********************************************************************************************************
0164     std::vector<layerInfo> layers;
0165     std::vector<unitInfo> uChannels;
0166     ifstream inUnit;
0167     inUnit.open(filenameUnitMapping,ios_base::in);
0168     if (!inUnit) {
0169         std::cout << "ERROR: file " << filenameUnitMapping.Data() << " not found!" << std::endl;
0170         return;
0171     }
0172 
0173     ifstream inLayerMap;
0174     inLayerMap.open(filenameLayerMapping,ios_base::in);
0175     if (!inLayerMap) {
0176         std::cout << "ERROR: file " << filenameLayerMapping.Data() << " not found!" << std::endl;
0177         return;
0178     }
0179 
0180     
0181     
0182     for( TString tempLine; tempLine.ReadLine(inUnit, kTRUE); ) {
0183         // check if line should be considered
0184         if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0185             continue;
0186         }
0187         if (debug > 1) std::cout << tempLine.Data() << std::endl;
0188 
0189         // Separate the string according to tabulators
0190         TObjArray *tempArr  = tempLine.Tokenize("\t");
0191         if(tempArr->GetEntries()<1){
0192             if (debug > 1) std::cout << "nothing to be done" << std::endl;
0193             delete tempArr;
0194             continue;
0195         } else if (tempArr->GetEntries() == 1 ){
0196             // Separate the string according to space
0197             tempArr       = tempLine.Tokenize(" ");
0198             if(tempArr->GetEntries()<1){
0199                 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0200                 delete tempArr;
0201                 continue;
0202             } else if (tempArr->GetEntries() == 1  ) {
0203                 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0204                 delete tempArr;
0205                 continue;
0206             }
0207         }
0208 
0209         // Put them to the correct variables    
0210         unitInfo tempUCh;
0211         tempUCh.chRU        = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0212         tempUCh.layerUnit   = ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();;
0213         tempUCh.chAssembly  = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0214             
0215         if (debug > 0) std::cout << "channel readout unit " << tempUCh.chRU << "\t layer in unit: " << tempUCh.layerUnit << "\t channel within assembly: "  << tempUCh.chAssembly  << std::endl;
0216         uChannels.push_back(tempUCh);
0217     }
0218 
0219     for( TString tempLine; tempLine.ReadLine(inLayerMap, kTRUE); ) {
0220         // check if line should be considered
0221         if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0222             continue;
0223         }
0224         if (debug > 1) std::cout << tempLine.Data() << std::endl;
0225 
0226         // Separate the string according to tabulators
0227         TObjArray *tempArr  = tempLine.Tokenize("\t");
0228         if(tempArr->GetEntries()<1){
0229             if (debug > 1) std::cout << "nothing to be done" << std::endl;
0230             delete tempArr;
0231             continue;
0232         } else if (tempArr->GetEntries() == 1 ){
0233             // Separate the string according to space
0234             tempArr       = tempLine.Tokenize(" ");
0235             if(tempArr->GetEntries()<1){
0236                 if (debug > 1) std::cout << "nothing to be done" << std::endl;
0237                 delete tempArr;
0238                 continue;
0239             } else if (tempArr->GetEntries() == 1  ) {
0240                 if (debug > 1) std::cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not been reset, no value given!" << std::endl;
0241                 delete tempArr;
0242                 continue;
0243             }
0244         }
0245 
0246         // Put them to the correct variables    
0247         layerInfo tempLayer;
0248         tempLayer.layerNrAbs    = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0249         tempLayer.layerLabel  = (TString)((TObjString*)tempArr->At(1))->GetString();
0250         tempLayer.rUnit   = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0251         tempLayer.layerUnit      = ((TString)((TObjString*)tempArr->At(3))->GetString()).Atoi();
0252             
0253         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;
0254         layers.push_back(tempLayer);
0255     }
0256     
0257     std::vector<channelInfo> channels;
0258     fstream fileMappingClassic(filenameMappingWrite.Data(), ios::out);
0259     // fileMappingClassic.open(filenameMappingWrite, ios::out);
0260     if (readout ==0 )fileMappingClassic << "#CAEN board CAEN Ch layer   assembly    board channel   row column modNr\n" ;
0261     if (readout ==1 )fileMappingClassic << "#HGCROC board   HGCROC Ch   layer   assembly    board channel   row column modNr\n" ;
0262     TFile* outputRootFile       = new TFile("mappingTree.root","RECREATE");
0263     TTree* mapping_tree           = new TTree("mapping_tree", "mapping_tree");
0264     mapping_tree->SetDirectory(outputRootFile);
0265     channelInfo tempChannel;
0266     
0267     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");
0268     
0269     for (int l = 0; l < (int)layers.size();l++){
0270       for (int chA = 1; chA < 9; chA++){
0271           Int_t channel = findChannelInUnit(uChannels, layers.at(l).layerUnit, chA);
0272           
0273           tempChannel.chRU        = channel;
0274           tempChannel.rUnit       = layers.at(l).rUnit;
0275           tempChannel.ruID        = Int_t(tempChannel.rUnit<<8)+tempChannel.chRU;   // 8 bit read-out unit number, 8 bit channel readout unit
0276           tempChannel.modNr       = 0;
0277           tempChannel.layer       = layers.at(l).layerNrAbs;
0278           tempChannel.nrAssembly  = (int)(((TString)layers.at(l).layerLabel.ReplaceAll("C","")).Atoi());
0279           tempChannel.chAssembly  = chA;
0280           tempChannel.rowAssembly = ReturnRowBoard(chA);
0281           tempChannel.colAssembly = ReturnColumnBoard(chA);
0282                                     // 11 bit module Nr., 1 bit row in assembly, 2 bit column in assembly, 6 bit layer in stack
0283           tempChannel.chID        = Int_t(tempChannel.modNr<<9)+Int_t(tempChannel.rowAssembly<<8)+Int_t(tempChannel.colAssembly<<6)+tempChannel.layer;
0284           tempChannel.posX        = ReturnPosXInLayer(chA);
0285           tempChannel.posY        = ReturnPosYInLayer(chA);
0286           tempChannel.posZ        = ReturnPosZAbs(layers.at(l).layerNrAbs);
0287           channels.push_back(tempChannel);
0288 
0289           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";
0290           PrintChannelInfo(tempChannel);
0291           
0292           // printf("%b\t%b\t%b\t%b\t%b\n", tempChannel.modNr, tempChannel.rowAssembly, tempChannel.colAssembly, tempChannel.layer, tempChannel.chID);
0293           mapping_tree->Fill();
0294       }
0295     }
0296 
0297     
0298     fileMappingClassic.close();
0299     mapping_tree->Print();
0300     // mapping_tree->Write();
0301     outputRootFile->Write();
0302     outputRootFile->Close();
0303   return;
0304 }