CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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   unsigned intindex=0;
00030   uint32_t  index = 0;
00031   int level = detectorLevel(aStep);
00032  
00033   if (level > 0) {
00034     int*      copyno = new int[level];
00035     G4String* name   = new G4String[level];
00036 
00037     detectorLevel(aStep, level, copyno, name);
00038 
00039     int zside   = 0;
00040     int channel = 0;
00041     int fiber   = 0;
00042     int layer   = 0;
00043     HcalZDCDetId::Section section = HcalZDCDetId::Unknown;
00044     
00045     for (int ich=0; ich  <  level; ich++) {
00046       if (name[ich] == "ZDC") {
00047         if(copyno[ich] == 1)zside = 1;
00048         if(copyno[ich] == 2)zside = -1;
00049       } 
00050       else if (name[ich] == "ZDC_EMLayer") {
00051         section = HcalZDCDetId::EM;
00052         layer = copyno[ich];
00053       }
00054       else if (name[ich] == "ZDC_EMFiber") {
00055         fiber = copyno[ich];
00056         if (fiber < 20)
00057           channel = 1;
00058         else if (fiber < 39)            
00059           channel = 2;                         
00060         else if (fiber < 58)
00061           channel = 3; 
00062         else if (fiber < 77)
00063           channel = 4;
00064         else
00065           channel = 5;  
00066       } 
00067       else if (name[ich] == "ZDC_LumLayer") {
00068         section = HcalZDCDetId::LUM;
00069         layer = copyno[ich];
00070         channel = layer;
00071       }
00072       else if (name[ich] == "ZDC_LumGas") {
00073         fiber = 1;
00074       }
00075       else if (name[ich] == "ZDC_HadLayer") {
00076         section = HcalZDCDetId::HAD;
00077         layer = copyno[ich];
00078         if (layer < 6)
00079           channel = 1;
00080         else if (layer < 12)
00081           channel = 2;
00082         else if (layer < 18)
00083           channel = 3;
00084         else
00085           channel = 4;
00086       }
00087       else if (name[ich] == "ZDC_HadFiber") {
00088         fiber = copyno[ich];
00089       }
00090     }
00091  
00092     // intindex = myPacker.packZdcIndex (section, layer, fiber, channel, zside);
00093     intindex = packZdcIndex (section, layer, fiber, channel, zside);
00094     bool true_for_positive_eta = true;
00095     //if(zside == 1)true_for_positive_eta = true;
00096     if(zside == -1)true_for_positive_eta = false;
00097 
00098     HcalZDCDetId zdcId(section, true_for_positive_eta, channel);
00099     index = zdcId.rawId();
00100 
00101 #ifdef debug
00102     std::cout<<"DetectorId: ";
00103     std::cout<<zdcId<<std::endl;
00104 
00105     
00106     std::cout<< "ZdcNumberingScheme:" 
00107              << "  getUnitID - # of levels = " 
00108              << level << std::endl;
00109     for (int ich = 0; ich < level; ich++)
00110       std::cout<< "  " << ich  << ": copyno " << copyno[ich] 
00111                << " name="  << name[ich]
00112                << "  section " << section << " zside " << zside
00113                << " layer " << layer << " fiber " << fiber
00114                << " channel " << channel << "packedIndex ="
00115                << intindex << " detId raw: "<<index<<std::endl;
00116                
00117 #endif
00118     
00119     delete[] copyno;
00120     delete[] name;
00121   }
00122   
00123   return index;
00124   
00125 }
00126 
00127 unsigned ZdcNumberingScheme::packZdcIndex(int section, int layer, int fiber,
00128                                           int channel, int z){
00129   unsigned int idx = ((z-1)&1)<<20;       //bit 20
00130   idx += (channel&7)<<17;                 //bits 17-19
00131   idx += (fiber&255)<<9;                  //bits 9-16
00132   idx += (layer&127)<<2;                  //bits 2-8
00133   idx += (section&3);                      //bits 0-1
00134 
00135   #ifdef debug
00136   std::cout<< "ZDC packing: section " << section << " layer  " << layer << " fiber "
00137            << fiber  << " channel " <<  channel << " zside "  << z << "idx: " <<idx <<  std::endl;
00138   int newsubdet, newlayer, newfiber, newchannel, newz;
00139   unpackZdcIndex(idx, newsubdet, newlayer, newfiber, newchannel, newz);
00140   #endif
00141 
00142   return idx;
00143 }
00144 
00145 
00146 void ZdcNumberingScheme::unpackZdcIndex(const unsigned int& idx, int& section,
00147                                         int& layer, int& fiber,
00148                                         int& channel, int& z) {
00149   z = 1 + ((idx>>20)&1);
00150   channel = (idx>>17)&7;
00151   fiber = (idx>>9)&255;
00152   layer = (idx>>2)&127;
00153   section = idx&3;
00154           
00155   #ifdef debug
00156   std::cout<< "ZDC unpacking: idx:"<< idx << " -> section " << section
00157            << " layer " << layer << " fiber " << fiber << " channel " 
00158            << channel << " zside " << z <<  std::endl;
00159   #endif 
00160 }
00161 
00162 int ZdcNumberingScheme::detectorLevel(const G4Step* aStep) const {
00163   
00164   //Find number of levels
00165   const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
00166   int level = 0;
00167   if (touch) level = ((touch->GetHistoryDepth())+1);
00168   return level;
00169 }
00170   
00171 void ZdcNumberingScheme::detectorLevel(const G4Step* aStep, int& level,
00172                                           int* copyno, G4String* name) const {
00173  
00174   //Get name and copy numbers
00175   if (level > 0) {
00176     const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
00177     for (int ii = 0; ii < level; ii++) {
00178       int i      = level - ii - 1;
00179       name[ii]   = touch->GetVolume(i)->GetName();
00180       copyno[ii] = touch->GetReplicaNumber(i);
00181     }
00182   }
00183 }