~vpec/maus/tof_calib_read

« back to all changes in this revision

Viewing changes to src/common_cpp/Simulation/MAUSGeant4Manager.cc

merged in from maus/merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
#include "Geant4/G4StateManager.hh"
22
22
#include "Geant4/G4ApplicationState.hh"
 
23
#include "Geant4/G4Region.hh"
 
24
#include "Geant4/G4RegionStore.hh"
 
25
#include "Geant4/G4UserLimits.hh"
23
26
 
24
27
#include "src/legacy/Interface/Squeak.hh"
25
28
 
68
71
    SetVisManager();
69
72
    _runManager = new G4RunManager;
70
73
    Json::Value* cards = Globals::GetInstance()->GetConfigurationCards();
 
74
 
 
75
    _keThreshold = JsonWrapper::GetProperty(*cards, "kinetic_energy_threshold",
 
76
                                            JsonWrapper::realValue).asDouble();
 
77
    _trackMax = JsonWrapper::GetProperty(*cards, "max_track_length",
 
78
                                         JsonWrapper::realValue).asDouble();
 
79
    _timeMax = JsonWrapper::GetProperty(*cards, "max_track_time",
 
80
                                        JsonWrapper::realValue).asDouble();
 
81
    _stepMax = JsonWrapper::GetProperty(*cards, "max_step_length",
 
82
                                        JsonWrapper::realValue).asDouble();
 
83
 
71
84
    // Create the gdml parser object
72
85
 
73
86
    std::string gdmlGeometry = "";
111
124
    // now set up full geometry
112
125
    SetMiceModules(*Globals::GetInstance()->GetMonteCarloMiceModules());
113
126
    if (usegdml) {
114
 
      SetAuxInformation(*Globals::GetInstance()->GetMonteCarloMiceModules());
 
127
      SetVolumeInformation(*Globals::GetInstance()->GetMonteCarloMiceModules(),
 
128
                           _parser.GetWorldVolume()->GetLogicalVolume());
115
129
    }
116
130
}
117
131
 
224
238
  return _detector->GetField();
225
239
}
226
240
 
227
 
