Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:15:47

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