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$
00028 //
00029 
00030 // G4SDStructure
00031 #include "G4SDStructure.hh"
00032 #include "G4ios.hh"
00033 
00034 G4SDStructure::G4SDStructure(G4String aPath):verboseLevel(0)
00035 {
00036   pathName = aPath;
00037   dirName = aPath;
00038   G4int i = dirName.length();
00039   if( i > 1 )
00040   {
00041     dirName.remove(i-1);
00042     G4int isl = dirName.last('/');
00043     dirName.remove(0,isl+1);
00044     dirName += "/";
00045   }
00046 }
00047 
00048 G4SDStructure::~G4SDStructure()
00049 {
00050   size_t nTree = structure.size();
00051   for(size_t iTree=0;iTree<nTree;iTree++)
00052   { delete structure[iTree]; }
00053   size_t nDet = detector.size();
00054   for(size_t iDet=0;iDet<nDet;iDet++)
00055   { delete detector[iDet]; }
00056 }
00057 
00058 G4int G4SDStructure::operator==(const G4SDStructure &right) const
00059 {
00060   return (this==&right);
00061 }
00062 
00063 void G4SDStructure::AddNewDetector(G4VSensitiveDetector*aSD, 
00064                                    G4String treeStructure)
00065 {
00066   G4String remainingPath = treeStructure;
00067   remainingPath.remove(0,pathName.length());
00068   if( ! remainingPath.isNull() )
00069   {  // The detector should be kept in subdirectory.
00070      // First, check if the subdirectoy exists.
00071     G4String subD = ExtractDirName( remainingPath );
00072     G4SDStructure* tgtSDS = FindSubDirectory(subD);
00073     if( tgtSDS == 0 )
00074     { // Subdirectory not found. Create a new directory.
00075       subD.prepend(pathName);
00076       tgtSDS = new G4SDStructure(subD);
00077       structure.push_back( tgtSDS );
00078     }
00079     tgtSDS->AddNewDetector(aSD,treeStructure);
00080   }
00081   else
00082   { // The sensitive detector should be kept in this directory.
00083     G4VSensitiveDetector* tgtSD = GetSD( aSD->GetName() );
00084     if( tgtSD != 0 )
00085     {
00086       G4cout << aSD->GetName() << " had already stored in "
00087            << pathName << G4endl;
00088     }
00089     else
00090     {
00091       detector.push_back( aSD );
00092     }
00093   }
00094 }
00095 
00096 G4SDStructure* G4SDStructure::FindSubDirectory(G4String subD)
00097 {
00098   for( size_t i=0; i<structure.size(); i++ )
00099   {
00100     if( subD == structure[i]->dirName ) return structure[i];
00101   } 
00102   return 0;
00103 }
00104 
00105 G4VSensitiveDetector* G4SDStructure::GetSD(G4String aSDName)
00106 {
00107   for( size_t i=0; i<detector.size(); i++ )
00108   {
00109     G4VSensitiveDetector* tgtSD = detector[i];
00110     if( aSDName == tgtSD->GetName() ) return tgtSD;
00111   }
00112   return 0;
00113 }
00114 
00115 G4String G4SDStructure::ExtractDirName(G4String aName)
00116 {
00117   G4String subD = aName;
00118   G4int i = aName.first('/');
00119   if( i != G4int(std::string::npos) ) subD.remove(i+1);
00120   return subD;
00121 }
00122 
00123 void G4SDStructure::Activate(G4String aName, G4bool sensitiveFlag)
00124 {
00125   G4String aPath = aName;
00126   aPath.remove(0,pathName.length());
00127   if( aPath.first('/') != G4int(std::string::npos) )
00128   {  // Command is ordered for a subdirectory.
00129     G4String subD = ExtractDirName(aPath);
00130     G4SDStructure* tgtSDS = FindSubDirectory(subD);
00131     if( tgtSDS == 0 )
00132     {  // The subdirectory is not found
00133       G4cout << subD << " is not found in " << pathName << G4endl;
00134     }
00135     else
00136     { 
00137       tgtSDS->Activate(aName,sensitiveFlag);
00138     }
00139   }
00140   else if( aPath.isNull() )
00141   {  // Command is ordered for all detectors in this directory.
00142     for( size_t i=0; i<detector.size(); i++)
00143     { 
00144       detector[i]->Activate(sensitiveFlag);
00145     }
00146     for( size_t j=0;j<structure.size(); j++)
00147     {
00148       structure[j]->Activate(G4String("/"),sensitiveFlag);
00149     }
00150   }
00151   else
00152   {  // Command is ordered to a particular detector.
00153     G4VSensitiveDetector* tgtSD = GetSD(aPath);
00154     if( tgtSD == 0 )
00155     {  // The detector is not found.
00156       G4cout << aPath << " is not found in " << pathName << G4endl;
00157     }
00158     else
00159     {
00160       tgtSD->Activate(sensitiveFlag);
00161     }
00162   }
00163 }
00164 
00165 G4VSensitiveDetector* G4SDStructure::FindSensitiveDetector(G4String aName, G4bool warning)
00166 {
00167   G4String aPath = aName;
00168   aPath.remove(0,pathName.length());
00169   if( aPath.first('/') != G4int(std::string::npos) )
00170   { // SD exists in sub-directory
00171     G4String subD = ExtractDirName(aPath);
00172     G4SDStructure* tgtSDS = FindSubDirectory(subD);
00173     if( tgtSDS == 0 )
00174     {  // The subdirectory is not found
00175       if (warning)
00176         G4cout << subD << " is not found in " << pathName << G4endl;
00177       return 0;
00178     }
00179     else
00180     {
00181       return tgtSDS->FindSensitiveDetector(aName);
00182     }
00183   }
00184   else
00185   { // SD must exist in this directory
00186     G4VSensitiveDetector* tgtSD = GetSD(aPath);
00187     if( tgtSD == 0 )
00188     {  // The detector is not found.
00189       if (warning)
00190         G4cout << aPath << " is not found in " << pathName << G4endl;
00191     }
00192     return tgtSD;
00193   }
00194 }
00195 
00196 void G4SDStructure::Initialize(G4HCofThisEvent*HCE)
00197 {
00198   size_t i;
00199   // Broadcast to subdirectories.
00200   for( i=0; i<structure.size(); i++ )
00201   {
00202     structure[i]->Initialize(HCE);
00203   }
00204   // Initialize all detectors in this directory.
00205   for( i=0; i<detector.size(); i++ )
00206   {
00207     if(detector[i]->isActive()) detector[i]->Initialize(HCE);
00208   }
00209 }
00210 
00211 void G4SDStructure::Terminate(G4HCofThisEvent*HCE)
00212 {
00213   size_t i;
00214   // Broadcast to subdirectories.
00215   for( i=0; i<structure.size(); i++ )
00216   {
00217     structure[i]->Terminate(HCE);
00218   }
00219   // Initialize all detectors in this directory.
00220   for( i=0; i<detector.size(); i++ )
00221   {
00222     if(detector[i]->isActive()) detector[i]->EndOfEvent(HCE);
00223   }
00224 }
00225 
00226 void G4SDStructure::ListTree()
00227 {
00228   G4cout << pathName << G4endl;
00229   for(size_t i=0; i<detector.size(); i++)
00230   {
00231     G4VSensitiveDetector* sd = detector[i];
00232     G4cout << pathName << sd->GetName();
00233     if( sd->isActive() )
00234     { G4cout << "   *** Active "; }
00235     else
00236     { G4cout << "   XXX Inactive "; }
00237     G4cout << G4endl;
00238   }
00239   for(size_t j=0; j<structure.size(); j++)
00240   { structure[j]->ListTree(); }
00241 }
00242         
00243 

Generated on Mon May 27 17:49:49 2013 for Geant4 by  doxygen 1.4.7