void MAUSGeant4Manager::SetAuxInformation(MiceModule& module) {
 
241
void MAUSGeant4Manager::SetVolumeInformation(MiceModule& module, G4LogicalVolume* base) {
 
242
 
 
243
  // G4VPhysicalVolume* world = _parser.GetWorldVolume();
 
244
  G4int nDaughters = base->GetNoDaughters();
 
245
  for ( int i = 0; i < nDaughters; i++ ) {
 
246
    G4VPhysicalVolume* daughter = base->GetDaughter(i);
 
247
    G4LogicalVolume* myvol = daughter->GetLogicalVolume();
 
248
    // first check if there are auxiliary objects
 
249
    const G4GDMLAuxListType auxlist = _parser.GetVolumeAuxiliaryInformation(myvol);
 
250
 
 
251
    // Squeak::mout(Squeak::info) << "Found volume " << myvol->GetName()
 
252
    // << " with "<< auxlist.size() <<" auxiliary elements." << std::endl;
 
253
    if (auxlist.size() > 0) {
 
254
      // Set auxiliary information
 
255
      SetAuxInformation(module, myvol, auxlist);
 
256
    }
 
257
    //   else {
 
258
    //   _detector->GetUserLimits().push_back(new G4UserLimits(_stepMax, _trackMax,
 
259
    //   _timeMax, _keThreshold));
 
260
    //   myvol->SetUserLimits(_detector->GetUserLimits().back());
 
261
    //   }
 
262
    if ( myvol->GetNoDaughters() > 0 ) {
 
263
      // Consider adding information to the daughter volumes
 
264
      SetVolumeInformation(module, myvol);
 
265
    }
 
266
  }
 
267
}
 
268
 
 
269
void MAUSGeant4Manager::SetSteppingLimits(G4LogicalVolume* myvol) {
 
270
 
 
271
  double stepMax = _stepMax;
 
272
  double timeMax = _timeMax;
 
273
  double trackMax = _trackMax;
 
274
  double keThreshold = _keThreshold;
 
275
  double red = 1.;
 
276
  double green = 1.;
 
277
  double blue = 1.;
 
278
  bool vis = true;
 
279
 
 
280
  // Squeak::mout(Squeak::info) << "Defining user limits for " << myvol->GetName() << std::endl;
 
281
  _detector->GetUserLimits().push_back(new G4UserLimits(stepMax, trackMax,
 
282
                                                        timeMax, keThreshold));
 
283
  // Squeak::mout(Squeak::info) << "Setting user limits for " << myvol->GetName() << std::endl;
 
284
  myvol->SetUserLimits(_detector->GetUserLimits().back());
 
285
  if (vis)
 
286
    _detector->GetVisAttributes().push_back(new G4VisAttributes(G4Color(red, green, blue)));
 
287
  else
 
288
    _detector->GetVisAttributes().push_back(new G4VisAttributes(false));
 
289
  myvol->SetVisAttributes(_detector->GetVisAttributes().back());
 
290
  // Squeak::mout(Squeak::info) << "Limits set for " << myvol->GetName() << std::endl;
 
291
}
 
292
 
 
293
void MAUSGeant4Manager::SetAuxInformation(MiceModule& module, G4LogicalVolume* myvol,
 
294
                                          const G4GDMLAuxListType auxlist) {
228
295
  // Establish sensitive detectors using the SDmanager
229
296
  // Get the map of the auxiliary information from the parser
 
297
  /*
230
298
  const G4GDMLAuxMapType* auxmap = _parser.GetAuxMap();
231
299
    Squeak::mout(Squeak::info) << "Found " << auxmap->size()
232
300
                             << " volume(s) with auxiliary information.\n\n";
233
 
 
 
301
  */
 
302
  double stepMax = _stepMax;
 
303
  double timeMax = _timeMax;
 
304
  double trackMax = _trackMax;
 
305
  double keThreshold = _keThreshold;
 
306
  double red = 1.;
 
307
  double green = 1.;
 
308
  double blue = 1.;
 
309
  bool vis = true;
 
310
  /*
234
311
  for (G4GDMLAuxMapType::const_iterator iter = auxmap->begin();
235
312
      iter != auxmap->end(); iter++) {
236
313
    // Construct a mice module containing the auxilliary information
237
314
    G4LogicalVolume* myvol = (*iter).first;
238
 
    // Want to know, specifically if there is a sensitive detector in this object
239
 
    for (G4GDMLAuxListType::const_iterator vit = (*iter).second.begin();
240
 
         vit != (*iter).second.end(); vit++) {
 
315
    Squeak::mout(Squeak::info) << "Checking aux map of volume "
 
316
                               << myvol->GetName() << std::endl;
 
317
  */
 
318
  // Define sensitive detectors etc.
 
319
  bool sensdet = false;
 
320
  std::string sensdetname = "";
 
321
  G4GDMLAuxListType::const_iterator vit = auxlist.begin();
 
322
  // Want to know, specifically, if there is a sensitive detector in this object
 
323
  // but there will be other information of interest
 
324
  // for (int i=0; i < auxlist.size(); i++){
 
325
  do {
 
326
    try {
241
327
      if ((*vit).type.contains("SensitiveDetector")) {
242
 
        // Find the module corresponding to the volume name
243
 
        std::vector<const MiceModule*> mods =
244
 
          module.findModulesByPropertyString((*vit).type, (*vit).value);
245
 
        // Squeak::mout(Squeak::info)<<"Search for detector "<<(*vit).value
246
 
        //                        <<" with name "<<myvol->GetName()<<": "
247
 
        //                        <<mods.size()<<" candidates\n";
248
 
        for (unsigned i = 0; i < mods.size(); i++) {
249
 
          // This is kind of a double check now to make
250
 
          // sure that this is the same object.
251
 
 
252
 
          if (mods.at(i)->name() == myvol->GetName()) {
253
 
             // propertyExists("SensitiveDetector","PropertyString")){
254
 
            // Make a copy of the module to remove the const cast
255
 
            MiceModule* tempmod = MiceModule::deepCopy(*mods[i], false);
256
 
            // tempmod->printThis(Squeak::mout(Squeak::info));
257
 
            _detector->SetUserLimits(myvol, tempmod);
258
 
            _detector->SetVisAttributes(myvol, tempmod);
259
 
            _detector->BuildSensitiveDetector(myvol, tempmod);
260
 
            _detector->AddToRegion(myvol, tempmod);
261
 
            // Now loop over all daughters to add them to the sensitive volumes
262
 
            if ((*vit).value == "SciFi" || (*vit).value == "KL") {
263
 
              if (myvol->GetNoDaughters() > 0) {
264
 
                SetDaughterSensitiveDetectors(myvol);
265
 
              }
266
 
            }
267
 
          }
268
 
        }
269
 
      }
270
 
    }
271
 
  }
272
 
}
273
 
 
 
328
        sensdet = true;
 
329
        sensdetname = (*vit).value;
 
330
      } else if ((*vit).type.contains("G4StepMax")) {
 
331
        stepMax  = atof((*vit).value.c_str());
 
332
        //
 
333
      } else if ((*vit).type.contains("G4TrackMax")) {
 
334
        trackMax = atof((*vit).value.c_str());
 
335
      } else if ((*vit).type.contains("G4TimeMax")) {
 
336
        timeMax  = atof((*vit).value.c_str());
 
337
      } else if ((*vit).type.contains("G4KinMin")) {
 
338
        keThreshold = atof((*vit).value.c_str());
 
339
      } else if ((*vit).type.contains("Region")) {
 
340
        std::string name = (*vit).value;
 
341
        G4RegionStore* store = G4RegionStore::GetInstance();
 
342
        if (store->GetRegion(name) == NULL) {
 
343
          new G4Region(name);
 
344
          _detector->GetRegions().push_back(name);
 
345
        }
 
346
        G4Region* region = store->GetRegion(name);
 
347
        if (region == NULL) {
 
348
          throw MAUS::Exception(Exception::recoverable,
 
349
                                "Failed to make region",
 
350
                                "MAUSgeant4Manager::SetAuxInformation");
 
351
        }
 
352
        region->AddRootLogicalVolume(myvol);
 
353
      } else if ((*vit).type.contains("Invisible")) {
 
354
        vis = false;
 
355
      } else if ((*vit).type.contains("RedColor")) {
 
356
        red = atof((*vit).value.c_str());
 
357
      } else if ((*vit).type.contains("GreenColor")) {
 
358
        green = atof((*vit).value.c_str());
 
359
      } else if ((*vit).type.contains("BlueColor")) {
 
360
        blue = atof((*vit).value.c_str());
 
361
      } else {
 
362
        // Don't really know what to do with this.
 
363
        // Do nothing if selection is not otherwise known.
 
364
      }
 
