Commit 1e20fe607a7a266a04fd158e963901798d8d0142
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.
Showing
3 changed files
with
67 additions
and
39 deletions
src/ctobssim/ctobssim.cpp
| ... | ... | @@ -264,26 +264,32 @@ void ctobssim::run(void) |
| 264 | 264 | // Initialise counters |
| 265 | 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 | 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 | 273 | GLog wrklog; |
| 271 | 274 | |
| 272 | - //Copy configuration | |
| 275 | + // Copy configuration from application logger to thread logger | |
| 273 | 276 | wrklog.date(log.date()); |
| 274 | 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 | 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 | 284 | #pragma omp for |
| 280 | - // Loop over all observation in the container | |
| 281 | 285 | for (int i = 0; i < m_obs.size(); ++i) { |
| 286 | + | |
| 282 | 287 | // Get CTA observation |
| 283 | 288 | GCTAObservation* obs = dynamic_cast<GCTAObservation*>(&m_obs[i]); |
| 284 | - | |
| 289 | + | |
| 285 | 290 | // Continue only if observation is a CTA observation |
| 286 | 291 | if (obs != NULL) { |
| 292 | + | |
| 287 | 293 | // Write header for observation |
| 288 | 294 | if (logTerse()) { |
| 289 | 295 | if (obs->name().length() > 1) { |
| ... | ... | @@ -293,23 +299,25 @@ void ctobssim::run(void) |
| 293 | 299 | wrklog.header3("Observation"); |
| 294 | 300 | } |
| 295 | 301 | } |
| 302 | + | |
| 296 | 303 | // Increment counter |
| 297 | 304 | n_observations++; |
| 298 | 305 | |
| 299 | 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 | 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 | 312 | } // endif: CTA observation found |
| 306 | 313 | |
| 307 | 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 | 318 | #pragma omp critical (log) |
| 311 | 319 | { |
| 312 | - log<<wrklog; | |
| 320 | + log << wrklog; | |
| 313 | 321 | } |
| 314 | 322 | |
| 315 | 323 | } // end pragma omp parallel |
| ... | ... | @@ -436,22 +444,33 @@ void ctobssim::get_parameters(void) |
| 436 | 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 | 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 | 463 | for (int i = 0; i < m_obs.size(); ++i) { |
| 464 | + | |
| 465 | + // Allocate new seed value | |
| 449 | 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 | 471 | do { |
| 452 | - // Each seed will be unique | |
| 453 | 472 | new_seed = (unsigned long long int)(master.int64() * 1.0e2); |
| 454 | - repeat = false; | |
| 473 | + repeat = false; | |
| 455 | 474 | for (int j = 0; j < seeds.size(); ++j) { |
| 456 | 475 | if (new_seed == seeds[j]) { |
| 457 | 476 | repeat = true; |
| ... | ... | @@ -460,13 +479,14 @@ void ctobssim::get_parameters(void) |
| 460 | 479 | } |
| 461 | 480 | } while(repeat); |
| 462 | 481 | |
| 463 | - //Add the seed to the vector | |
| 482 | + // Add the seed to the vector for bookkeeping | |
| 464 | 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 | 487 | m_rans.push_back(GRan(new_seed)); |
| 468 | - } | |
| 469 | - | |
| 488 | + | |
| 489 | + } // endfor: looped over observations | |
| 470 | 490 | |
| 471 | 491 | // Return |
| 472 | 492 | return; |
| ... | ... | @@ -556,6 +576,8 @@ void ctobssim::set_list(GCTAObservation* obs) |
| 556 | 576 | * |
| 557 | 577 | * @param[in] obs Pointer on CTA observation. |
| 558 | 578 | * @param[in] photons Photon list. |
| 579 | + * @param[in] ran Random number generator. | |
| 580 | + * @param[in] wrklog Pointer to logger. | |
| 559 | 581 | * |
| 560 | 582 | * @exception GCTAException::no_response |
| 561 | 583 | * No valid response found in CTA observation |
| ... | ... | @@ -571,13 +593,15 @@ void ctobssim::set_list(GCTAObservation* obs) |
| 571 | 593 | * they can be scatter within the ROI and energy interval by the |
| 572 | 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 | 599 | // Continue only if observation pointer is valid |
| 577 | 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 | 607 | // Get pointer on CTA response. Throw an exception if the response |
| ... | ... | @@ -789,19 +813,23 @@ void ctobssim::simulate_source(GCTAObservation* obs, const GModels& models, GRan |
| 789 | 813 | * |
| 790 | 814 | * @param[in] obs Pointer on CTA observation. |
| 791 | 815 | * @param[in] models Models. |
| 816 | + * @param[in] ran Random number generator. | |
| 817 | + * @param[in] wrklog Pointer to logger. | |
| 792 | 818 | * |
| 793 | 819 | * Simulate background events from models. The events are stored as event |
| 794 | 820 | * list in the observation. |
| 795 | 821 | * |
| 796 | 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 | 827 | // Continue only if observation pointer is valid |
| 801 | 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 | 835 | // Make sure that the observation holds a CTA event list. If this |
| ... | ... | @@ -892,6 +920,7 @@ void ctobssim::init_members(void) |
| 892 | 920 | m_deadc = 1.0; |
| 893 | 921 | |
| 894 | 922 | // Initialise protected members |
| 923 | + m_rans.clear(); | |
| 895 | 924 | m_obs.clear(); |
| 896 | 925 | m_use_xml = false; |
| 897 | 926 | m_read_ahead = false; |
| ... | ... | @@ -934,7 +963,7 @@ void ctobssim::copy_members(const ctobssim& app) |
| 934 | 963 | // Copy protected members |
| 935 | 964 | m_area = app.m_area; |
| 936 | 965 | m_time_max = app.m_time_max; |
| 937 | - m_rans = app.m_rans; | |
| 966 | + m_rans = app.m_rans; | |
| 938 | 967 | m_obs = app.m_obs; |
| 939 | 968 | m_use_xml = app.m_use_xml; |
| 940 | 969 | m_read_ahead = app.m_read_ahead; | ... | ... |
src/ctobssim/ctobssim.hpp
| ... | ... | @@ -33,7 +33,7 @@ |
| 33 | 33 | |
| 34 | 34 | /* __Definitions _________________________________________________________ */ |
| 35 | 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 | 95 | double m_deadc; //!< Average deadtime correction |
| 96 | 96 | |
| 97 | 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 | 106 | #endif /* CTOBSSIM_HPP */ | ... | ... |
src/ctobssim/ctobssim.par