CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/SimG4CMS/Forward/src/ZdcNumberingScheme.cc

Go to the documentation of this file.
00001 
00002 // File: ZdcNumberingScheme.cc
00003 // Date: 02.04
00004 // Description: Numbering scheme for Zdc
00005 // Modifications:
00007 #include "SimG4CMS/Forward/interface/ZdcNumberingScheme.h"
00008 #include "DataFormats/HcalDetId/interface/HcalZDCDetId.h"
00009 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00010 #include <iostream>
00011 #undef debug
00012 
00013 ZdcNumberingScheme::ZdcNumberingScheme(int iv){
00014   verbosity = iv;
00015   if (verbosity>0) 
00016     std::cout << "Creating ZDCNumberingScheme" << std::endl;
00017 }
00018 
00019 ZdcNumberingScheme::~ZdcNumberingScheme() {
00020   if (verbosity>0) 
00021     std::cout<< " Deleting ZdcNumberingScheme" <<  std::endl;
00022 }
00023 
00024 
00025 void ZdcNumberingScheme::setVerbosity(const int iv){ verbosity = iv; }
00026 
00027 unsigned int ZdcNumberingScheme::getUnitID(const G4Step* aStep) const {
00028 
00029   uint32_t  index = 0;
00030   int level = detectorLevel(aStep);
00031  
00032   if (level > 0) {
00033     int*      copyno = new int[level];
00034     G4String* name   = new G4String[level];
00035 
00036     detectorLevel(aStep, level, copyno, name);
00037 
00038     int zside   = 0;
00039     int channel = 0;
00040     int fiber   = 0;
00041     int layer   = 0;
00042     HcalZDCDetId::Section section = HcalZDCDetId::Unknown;
00043     
00044     for (int ich=0; ich  <  level; ich++) {
00045       if (name[ich] == "ZDC") {
00046         if(copyno[ich] == 1)zside = 1;
00047         if(copyno[ich] == 2)zside = -1;
00048       } 
00049       else if (name[ich] == "ZDC_EMLayer") {
00050         section = HcalZDCDetId::EM;
00051         layer = copyno[ich];
00052       }
00053       else if (name[ich] == "ZDC_EMFiber") {
00054         fiber = copyno[ich];
00055         if (fiber < 20)
00056           channel = 1;
00057         else if (fiber < 39)            
00058           channel = 2;                         
00059         else if (fiber < 58)
00060           channel = 3; 
00061         else if (fiber < 77)
00062           channel = 4;
00063         else
00064           channel = 5;  
00065       } 
00066       else if (name[ich] == "ZDC_LumLayer") {
00067         section = HcalZDCDetId::LUM;
00068         layer = copyno[ich];
00069         channel = layer;
00070       }
00071       else if (name[ich] == "ZDC_LumGas") {
00072         fiber = 1;
00073       }
00074       else if (name[ich] == "ZDC_HadLayer") {
00075         section = HcalZDCDetId::HAD;
00076         layer = copyno[ich];
00077         if (layer < 6)
00078           channel = 1;
00079         else if (layer < 12)
00080           channel = 2;
00081         else if (layer < 18)
00082           channel = 3;
00083         else
00084           channel = 4;
00085       }
00086       else if (name[ich] == "ZDC_HadFiber") {
00087         fiber = copyno[ich];
00088       }
00089     }
00090  
00091 #ifdef debug
00092     unsigned intindex=0;
00093     // intindex = myPacker.packZdcIndex (section, layer, fiber, channel, zside);
00094     intindex = packZdcIndex (section, layer, fiber, channel, zside);
00095 #endif
00096 
00097     bool true_for_positive_eta = true;
00098     //if(zside == 1)true_for_positive_eta = true;
00099     if(zside == -1)true_for_positive_eta = false;
00100 
00101     HcalZDCDetId zdcId(section, true_for_positive_eta, channel);
00102     index = zdcId.rawId();
00103 
00104 #ifdef debug
00105     std::cout<<"DetectorId: ";
00106     std::cout<<zdcId<<std::endl;
00107 
00108     
00109     std::cout<< "ZdcNumberingScheme:" 
00110              << "  getUnitID - # of levels = " 
00111              << level << std::endl;
00112     for (int ich = 0; ich < level; ich++)
00113       std::cout<< "  " << ich  << ": copyno " << copyno[ich] 
00114                << " name="  << name[ich]
00115                << "  section " << section << " zside " << zside
00116                << " layer " << layer << " fiber " << fiber
00117                << " channel " << channel << "packedIndex ="
00118                << intindex << " detId raw: "<<index<<std::endl;
00119                
00120 #endif
00121     
00122     delete[] copyno;
00123     delete[] name;
00124   }
00125   
00126   return index;
00127   
00128 }
00129 
00130 unsigned ZdcNumberingScheme::packZdcIndex(int section, int layer, int fiber,
00131                                           int channel, int z){
00132   unsigned int idx = ((z-1)&1)<<20;       //bit 20
00133   idx += (channel&7)<<17;                 //bits 17-19
00134   idx += (fiber&255)<<9;                  //bits 9-16
00135   idx += (layer&127)<<2;                  //bits 2-8
00136   idx += (section&3);                      //bits 0-1
00137 
00138   #ifdef debug
00139   std::cout<< "ZDC packing: section " << section << " layer  " << layer << " fiber "
00140            << fiber  << " channel " <<  channel << " zside "  << z << "idx: " <<idx <<  std::endl;
00141   int newsubdet, newlayer, newfiber, newchannel, newz;
00142   unpackZdcIndex(idx, newsubdet, newlayer, newfiber, newchannel, newz);
00143   #endif
00144 
00145   return idx;
00146 }
00147 
00148 
00149 void ZdcNumberingScheme::unpackZdcIndex(const unsigned int& idx, int& section,
00150                                         int& layer, int& fiber,
00151                                         int& channel, int& z) {
00152   z = 1 + ((idx>>20)&1);
00153   channel = (idx>>17)&7;
00154   fiber = (idx>>9)&255;
00155   layer = (idx>>2)&127;
00156   section = idx&3;
00157           
00158   #ifdef debug
00159   std::cout<< "ZDC unpacking: idx:"<< idx << " -> section " << section
00160            << " layer " << layer << " fiber " << fiber << " channel " 
00161            << channel << " zside " << z <<  std::endl;
00162   #endif 
00163 }
00164 
00165 int ZdcNumberingScheme::detectorLevel(const G4Step* aStep) const {
00166   
00167   //Find number of levels
00168   const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
00169   int level = 0;
00170   if (touch) level = ((touch->GetHistoryDepth())+1);
00171   return level;
00172 }
00173   
00174 void ZdcNumberingScheme::detectorLevel(const G4Step* aStep, int& level,
00175                                           int* copyno, G4String* name) const {
00176  
00177   //Get name and copy numbers
00178   if (level > 0) {
00179     const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
00180     for (int ii = 0; ii < level; ii++) {
00181       int i      = level - ii - 1;
00182       name[ii]   = touch->GetVolume(i)->GetName();
00183       copyno[ii] = touch->GetReplicaNumber(i);
00184     }
00185   }
00186 }