365
    } catch (...) {
 
366
      continue;
 
367
    }
 
368
    // Squeak::mout(Squeak::info) << "Found " << (*vit).type << " with value "
 
369
    //                 << (*vit).value << " in object "
 
370
    //                         << myvol->GetName() << "\n";
 
371
    vit++;
 
372
  } while (vit != auxlist.end());
 
373
 
 
374
  if (sensdet) {
 
375
    DefineSensitiveDetector(module, myvol, sensdetname);
 
376
  }
 
377
  _detector->GetUserLimits().push_back(new G4UserLimits(stepMax, trackMax,
 
378
                                                        timeMax, keThreshold));
 
379
  // Squeak::mout(Squeak::info) << "User limits set for " << myvol->GetName()
 
380
  //                         << " with max step " << stepMax << std::endl;
 
381
  myvol->SetUserLimits(_detector->GetUserLimits().back());
 
382
  // if (myvol->GetNoDaughters() > 0) {
 
383
  //   SetDaughterUserLimits(myvol);
 
384
  // }
 
385
  /*
 
386
  if (vis)
 
387
    _detector->GetVisAttributes().push_back(new G4VisAttributes(G4Color(red, green, blue)));
 
388
  else
 
389
    _detector->GetVisAttributes().push_back(new G4VisAttributes(false));
 
390
  
 
391
  myvol->SetVisAttributes(_detector->GetVisAttributes().back());
 
392
  */
 
393
  // Squeak::mout(Squeak::info) << "Attributes set for volume "
 
394
  // << myvol->GetName() << std::endl;
 
395
}
 
396
 
 
397
 
 
398
void MAUSGeant4Manager::DefineSensitiveDetector(MiceModule& module, G4LogicalVolume* myvol,
 
399
                                                std::string sensdetname) {
 
400
  // Find the module corresponding to the volume name
 
401
  std::vector<const MiceModule*> mods =
 
402
    module.findModulesByPropertyString("SensitiveDetector", sensdetname);
 
403
  // Squeak::mout(Squeak::info) << "Search for detector " << sensdetname
 
404
  //                         << " with name " << myvol->GetName() << ": "
 
405
  //                 << mods.size() << " candidates\n";
 
406
  bool moduleFound = false;
 
407
  for ( unsigned i = 0; i < mods.size(); i++ ) {
 
408
    // This is kind of a double check now to make
 
409
    // sure that this is the same object.
 
410
    // Squeak::mout(Squeak::info) << "Check module " << i << " named "
 
411
    // << mods.at(i)->name() << std::endl;
 
412
    if (mods.at(i)->name() == myvol->GetName()) {
 
413
      moduleFound = true;
 
414
      // propertyExists("SensitiveDetector","PropertyString")){
 
415
      // Make a copy of the module to remove the const cast
 
416
      MiceModule* tempmod = MiceModule::deepCopy(*mods[i], false);
 
417
      // tempmod->printThis(Squeak::mout(Squeak::info));
 
418
      _detector->SetUserLimits(myvol, tempmod);
 
419
      _detector->SetVisAttributes(myvol, tempmod);
 
420
      _detector->BuildSensitiveDetector(myvol, tempmod);
 
421
      _detector->AddToRegion(myvol, tempmod);
 
422
      // Now loop over all daughters to add them to the sensitive volumes
 
423
      if (sensdetname == "SciFi" || sensdetname == "KL") {
 
424
        if (myvol->GetNoDaughters() > 0) {
 
425
          SetDaughterSensitiveDetectors(myvol);
 
426
        }
 
427
      }
 
428
    }
 
429
    // if (moduleFound)
 
430
    // Squeak::mout(Squeak::info) << "Module for " << myvol->GetName()
 
431
    //                   << " found." << std::endl;
 
432
  }
 
433
}
274
434
void MAUSGeant4Manager::SetDaughterSensitiveDetectors(G4LogicalVolume* logic) {
275
435
  // std::cout << "Adding " << logic->GetNoDaughters()
276
 
  // << " to sensitive detector in " << logic->GetName() << std::endl;
 
436
  //        << " to sensitive detector in " << logic->GetName() << std::endl;
277
437
 
278
438
  for (G4int i = 0; i < logic->GetNoDaughters(); i++) {
279
439
    logic->GetDaughter(i)->GetLogicalVolume()->