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
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 {
00071
00072 G4String subD = ExtractDirName( remainingPath );
00073 G4SDStructure* tgtSDS = FindSubDirectory(subD);
00074 if( tgtSDS == 0 )
00075 {
00076 subD.prepend(pathName);
00077 tgtSDS = new G4SDStructure(subD);
00078 structure.push_back( tgtSDS );
00079 }
00080 tgtSDS->AddNewDetector(aSD,treeStructure);
00081 }
00082 else
00083 {
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 {
00130 G4String subD = ExtractDirName(aPath);
00131 G4SDStructure* tgtSDS = FindSubDirectory(subD);
00132 if( tgtSDS == 0 )
00133 {
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 {
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 {
00154 G4VSensitiveDetector* tgtSD = GetSD(aPath);
00155 if( tgtSD == 0 )
00156 {
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 {
00172 G4String subD = ExtractDirName(aPath);
00173 G4SDStructure* tgtSDS = FindSubDirectory(subD);
00174 if( tgtSDS == 0 )
00175 {
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 {
00187 G4VSensitiveDetector* tgtSD = GetSD(aPath);
00188 if( tgtSD == 0 )
00189 {
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
00201 for( i=0; i<structure.size(); i++ )
00202 {
00203 structure[i]->Initialize(HCE);
00204 }
00205
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
00216 for( i=0; i<structure.size(); i++ )
00217 {
00218 structure[i]->Terminate(HCE);
00219 }
00220
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