Commit 1e20fe607a7a266a04fd158e963901798d8d0142

Authored by Jürgen Knödlseder
1 parent 4825ce61

Make compliant to coding standards and update version number to 00-06-00.

Made code compliant to coding standards and added more text for comments.

Updated also the version number to 00-06-00.
src/ctobssim/ctobssim.cpp
@@ -264,26 +264,32 @@ void ctobssim::run(void) @@ -264,26 +264,32 @@ void ctobssim::run(void)
264 // Initialise counters 264 // Initialise counters
265 int n_observations = 0; 265 int n_observations = 0;
266 266
  267 + // From here on the code can be parallelized if OpenMP support
  268 + // is enabled. The code in the following block corresponds to the
  269 + // code that will be executed in each thread
267 #pragma omp parallel 270 #pragma omp parallel
268 { 271 {
269 - //each thread has his own variable; 272 + // Each thread will have it's own logger to avoid conflicts
270 GLog wrklog; 273 GLog wrklog;
271 274
272 - //Copy configuration 275 + // Copy configuration from application logger to thread logger
273 wrklog.date(log.date()); 276 wrklog.date(log.date());
274 wrklog.name(log.name()); 277 wrklog.name(log.name());
275 278
276 - // Set a big value to block the flush 279 + // Set a big value to avoid flushing
277 wrklog.max_size(10000000); 280 wrklog.max_size(10000000);
278 281
  282 + // Loop over all observation in the container. If OpenMP support
  283 + // is enabled, this looped will be parallelized.
279 #pragma omp for 284 #pragma omp for
280 - // Loop over all observation in the container  
281 for (int i = 0; i < m_obs.size(); ++i) { 285 for (int i = 0; i < m_obs.size(); ++i) {
  286 +
282 // Get CTA observation 287 // Get CTA observation
283 GCTAObservation* obs = dynamic_cast<GCTAObservation*>(&m_obs[i]); 288 GCTAObservation* obs = dynamic_cast<GCTAObservation*>(&m_obs[i]);
284 - 289 +
285 // Continue only if observation is a CTA observation 290 // Continue only if observation is a CTA observation
286 if (obs != NULL) { 291 if (obs != NULL) {
  292 +
287 // Write header for observation 293 // Write header for observation
288 if (logTerse()) { 294 if (logTerse()) {
289 if (obs->name().length() > 1) { 295 if (obs->name().length() > 1) {
@@ -293,23 +299,25 @@ void ctobssim::run(void) @@ -293,23 +299,25 @@ void ctobssim::run(void)
293 wrklog.header3("Observation"); 299 wrklog.header3("Observation");
294 } 300 }
295 } 301 }
  302 +
296 // Increment counter 303 // Increment counter
297 n_observations++; 304 n_observations++;
298 305
299 // Simulate source events 306 // Simulate source events
300 - simulate_source(obs, m_obs.models(),m_rans[i],&wrklog); 307 + simulate_source(obs, m_obs.models(), m_rans[i], &wrklog);
301 308
302 // Simulate source events 309 // Simulate source events
303 - simulate_background(obs, m_obs.models(),m_rans[i],&wrklog); 310 + simulate_background(obs, m_obs.models(), m_rans[i], &wrklog);
304 311
305 } // endif: CTA observation found 312 } // endif: CTA observation found
306 313
307 } // endfor: looped over observations 314 } // endfor: looped over observations
308 315
309 - // Add wrklog to log 316 + // At the end, the content of the thread logger is added to
  317 + // the application logger
310 #pragma omp critical (log) 318 #pragma omp critical (log)
311 { 319 {
312 - log<<wrklog; 320 + log << wrklog;
313 } 321 }
314 322
315 } // end pragma omp parallel 323 } // end pragma omp parallel
@@ -436,22 +444,33 @@ void ctobssim::get_parameters(void) @@ -436,22 +444,33 @@ void ctobssim::get_parameters(void)
436 m_prefix = (*this)["prefix"].string(); 444 m_prefix = (*this)["prefix"].string();
437 } 445 }
438 446
439 - // Initialise random number generator  
440 - //m_ran.seed(m_seed);  
441 - 447 + // Initialise random number generators. We initialise here one random
  448 + // number generator per observation so that each observation will
  449 + // get it's own random number generator. This will lead to identical
  450 + // results independently of code parallelization with OpenMP. The
  451 + // seeds for all random number generators are derived randomly but
  452 + // fully deterministacally from the seed parameter, so that a given
  453 + // seed parameter leads always to the same set of simulated events, and
  454 + // this independently of parallelization.
  455 +
  456 + // Get a random number generator for seed determination
442 GRan master(m_seed); 457 GRan master(m_seed);
443 - std::vector<unsigned long long int> seeds;  
444 458
445 - bool repeat = false; 459 + // Allocate vector of random number generator seeds
  460 + std::vector<unsigned long long int> seeds;
446 461
447 - // Loop over all observation in the container 462 + // Loop over all observations in the container
448 for (int i = 0; i < m_obs.size(); ++i) { 463 for (int i = 0; i < m_obs.size(); ++i) {
  464 +
  465 + // Allocate new seed value
449 unsigned long long int new_seed; 466 unsigned long long int new_seed;
450 - 467 +
  468 + // Determine new seed value. We make sure that the new seed
  469 + // value has not been used already for another observation.
  470 + bool repeat = false;
451 do { 471 do {
452 - // Each seed will be unique  
453 new_seed = (unsigned long long int)(master.int64() * 1.0e2); 472 new_seed = (unsigned long long int)(master.int64() * 1.0e2);
454 - repeat = false; 473 + repeat = false;
455 for (int j = 0; j < seeds.size(); ++j) { 474 for (int j = 0; j < seeds.size(); ++j) {
456 if (new_seed == seeds[j]) { 475 if (new_seed == seeds[j]) {
457 repeat = true; 476 repeat = true;
@@ -460,13 +479,14 @@ void ctobssim::get_parameters(void) @@ -460,13 +479,14 @@ void ctobssim::get_parameters(void)
460 } 479 }
461 } while(repeat); 480 } while(repeat);
462 481
463 - //Add the seed to the vector 482 + // Add the seed to the vector for bookkeeping
464 seeds.push_back(new_seed); 483 seeds.push_back(new_seed);
465 484
466 - //Use the seed to create a GRan for the observation number i 485 + // Use the seed to create a random number generator for the
  486 + // actual observation
467 m_rans.push_back(GRan(new_seed)); 487 m_rans.push_back(GRan(new_seed));
468 - }  
469 - 488 +
  489 + } // endfor: looped over observations
470 490
471 // Return 491 // Return
472 return; 492 return;
@@ -556,6 +576,8 @@ void ctobssim::set_list(GCTAObservation* obs) @@ -556,6 +576,8 @@ void ctobssim::set_list(GCTAObservation* obs)
556 * 576 *
557 * @param[in] obs Pointer on CTA observation. 577 * @param[in] obs Pointer on CTA observation.
558 * @param[in] photons Photon list. 578 * @param[in] photons Photon list.
  579 + * @param[in] ran Random number generator.
  580 + * @param[in] wrklog Pointer to logger.
559 * 581 *
560 * @exception GCTAException::no_response 582 * @exception GCTAException::no_response
561 * No valid response found in CTA observation 583 * No valid response found in CTA observation
@@ -571,13 +593,15 @@ void ctobssim::set_list(GCTAObservation* obs) @@ -571,13 +593,15 @@ void ctobssim::set_list(GCTAObservation* obs)
571 * they can be scatter within the ROI and energy interval by the 593 * they can be scatter within the ROI and energy interval by the
572 * PSF and energy dispersion. 594 * PSF and energy dispersion.
573 ***************************************************************************/ 595 ***************************************************************************/
574 -void ctobssim::simulate_source(GCTAObservation* obs, const GModels& models, GRan& ran, GLog* wrklog) 596 +void ctobssim::simulate_source(GCTAObservation* obs, const GModels& models,
  597 + GRan& ran, GLog* wrklog)
575 { 598 {
576 // Continue only if observation pointer is valid 599 // Continue only if observation pointer is valid
577 if (obs != NULL) { 600 if (obs != NULL) {
578 -  
579 - if(wrklog==NULL){  
580 - wrklog=&log; 601 +
  602 + // If no logger is specified then use the default logger
  603 + if (wrklog == NULL) {
  604 + wrklog = &log;
581 } 605 }
582 606
583 // Get pointer on CTA response. Throw an exception if the response 607 // Get pointer on CTA response. Throw an exception if the response
@@ -789,19 +813,23 @@ void ctobssim::simulate_source(GCTAObservation* obs, const GModels&amp; models, GRan @@ -789,19 +813,23 @@ void ctobssim::simulate_source(GCTAObservation* obs, const GModels&amp; models, GRan
789 * 813 *
790 * @param[in] obs Pointer on CTA observation. 814 * @param[in] obs Pointer on CTA observation.
791 * @param[in] models Models. 815 * @param[in] models Models.
  816 + * @param[in] ran Random number generator.
  817 + * @param[in] wrklog Pointer to logger.
792 * 818 *
793 * Simulate background events from models. The events are stored as event 819 * Simulate background events from models. The events are stored as event
794 * list in the observation. 820 * list in the observation.
795 * 821 *
796 * This method does nothing if the observation pointer is NULL. 822 * This method does nothing if the observation pointer is NULL.
797 ***************************************************************************/ 823 ***************************************************************************/
798 -void ctobssim::simulate_background(GCTAObservation* obs, const GModels& models, GRan& ran, GLog* wrklog) 824 +void ctobssim::simulate_background(GCTAObservation* obs, const GModels& models,
  825 + GRan& ran, GLog* wrklog)
799 { 826 {
800 // Continue only if observation pointer is valid 827 // Continue only if observation pointer is valid
801 if (obs != NULL) { 828 if (obs != NULL) {
802 829
803 - if(wrklog==NULL){  
804 - wrklog=&log; 830 + // If no logger is specified then use the default logger
  831 + if (wrklog == NULL) {
  832 + wrklog = &log;
805 } 833 }
806 834
807 // Make sure that the observation holds a CTA event list. If this 835 // Make sure that the observation holds a CTA event list. If this
@@ -892,6 +920,7 @@ void ctobssim::init_members(void) @@ -892,6 +920,7 @@ void ctobssim::init_members(void)
892 m_deadc = 1.0; 920 m_deadc = 1.0;
893 921
894 // Initialise protected members 922 // Initialise protected members
  923 + m_rans.clear();
895 m_obs.clear(); 924 m_obs.clear();
896 m_use_xml = false; 925 m_use_xml = false;
897 m_read_ahead = false; 926 m_read_ahead = false;
@@ -934,7 +963,7 @@ void ctobssim::copy_members(const ctobssim&amp; app) @@ -934,7 +963,7 @@ void ctobssim::copy_members(const ctobssim&amp; app)
934 // Copy protected members 963 // Copy protected members
935 m_area = app.m_area; 964 m_area = app.m_area;
936 m_time_max = app.m_time_max; 965 m_time_max = app.m_time_max;
937 - m_rans = app.m_rans; 966 + m_rans = app.m_rans;
938 m_obs = app.m_obs; 967 m_obs = app.m_obs;
939 m_use_xml = app.m_use_xml; 968 m_use_xml = app.m_use_xml;
940 m_read_ahead = app.m_read_ahead; 969 m_read_ahead = app.m_read_ahead;
src/ctobssim/ctobssim.hpp
@@ -33,7 +33,7 @@ @@ -33,7 +33,7 @@
33 33
34 /* __Definitions _________________________________________________________ */ 34 /* __Definitions _________________________________________________________ */
35 #define CTOBSSIM_NAME "ctobssim" 35 #define CTOBSSIM_NAME "ctobssim"
36 -#define CTOBSSIM_VERSION "00-05-00" 36 +#define CTOBSSIM_VERSION "00-06-00"
37 37
38 38
39 /***********************************************************************//** 39 /***********************************************************************//**
@@ -95,13 +95,12 @@ protected: @@ -95,13 +95,12 @@ protected:
95 double m_deadc; //!< Average deadtime correction 95 double m_deadc; //!< Average deadtime correction
96 96
97 // Protected members 97 // Protected members
98 - double m_area; //!< Surface area for simulation (cm2)  
99 - GTime m_time_max; //!< Maximum length of time slice (sec)  
100 - //GRan m_ran; //!< Random number generator  
101 - std::vector<GRan> m_rans; //!< Random number generators  
102 - GObservations m_obs; //!< Observation container  
103 - bool m_use_xml; //!< Use XML file instead of FITS file  
104 - bool m_read_ahead; //!< Read ahead parameters 98 + double m_area; //!< Surface area for simulation (cm2)
  99 + GTime m_time_max; //!< Maximum length of time slice (sec)
  100 + std::vector<GRan> m_rans; //!< Random number generators
  101 + GObservations m_obs; //!< Observation container
  102 + bool m_use_xml; //!< Use XML file instead of FITS file
  103 + bool m_read_ahead; //!< Read ahead parameters
105 }; 104 };
106 105
107 #endif /* CTOBSSIM_HPP */ 106 #endif /* CTOBSSIM_HPP */
src/ctobssim/ctobssim.par
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 # ------------------------------------------------------------------------- # 5 # ------------------------------------------------------------------------- #
6 # # 6 # #
7 # File: ctobssim.par # 7 # File: ctobssim.par #
8 -# Version: 00-05-00 # 8 +# Version: 00-06-00 #
9 # # 9 # #
10 # Author: Jurgen Knodlseder # 10 # Author: Jurgen Knodlseder #
11 # Jurgen.Knodlseder@irap.omp.eu # 11 # Jurgen.Knodlseder@irap.omp.eu #