00001
00002
00003
00004
00005
00006
00007
00008 #include "myglobals.hh"
00009 #include "G4SDManager.hh"
00010 #include "G4DigiManager.hh"
00011
00012 #include <sstream>
00013 #include <fstream>
00014 #include <string.h>
00015 #include <iostream>
00016
00017
00018 #include "MonitorSD.hh"
00019 #include "KillerSD.hh"
00020
00021
00022 #include "SimpleGeometrySvc.hh"
00023
00024 #include "MyVGeometrySvc.hh"
00025 #include "MyVGeometryParameter.hh"
00026
00027 #include "MyDetectorManagerMessenger.hh"
00028 #include "MyDetectorManager.hh"
00029
00030 #include "DEBUG.hh"
00031
00032 MyDetectorManager* MyDetectorManager::fMyDetectorManager = 0;
00033
00034 MyDetectorManager::MyDetectorManager()
00035 :fVerboseLevel(0)
00036 {
00037 if (fMyDetectorManager){
00038 G4Exception("MyDetectorManager::MyDetectorManager()","Run0031",
00039 FatalException, "MyDetectorManager constructed twice.");
00040 }
00041 fMyDetectorManager = this;
00042 fSDman = G4SDManager::GetSDMpointer();
00043 if (!fSDman) DEBUG("Can not find G4SDManager.");
00044 m_MyDetectorManagerMessenger = new MyDetectorManagerMessenger(this);
00045 }
00046
00047 MyDetectorManager::~MyDetectorManager()
00048 {
00049 delete m_MyDetectorManagerMessenger;
00050 }
00051
00052 MyDetectorManager* MyDetectorManager::GetMyDetectorManager(){
00053 if ( !fMyDetectorManager ){
00054 fMyDetectorManager = new MyDetectorManager;
00055 }
00056 return fMyDetectorManager;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 void MyDetectorManager::ReadCard(G4String file_name){
00068
00069 for ( int i = 0; i < fMyVGeometrySvc.size(); i++ ){
00070 if (fMyVGeometrySvc[i]) delete (fMyVGeometrySvc[i]);
00071 }
00072 fMyVGeometrySvc.clear();
00073 fSvcName.clear();
00074
00075
00076 if(file_name[0] != '/'){
00077 G4String dir_name = getenv("CONFIGUREROOT");
00078 if (dir_name[dir_name.size()-1] != '/') dir_name.append("/");
00079 file_name = dir_name + file_name;
00080 }
00081
00082 std::ifstream fin_card(file_name);
00083 if ( !fin_card ){
00084 std::cout<<"Geometry file: "<<file_name<<" is not available!!!"<<std::endl;
00085 G4Exception("MyDetectorManager::ReadCard()","Run0031",
00086 FatalException, "Geometry file is not available.");
00087 }
00088
00089
00090 std::stringstream buf_card;
00091 std::string s_card;
00092 while(getline(fin_card,s_card)){
00093 buf_card.str("");
00094 buf_card.clear();
00095 buf_card<<s_card;
00096 const char* c = s_card.c_str();
00097 int length = strlen(c);
00098 int offset = 0;
00099 for ( ; offset < length; offset++ ){
00100 if ( c[offset] != ' ' ) break;
00101 }
00102 if ( c[offset] == '#' ) continue;
00103 else if ( c[offset] == '/' && c[offset+1] == '/' ) continue;
00104 else if ( length - offset == 0 ) continue;
00105
00106 G4String name, type, file;
00107 MyVGeometrySvc* aGSvc = 0;
00108 buf_card>>name;
00109
00110 if ( name == "VerboseLevel:" ){
00111 buf_card>>fVerboseLevel;
00112 }
00113 else{
00114 buf_card>>type>>file;
00115 if( type == "Simple" ){
00116 aGSvc = new SimpleGeometrySvc( name );
00117 }
00118 else{
00119 std::cout<<"In MyDetectorManager::ReadCard, unsupported GeometrySvc type: "<<type<<"! Will ignore this line!"<<std::endl;
00120 }
00121 }
00122 if (aGSvc){
00123 aGSvc->set_VerboseLevel(fVerboseLevel);
00124
00125 fMyVGeometrySvc.push_back(aGSvc);
00126 fSvcName.push_back(name);
00127 aGSvc->ReadCard(file);
00128 }
00129 }
00130 }
00131
00132
00133
00134
00135 void MyDetectorManager::AddGeo(G4String name, G4String file, G4String type){
00136 MyVGeometrySvc* aGSvc = 0;
00137 if( type == "Simple" ){
00138 aGSvc = new SimpleGeometrySvc( name );
00139 }
00140 else{
00141 std::cout<<"In MyDetectorManager::ReadCard, unsupported GeometrySvc type: "<<type<<"! Will ignore this line!"<<std::endl;
00142 }
00143 if (aGSvc){
00144 aGSvc->set_VerboseLevel(fVerboseLevel);
00145
00146 fMyVGeometrySvc.push_back(aGSvc);
00147 fSvcName.push_back(name);
00148 aGSvc->ReadCard(file);
00149 }
00150 }
00151 void MyDetectorManager::ClearGeo(){
00152 fSvcName.clear();
00153 fMyVGeometrySvc.clear();
00154 printf("MyDetectorManager::ClearGeo()\n");
00155 }
00156
00157
00158
00159
00160
00161 G4VPhysicalVolume* MyDetectorManager::SetGeometry(){
00162 if (fMyVGeometrySvc.size()==0){
00163 AddGeo("default","world_default","Simple");
00164 }
00165 G4VPhysicalVolume* vol_world = (fMyVGeometrySvc[0])->SetGeometry();
00166 for ( int i = 1; i < fMyVGeometrySvc.size(); i++ ){
00167 (fMyVGeometrySvc[i])->SetGeometry();
00168 }
00169 return vol_world;
00170 }
00171
00172
00173
00174
00175
00176 void MyDetectorManager::ReadOutputCard( G4String file_name ){
00177 for ( int i = 0; i < fSDList.size(); i++ ){
00178 fSDList[i]->ReadOutputCard(file_name);
00179 }
00180 }
00181
00182
00183
00184 void MyDetectorManager::SetBranch(){
00185 for ( int i = 0; i < fSDList.size(); i++ ){
00186 fSDList[i]->SetBranch();
00187 }
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 G4VSensitiveDetector* MyDetectorManager::GetSD(G4String VolName, G4String SDName, MyVGeometrySvc* pointer){
00207 MYDETM_LINEINFO()
00208 MyVGeometryParameter* pPara = pointer->get_GeometryParameter();
00209
00210
00211 int index = -1;
00212 for ( int i = 0; i < fMyVGeometrySvc.size(); i++ ){
00213 if ( (void*) pointer == (void*) fMyVGeometrySvc[i]) index = i;
00214 }
00215 if (index == -1){
00216 std::cout<<"WRONG!!! unrecorded MyVGeometrySvc pointer!!!"<<std::endl;
00217 G4Exception("MyDetectorManager::GetSD()","Run0031",
00218 FatalException, "unrecorded MyVGeometrySvc pointer.");
00219 }
00220
00221
00222 G4String newVolName;
00223 G4String newSDName;
00224 size_t sLast = SDName.last('/');
00225 if(sLast==std::string::npos){
00226 newVolName = VolName;
00227 newSDName = SDName;
00228 }
00229 else{
00230 newVolName = SDName.substr(0,sLast);
00231 newSDName = SDName.substr(sLast+1,SDName.length()-sLast-1);
00232 }
00233
00234
00235 MYDETM_LINEINFO()
00236 MySD* aMySD = 0;
00237 for ( int i = 0; i < fSDList.size(); i++ ){
00238 if ( fSDName[i] == newSDName ){
00239 if ( fVolName[i] == newVolName ){
00240 aMySD = fSDList[i];
00241 if ( !aMySD ) DEBUG("An element of fSDList is zero!");
00242
00243 aMySD->set_GeometryParameter(pPara);
00244 break;
00245 }
00246 }
00247 }
00248 MYDETM_LINEINFO()
00249
00250
00251 G4VSensitiveDetector* aG4SD = 0;
00252 if ( !aMySD ){
00253 G4String FullSDName = newVolName + "/" + newSDName;
00254 if ( newSDName == "KillerSD" ){
00255 aG4SD = new KillerSD( FullSDName, pPara );
00256 fSDman->AddNewDetector( aG4SD );
00257 }
00258 else if ( newSDName == "MonitorSD" ){
00259 aG4SD = new MonitorSD( FullSDName, pPara );
00260 fSDman->AddNewDetector( aG4SD );
00261 }
00262 else if ( newSDName != "none" ) {
00263 DEBUG("Sensitive detector type " + newSDName + " is not supported yet!!!");
00264 }
00265
00266 if ( aG4SD ){
00267 MySD* aSD = dynamic_cast<MySD*> (aG4SD);
00268 aSD->SetVerbose(fVerboseLevel);
00269 if ( aSD == NULL ){
00270 G4String content = "Cannot convert "+newSDName+" from G4VSensitiveDetector* to MySD* at construction";
00271 DEBUG(content);
00272 }
00273 fSDList.push_back(aSD);
00274 fVolName.push_back(newVolName);
00275 fSDName.push_back(newSDName);
00276 }
00277 }
00278 MYDETM_LINEINFO()
00279 else {
00280 aMySD->SetVerbose(fVerboseLevel);
00281 aG4SD = dynamic_cast<G4VSensitiveDetector*> (aMySD);
00282 if ( aG4SD == NULL ){
00283 G4String content = "Cannot convert "+newSDName+" from G4VSensitiveDetector* to MySD* at construction";
00284 DEBUG(content);
00285 }
00286 }
00287 MYDETM_LINEINFO()
00288
00289 return aG4SD;
00290 }
00291
00292 G4VSensitiveDetector* MyDetectorManager::GetSD(G4String VolName, G4String SDName){
00293
00294
00295 G4String newVolName;
00296 G4String newSDName;
00297 size_t sLast = SDName.last('/');
00298 if(sLast==std::string::npos){
00299 newVolName = VolName;
00300 newSDName = SDName;
00301 }
00302 else{
00303 newVolName = SDName.substr(0,sLast);
00304 newSDName = SDName.substr(sLast+1,SDName.length()-sLast-1);
00305 }
00306
00307
00308 MySD* aMySD = 0;
00309
00310 for ( int i = 0; i < fSDList.size(); i++ ){
00311
00312 if ( fSDName[i] == newSDName ){
00313
00314 if ( fVolName[i] == newVolName ){
00315
00316 aMySD = fSDList[i];
00317 if ( !aMySD ) DEBUG("An element of fSDList is zero!");
00318 break;
00319 }
00320 }
00321 }
00322
00323
00324 G4VSensitiveDetector* aG4SD = 0;
00325 if ( !aMySD ){
00326 }
00327 else {
00328 aG4SD = dynamic_cast<G4VSensitiveDetector*> (aMySD);
00329 if ( aG4SD == NULL ){
00330 G4String content = "Cannot convert "+newSDName+" from G4VSensitiveDetector* to MySD* at construction";
00331 DEBUG(content);
00332 }
00333 }
00334
00335 return aG4SD;
00336 }
00337
00338
00339
00340
00341 void MyDetectorManager::RegistorDM( G4String DMName ){
00342 if ( DMName != "none" ) fDMName.push_back(DMName);
00343 }
00344
00345
00346
00347
00348 void MyDetectorManager::Digitize(){
00349 for ( int i = 0; i < fDMName.size(); i++ ){
00350 G4String DMName = fDMName[i];
00351 G4DigiManager::GetDMpointer()->Digitize(DMName);
00352 }
00353 }
00354
00355
00356
00357 void MyDetectorManager::DEBUG(G4String content){
00358 G4Exception("MyDetectorManager","Run0031",
00359 FatalException, content);
00360 }