digits_hits/detector/src/G4SDStructure.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id: G4SDStructure.cc,v 1.3 2006/06/29 18:05:53 gunter Exp $
00028 // GEANT4 tag $Name: geant4-09-01-patch-01 $
00029 //
00030 
00031 // G4SDStructure
00032 #include "G4SDStructure.hh"
00033 #include "G4ios.hh"
00034 
00035 G4SDStructure::G4SDStructure(G4String aPath):verboseLevel(0)
00036 {
00037   pathName = aPath;
00038   dirName = aPath;
00039   G4int i = dirName.length();
00040   if( i > 1 )
00041   {
00042     dirName.remove(i-1);
00043     G4int isl = dirName.last('/');
00044     dirName.remove(0,isl+1);
00045     dirName += "/";
00046   }
00047 }
00048 
00049 G4SDStructure::~G4SDStructure()
00050 {
00051   size_t nTree = structure.size();
00052   for(size_t iTree=0;iTree<nTree;iTree++)
00053   { delete structure[iTree]; }
00054   size_t nDet = detector.size();
00055   for(size_t iDet=0;iDet<nDet;iDet++)
00056   { delete detector[iDet]; }
00057 }
00058 
00059 G4int G4SDStructure::operator==(const G4SDStructure &right) const
00060 {
00061   return (this==&right);
00062 }
00063 
00064 void G4SDStructure::AddNewDetector(G4VSensitiveDetector*aSD, 
00065                                    G4String treeStructure)
00066 {
00067   G4String remainingPath = treeStructure;
00068   remainingPath.remove(0,pathName.length());
00069   if( ! remainingPath.isNull() )
00070   {  // The detector should be kept in subdirectory.
00071      // First, check if the subdirectoy exists.
00072     G4String subD = ExtractDirName( remainingPath );
00073     G4SDStructure* tgtSDS = FindSubDirectory(subD);
00074     if( tgtSDS == 0 )
00075     { // Subdirectory not found. Create a new directory.
00076       subD.prepend(pathName);
00077       tgtSDS = new G4SDStructure(subD);
00078       structure.push_back( tgtSDS );
00079     }
00080     tgtSDS->AddNewDetector(aSD,treeStructure);
00081   }
00082   else
00083   { // The sensitive detector should be kept in this directory.
00084     G4VSensitiveDetector* tgtSD = GetSD( aSD->GetName() );
00085     if( tgtSD != 0 )
00086     {
00087       G4cout << aSD->GetName() << " had already stored in "
00088            << pathName << G4endl;
00089     }
00090     else
00091     {
00092       detector.push_back( aSD );
00093     }
00094   }
00095 }
00096 
00097 G4SDStructure* G4SDStructure::FindSubDirectory(G4String subD)
00098 {
00099   for( size_t i=0; i<structure.size(); i++ )
00100   {
00101     if( subD == structure[i]->dirName ) return structure[i];
00102   } 
00103   return 0;
00104 }
00105 
00106 G4VSensitiveDetector* G4SDStructure::GetSD(G4String aSDName)
00107 {
00108   for( size_t i=0; i<detector.size(); i++ )
00109   {
00110     G4VSensitiveDetector* tgtSD = detector[i];
00111     if( aSDName == tgtSD->GetName() ) return tgtSD;
00112   }
00113   return 0;
00114 }
00115 
00116 G4String G4SDStructure::ExtractDirName(G4String aName)
00117 {
00118   G4String subD = aName;
00119   G4int i = aName.first('/');
00120   if( i != G4int(std::string::npos) ) subD.remove(i+1);
00121   return subD;
00122 }
00123 
00124 void G4SDStructure::Activate(G4String aName, G4bool sensitiveFlag)
00125 {
00126   G4String aPath = aName;
00127   aPath.remove(0,pathName.length());
00128   if( aPath.first('/') != G4int(std::string::npos) )
00129   {  // Command is ordered for a subdirectory.
00130     G4String subD = ExtractDirName(aPath);
00131     G4SDStructure* tgtSDS = FindSubDirectory(subD);
00132     if( tgtSDS == 0 )
00133     {  // The subdirectory is not found
00134       G4cout << subD << " is not found in " << pathName << G4endl;
00135     }
00136     else
00137     { 
00138       tgtSDS->Activate(aName,sensitiveFlag);
00139     }
00140   }
00141   else if( aPath.isNull() )
00142   {  // Command is ordered for all detectors in this directory.
00143     for( size_t i=0; i<detector.size(); i++)
00144     { 
00145       detector[i]->Activate(sensitiveFlag);
00146     }
00147     for( size_t j=0;j<structure.size(); j++)
00148     {
00149       structure[j]->Activate(G4String("/"),sensitiveFlag);
00150     }
00151   }
00152   else
00153   {  // Command is ordered to a particular detector.
00154     G4VSensitiveDetector* tgtSD = GetSD(aPath);
00155     if( tgtSD == 0 )
00156     {  // The detector is not found.
00157       G4cout << aPath << " is not found in " << pathName << G4endl;
00158     }
00159     else
00160     {
00161       tgtSD->Activate(sensitiveFlag);
00162     }
00163   }
00164 }
00165 
00166 G4VSensitiveDetector* G4SDStructure::FindSensitiveDetector(G4String aName, G4bool warning)
00167 {
00168   G4String aPath = aName;
00169   aPath.remove(0,pathName.length());
00170   if( aPath.first('/') != G4int(std::string::npos) )
00171   { // SD exists in sub-directory
00172     G4String subD = ExtractDirName(aPath);
00173     G4SDStructure* tgtSDS = FindSubDirectory(subD);
00174     if( tgtSDS == 0 )
00175     {  // The subdirectory is not found
00176       if (warning)
00177         G4cout << subD << " is not found in " << pathName << G4endl;
00178       return 0;
00179     }
00180     else
00181     {
00182       return tgtSDS->FindSensitiveDetector(aName);
00183     }
00184   }
00185   else
00186   { // SD must exist in this directory
00187     G4VSensitiveDetector* tgtSD = GetSD(aPath);
00188     if( tgtSD == 0 )
00189     {  // The detector is not found.
00190       if (warning)
00191         G4cout << aPath << " is not found in " << pathName << G4endl;
00192     }
00193     return tgtSD;
00194   }
00195 }
00196 
00197 void G4SDStructure::Initialize(G4HCofThisEvent*HCE)
00198 {
00199   size_t i;
00200   // Broadcast to subdirectories.
00201   for( i=0; i<structure.size(); i++ )
00202   {
00203     structure[i]->Initialize(HCE);
00204   }
00205   // Initialize all detectors in this directory.
00206   for( i=0; i<detector.size(); i++ )
00207   {
00208     if(detector[i]->isActive()) detector[i]->Initialize(HCE);
00209   }
00210 }
00211 
00212 void G4SDStructure::Terminate(G4HCofThisEvent*HCE)
00213 {
00214   size_t i;
00215   // Broadcast to subdirectories.
00216   for( i=0; i<structure.size(); i++ )
00217   {
00218     structure[i]->Terminate(HCE);
00219   }
00220   // Initialize all detectors in this directory.
00221   for( i=0; i<detector.size(); i++ )
00222   {
00223     if(detector[i]->isActive()) detector[i]->EndOfEvent(HCE);
00224   }
00225 }
00226 
00227 void G4SDStructure::ListTree()
00228 {
00229   G4cout << pathName << G4endl;
00230   for(size_t i=0; i<detector.size(); i++)
00231   {
00232     G4VSensitiveDetector* sd = detector[i];
00233     G4cout << pathName << sd->GetName();
00234     if( sd->isActive() )
00235     { G4cout << "   *** Active "; }
00236     else
00237     { G4cout << "   XXX Inactive "; }
00238     G4cout << G4endl;
00239   }
00240   for(size_t j=0; j<structure.size(); j++)
00241   { structure[j]->ListTree(); }
00242 }
00243         
00244 

Generated on Fri Apr 11 17:09:46 2008 for Geant4 by  doxygen 1.4.7