00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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 {
00070
00071 G4String subD = ExtractDirName( remainingPath );
00072 G4SDStructure* tgtSDS = FindSubDirectory(subD);
00073 if( tgtSDS == 0 )
00074 {
00075 subD.prepend(pathName);
00076 tgtSDS = new G4SDStructure(subD);
00077 structure.push_back( tgtSDS );
00078 }
00079 tgtSDS->AddNewDetector(aSD,treeStructure);
00080 }
00081 else
00082 {
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 {
00129 G4String subD = ExtractDirName(aPath);
00130 G4SDStructure* tgtSDS = FindSubDirectory(subD);
00131 if( tgtSDS == 0 )
00132 {
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 {
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 {
00153 G4VSensitiveDetector* tgtSD = GetSD(aPath);
00154 if( tgtSD == 0 )
00155 {
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 {
00171 G4String subD = ExtractDirName(aPath);
00172 G4SDStructure* tgtSDS = FindSubDirectory(subD);
00173 if( tgtSDS == 0 )
00174 {
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 {
00186 G4VSensitiveDetector* tgtSD = GetSD(aPath);
00187 if( tgtSD == 0 )
00188 {
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
00200 for( i=0; i<structure.size(); i++ )
00201 {
00202 structure[i]->Initialize(HCE);
00203 }
00204
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
00215 for( i=0; i<structure.size(); i++ )
00216 {
00217 structure[i]->Terminate(HCE);
00218 }
00219
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