Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

LCDG4CalSD.cc

Go to the documentation of this file.
00001 #include "LCDG4CalSD.hh"
00002 
00003 // Geant4
00004 // #include "G4String.hh"
00005 // #include "G4Step.hh"
00006 // #include "G4HCofThisEvent.hh"
00007 // #include "G4Track.hh"
00008 
00009 #include <map>
00010 using std::map;
00011 #include <fstream>
00012 using std::ifstream;
00013 
00014 /*
00015  * Constructor
00016  */
00017 LCDG4CalSD::LCDG4CalSD(G4String name)
00018   : G4VSensitiveDetector(G4String("/lcddet/")+name)
00019   , HCID(-1)
00020   , layer0CenterDepth(0)
00021   , layerThickness(0)
00022   , debugLevel(0)
00023 {
00024   // read debugLevel flag from file ./debfile.
00025   // If no file is present, debugLevel = 0
00026   // The higher its value, the more output
00027   ifstream* debfile = new ifstream("debfile");
00028   if(debfile) {
00029     *debfile >> debugLevel;
00030     debfile->close();
00031     delete debfile;
00032     debfile = 0;
00033   }
00034 
00035   // save name of hits collection associated to this Sensitive Detector
00036   G4String HCname = name+"Collection";
00037   collectionName.insert( HCname );
00038 
00039   G4cout<< "SensitiveDetectorName = "<< SensitiveDetectorName << G4endl;
00040 }
00041 
00042 /*
00043  * Destructor
00044  */
00045 LCDG4CalSD::~LCDG4CalSD() {
00046   // methinks this causes Seg Fault JM062503
00047   // delete _hitCollection;
00048 }
00049 
00050 /*
00051  * Initialize the hit collection for each event
00052  */
00053 void LCDG4CalSD::Initialize(G4HCofThisEvent* HCE) {
00054   // Create a new hit collection for this event
00055   _hitCollection = 
00056     new LCDG4CalHitsCollection(SensitiveDetectorName,
00057                                collectionName[0]);
00058   // save hit collection id
00059   if(HCID < 0) HCID = GetCollectionID(0);
00060   // add hit collection to event
00061   HCE->AddHitsCollection(HCID,_hitCollection);
00062 }
00063 
00064 /*
00065  * Process the step information, feed it into LCDG4CalHit
00066  */
00067 G4bool LCDG4CalSD::ProcessHits(G4Step* aStep, G4TouchableHistory* ROhist) {
00068 
00069   G4double edep = aStep->GetTotalEnergyDeposit();
00070   if(edep==0) return false;
00071 
00072   const G4VPhysicalVolume* physVol
00073     = aStep->GetPreStepPoint()->GetPhysicalVolume();
00074   if( ROhist!=NULL ) {
00075     G4VPhysicalVolume* ROvol = ROhist->GetVolume();
00076     if(ROvol!=physVol) {
00077       G4cout<<"ROvol="<< ROvol <<", physVol="<< physVol <<G4endl;
00078     }
00079   }
00080 
00081   // some aliases
00082   unsigned int& sysNo = index[0];
00083   unsigned int& layno = index[1];
00084 
00085   G4LogicalVolume* logVol=physVol->GetLogicalVolume();
00086 #ifndef TXT_MODE
00087   // no need to keep track of hits in absorbers
00088   if (!_mapsens[logVol]) return false;
00089 #endif
00090 
00091   sysNo = _mapsys[logVol];
00092   layno = _maplayer[logVol];
00093   barend = _mapbe[logVol];
00094   north = _mapns[logVol];
00095 
00096   G4Track* ptrk = aStep->GetTrack();
00097   G4int trkID = ptrk->GetTrackID();
00098   G4double tdep = ptrk->GetGlobalTime();
00099 
00100   // edep hit position is at middle of preStep and postStep points
00101   G4ThreeVector pos = 0.5*(aStep->GetPreStepPoint()->GetPosition()+
00102                            aStep->GetPostStepPoint()->GetPosition() );
00103 
00104   if(debugLevel) {
00105     G4cout << "\nCheck PhysVol on pre/post step: "
00106            <<aStep->GetPreStepPoint()->GetPhysicalVolume()->GetName() <<' '
00107            <<aStep->GetPostStepPoint()->GetPhysicalVolume()->GetName()<<G4endl;
00108     G4cout << "Hit in layer " << layno << G4endl
00109            << "hit info (x,y,z)=("<< pos.x() <<';'<< pos.y() <<';'<< pos.z()
00110            <<"), cylRho="<< pos.rho() <<", th="<< pos.theta() <<", phi="<< pos.phi()
00111            << G4endl;
00112   }
00113 
00114   // Geometry dependent calculations
00115   unsigned int cellIndex = findCell(pos);
00116   G4ThreeVector cellCenter = getCellCenter(cellIndex);
00117   if(debugLevel) {
00118     G4cout<< "cellIndex="<< cellIndex
00119           <<'/'<< getIndex3(cellIndex)
00120           <<'/'<< getIndex2(cellIndex)
00121           <<'/'<< getIndex1(cellIndex) <<'/'<< sysNo
00122            << " -> cell center info (x,y,z)=("<< cellCenter.x() <<';'<< cellCenter.y() <<';'<< cellCenter.z()
00123           <<"), cylRho="<< cellCenter.rho() <<", th="<< cellCenter.theta() <<", phi="<< cellCenter.phi() <<' '<<", north="<< north << G4endl;
00124   }
00125 
00126   // Get a pointer to hit if it exists, or create a new one
00127   LCDG4CalHit *hit = NULL;
00128   map<int,LCDG4CalHit*>::iterator pcellhit = _cellhit.find(cellIndex);
00129   if( pcellhit!=_cellhit.end() ) hit = _cellhit.find(cellIndex)->second;
00130   if(hit == NULL) {
00131     if (debugLevel>3) G4cout << "creating new hit cell" << G4endl;
00132     hit = new LCDG4CalHit();
00133     hit->SetSystem(sysNo);
00134     hit->SetBarEnd(barend);
00135     hit->SetNorth(north);
00136     hit->SetLayer(layno);
00137 
00138     // set hit position to center of cell
00139     hit->SetPos(cellCenter);
00140 
00141     // save cell indices (virtual cells)
00142     hit->SetPhi( getIndex1(cellIndex) );
00143     hit->SetTheta( getIndex2(cellIndex) );
00144 
00145     // Insert hit into hit collection
00146     _hitCollection->insert(hit);
00147     _cellhit.insert( std::pair<G4int,LCDG4CalHit*>( cellIndex, hit) );
00148   }
00149 
00150   if (_mapsens[logVol]) {
00151     // hit in sensitive material
00152     hit->AddMcPart(trkID,edep,tdep);
00153   } else {
00154     // hit in absorber
00155     hit->AddMcPartAbs(edep);
00156   }
00157   if(debugLevel>3) {
00158     G4cout<<"After adding hit: hit(edep,edepa) = "
00159           << hit->GetEdep() <<' '<< hit->GetEdepAbs() << G4endl;
00160   }
00161   //    if ( hit->GetNMC() > 1 ) {
00162   //      G4cout <<"CalSD: cellid,nmc = "<< layno <<' '<< sphi <<' '<< sthe <<' '<< hit->GetNMC() << G4endl;
00163   //      for(int i=0; i<hit->GetNMC(); i++) {
00164   //        G4cout <<"  hit,id,emc = " <<' '<< i <<' '<< hit->GetMCIdx(i) <<' '<< hit->GetEMC(i) << G4endl;
00165   //      }
00166   //    }
00167 
00168   return true;
00169 }
00170 
00171 
00172 /*
00173  * EndOfEvent method clear cell hit maps for barrel and endcap
00174  */
00175 void LCDG4CalSD::EndOfEvent(G4HCofThisEvent* HCE) {
00176   if(HCE) ;  // make compiler happy
00177   // clear member hit collections.  The hits are owned by the event, 
00178   // so deleting them here will produce a crash
00179   _cellhit.clear();
00180 }
00181 
00182 /*
00183  * Add layer no, sysNo, barrel/endcap, N/S, and sensitive information
00184  * to logical volume maps
00185  */
00186 G4int LCDG4CalSD::AddLogVolInfo(G4LogicalVolume* g4logvol,
00187                                 G4int startlayerNo,
00188                                 G4int sysNo, G4int be,
00189                                 G4int ns, G4int sens)
00190 {
00191   _maplayer.insert(std::pair<G4LogicalVolume*,G4int>(g4logvol,startlayerNo));
00192   _mapsys.insert(std::pair<G4LogicalVolume*,G4int>(g4logvol,sysNo));
00193   _mapbe.insert(std::pair<G4LogicalVolume*,G4int>(g4logvol,be));
00194   _mapns.insert(std::pair<G4LogicalVolume*,G4int>(g4logvol,ns));
00195   _mapsens.insert(std::pair<G4LogicalVolume*,G4int>(g4logvol,sens));
00196   return _maplayer.size();
00197 }
00198 
00199 /*
00200  * Clear method, not used
00201  */
00202 void LCDG4CalSD::clear() {
00203 }
00204 
00205 /*
00206  * Draw method, not used
00207  */
00208 void LCDG4CalSD::DrawAll() {
00209 }
00210 
00211 /*
00212  * Print method, not used
00213  */
00214 void LCDG4CalSD::PrintAll() {
00215 }

Generated on Thu Oct 7 18:44:46 2004 for LCDG4 by doxygen 1.3.4