LCOV - code coverage report
Current view: top level - src - Candidate.cpp (source / functions) Coverage Total Hit
Test: coverage.info.cleaned Lines: 88.4 % 155 137
Test Date: 2026-07-13 06:03:10 Functions: 94.7 % 38 36

            Line data    Source code
       1              : #include "crpropa/Candidate.h"
       2              : #include "crpropa/ParticleID.h"
       3              : #include "crpropa/Units.h"
       4              : 
       5              : #include <stdexcept>
       6              : 
       7              : namespace crpropa {
       8              : 
       9      3379608 : Candidate::Candidate(int id, double E, Vector3d pos, Vector3d dir, double z, double weight, std::string tagOrigin) :
      10      6759216 :   redshift(z), trajectoryLength(0), weight(weight), currentStep(0), nextStep(0), active(true), parent(0), tagOrigin(tagOrigin), time(0) {
      11      3379608 :         ParticleState state(id, E, pos, dir);
      12              :         source = state;
      13              :         created = state;
      14              :         previous = state;
      15              :         current = state;
      16              : 
      17              : #if defined(OPENMP_3_1)
      18              :                 #pragma omp atomic capture
      19              :                 {serialNumber = nextSerialNumber++;}
      20              : #elif defined(__GNUC__)
      21      3379608 :                 {serialNumber = __sync_add_and_fetch(&nextSerialNumber, 1);}
      22              : #else
      23              :                 #pragma omp critical(serialNumber)
      24              :                 {serialNumber = nextSerialNumber++;}
      25              : #endif
      26              : 
      27      3379608 : }
      28              : 
      29           20 : Candidate::Candidate(const ParticleState &state) :
      30           20 :                 source(state), created(state), current(state), previous(state), redshift(0), trajectoryLength(0), currentStep(0), nextStep(0), active(true), parent(0), tagOrigin ("PRIM"), time(0) {
      31              : 
      32              : #if defined(OPENMP_3_1)
      33              :                 #pragma omp atomic capture
      34              :                 {serialNumber = nextSerialNumber++;}
      35              : #elif defined(__GNUC__)
      36           20 :                 {serialNumber = __sync_add_and_fetch(&nextSerialNumber, 1);}
      37              : #else
      38              :                 #pragma omp critical(serialNumber)
      39              :                 {serialNumber = nextSerialNumber++;}
      40              : #endif
      41              : 
      42           20 : }
      43              : 
      44        15426 : bool Candidate::isActive() const {
      45        15426 :         return active;
      46              : }
      47              : 
      48         1644 : void Candidate::setActive(bool b) {
      49         1644 :         active = b;
      50         1644 : }
      51              : 
      52       486594 : double Candidate::getRedshift() const {
      53       486594 :         return redshift;
      54              : }
      55              : 
      56      1011688 : double Candidate::getTrajectoryLength() const {
      57      1011688 :         return trajectoryLength;
      58              : }
      59              : 
      60      3829745 : double Candidate::getVelocity() const {
      61      3829745 :         return c_light;
      62              : }
      63              : 
      64         2029 : double Candidate::getWeight() const {
      65         2029 :         return weight;
      66              : }
      67              : 
      68         9499 : double Candidate::getCurrentStep() const {
      69         9499 :         return currentStep;
      70              : }
      71              : 
      72       492400 : double Candidate::getNextStep() const {
      73       492400 :         return nextStep;
      74              : }
      75              : 
      76      3336525 : void Candidate::setRedshift(double z) {
      77      3336525 :         redshift = z;
      78      3336525 : }
      79              : 
      80      3335683 : void Candidate::setTrajectoryLength(double a) {
      81      3335683 :         trajectoryLength = a;
      82      3335683 : }
      83              : 
      84      3336664 : void Candidate::setWeight(double w) {
      85      3336664 :         weight = w;
      86      3336664 : }
      87              : 
      88            5 : void Candidate::updateWeight(double w) {
      89            5 :         weight *= w;
      90            5 : }
      91              : 
      92       494099 : void Candidate::setCurrentStep(double lstep) {
      93       494099 :         currentStep = lstep;
      94       494099 :         trajectoryLength += lstep;
      95       494099 :         time += lstep / getVelocity();
      96       494099 : }
      97              : 
      98       492417 : void Candidate::setNextStep(double step) {
      99       492417 :         nextStep = step;
     100       492417 : }
     101              : 
     102       494705 : void Candidate::limitNextStep(double step) {
     103       494705 :         nextStep = std::min(nextStep, step);
     104       494705 : }
     105              : 
     106         1145 : void Candidate::setProperty(const std::string &name, const Variant &value) {
     107         1145 :         properties[name] = value;
     108         1145 : }
     109              : 
     110      3335653 : void Candidate::setTagOrigin (std::string tagOrigin) {
     111      3335653 :         this->tagOrigin = tagOrigin;
     112      3335653 : }
     113              : 
     114           26 : std::string Candidate::getTagOrigin () const {
     115           26 :         return tagOrigin;
     116              : }
     117              : 
     118      3335665 : void Candidate::setTime(double t) {
     119      3335665 :         time = t;
     120      3335665 : }
     121              : 
     122       480023 : double Candidate::getTime() const {
     123       480023 :         return time;
     124              : }
     125              : 
     126           14 : const Variant &Candidate::getProperty(const std::string &name) const {
     127           14 :         PropertyMap::const_iterator i = properties.find(name);
     128           14 :         if (i == properties.end())
     129            0 :                 throw std::runtime_error("Unknown candidate property: " + name);
     130           14 :         return i->second;
     131              : }
     132              : 
     133            3 : bool Candidate::removeProperty(const std::string& name) {
     134            3 :         PropertyMap::iterator i = properties.find(name);
     135            3 :         if (i == properties.end())
     136              :                 return false;
     137              :         properties.erase(i);
     138              :         return true;
     139              : }
     140              : 
     141           32 : bool Candidate::hasProperty(const std::string &name) const {
     142           32 :         PropertyMap::const_iterator i = properties.find(name);
     143           32 :         if (i == properties.end())
     144            8 :                 return false;
     145              :         return true;
     146              : }
     147              : 
     148            4 : void Candidate::addSecondary(Candidate *c) {
     149            4 :         secondaries.push_back(c);
     150            4 : }
     151              : 
     152            5 : void Candidate::addSecondary(int id, double energy, double w, std::string tagOrigin) {
     153            5 :         ref_ptr<Candidate> secondary = new Candidate;
     154            5 :         secondary->setRedshift(redshift);
     155            5 :         secondary->setTrajectoryLength(trajectoryLength);
     156            5 :         secondary->setTime(time);
     157            5 :         secondary->setWeight(weight * w);
     158           10 :         secondary->setTagOrigin(tagOrigin);
     159            5 :         for (PropertyMap::const_iterator it = properties.begin(); it != properties.end(); ++it) {
     160            0 :                 secondary->setProperty(it->first, it->second);         
     161              :         }
     162              :         secondary->source = source;
     163              :         secondary->previous = previous;
     164              :         secondary->created = previous;
     165              :         secondary->current = current;
     166            5 :         secondary->current.setId(id);
     167            5 :         secondary->current.setEnergy(energy);
     168            5 :         secondary->parent = this;
     169            5 :         secondaries.push_back(secondary);
     170            5 : }
     171              : 
     172      3335646 : void Candidate::addSecondary(int id, double energy, Vector3d position, double w, std::string tagOrigin) {
     173      3335646 :         ref_ptr<Candidate> secondary = new Candidate;
     174      3335646 :         secondary->setRedshift(redshift);
     175      3335646 :         secondary->setTrajectoryLength(trajectoryLength - (current.getPosition() - position).getR());
     176      3335646 :         secondary->setTime(time - (current.getPosition() - position).getR() / getVelocity());
     177      3335646 :         secondary->setWeight(weight * w);
     178      6671292 :         secondary->setTagOrigin(tagOrigin);
     179      3335646 :         for (PropertyMap::const_iterator it = properties.begin(); it != properties.end(); ++it) {
     180            0 :                 secondary->setProperty(it->first, it->second);         
     181              :         }
     182              :         secondary->source = source;
     183              :         secondary->previous = previous;
     184              :         secondary->created = previous;
     185              :         secondary->current = current;
     186      3335646 :         secondary->current.setId(id);
     187      3335646 :         secondary->current.setEnergy(energy);
     188      3335646 :         secondary->current.setPosition(position);
     189      3335646 :         secondary->created.setPosition(position);
     190      3335646 :         secondary->parent = this;
     191      3335646 :         secondaries.push_back(secondary);
     192      3335646 : }
     193              : 
     194            0 : void Candidate::clearSecondaries() {
     195              :         secondaries.clear();
     196            0 : }
     197              : 
     198            0 : std::string Candidate::getDescription() const {
     199            0 :         std::stringstream ss;
     200            0 :         ss << "CosmicRay at z = " << getRedshift() << "\n";
     201            0 :         ss << "  source:  " << source.getDescription() << "\n";
     202            0 :         ss << "  current: " << current.getDescription();
     203            0 :         return ss.str();
     204            0 : }
     205              : 
     206           30 : ref_ptr<Candidate> Candidate::clone(bool recursive) const {
     207           30 :         ref_ptr<Candidate> cloned = new Candidate;
     208              :         cloned->source = source;
     209              :         cloned->created = created;
     210              :         cloned->current = current;
     211              :         cloned->previous = previous;
     212              : 
     213           30 :         cloned->properties = properties;
     214           30 :         cloned->active = active;
     215           30 :         cloned->redshift = redshift;
     216           30 :         cloned->weight = weight;
     217           30 :         cloned->trajectoryLength = trajectoryLength;
     218           30 :         cloned->time = time;
     219           30 :         cloned->currentStep = currentStep;
     220           30 :         cloned->nextStep = nextStep;
     221           30 :         if (recursive) {
     222            0 :                 cloned->secondaries.reserve(secondaries.size());
     223            0 :                 for (size_t i = 0; i < secondaries.size(); i++) {
     224            0 :                         ref_ptr<Candidate> s = secondaries[i]->clone(recursive);
     225            0 :                         s->parent = cloned;
     226            0 :                         cloned->secondaries.push_back(s);
     227              :                 }
     228              :         }
     229           30 :         return cloned;
     230              : }
     231              : 
     232           14 : uint64_t Candidate::getSerialNumber() const {
     233           14 :         return serialNumber;
     234              : }
     235              : 
     236           11 : void Candidate::setSerialNumber(const uint64_t snr) {
     237           11 :         serialNumber = snr;
     238           11 : }
     239              : 
     240           14 : uint64_t Candidate::getSourceSerialNumber() const {
     241           14 :         if (parent)
     242              :                 return parent->getSourceSerialNumber();
     243              :         else
     244           14 :                 return serialNumber;
     245              : }
     246              : 
     247           13 : uint64_t Candidate::getCreatedSerialNumber() const {
     248           13 :         if (parent)
     249            0 :                 return parent->getSerialNumber();
     250              :         else
     251           13 :                 return serialNumber;
     252              : }
     253              : 
     254            1 : void Candidate::setNextSerialNumber(uint64_t snr) {
     255            1 :         nextSerialNumber = snr;
     256            1 : }
     257              : 
     258            6 : uint64_t Candidate::getNextSerialNumber() {
     259            6 :         return nextSerialNumber;
     260              : }
     261              : 
     262              : uint64_t Candidate::nextSerialNumber = 0;
     263              : 
     264            1 : void Candidate::restart() {
     265            1 :         setActive(true);
     266            1 :         setTrajectoryLength(0);
     267            1 :         setTime(0);
     268              :         previous = source;
     269              :         current = source;
     270            1 : }
     271              : 
     272              : } // namespace crpropa
        

Generated by: LCOV version 2.0-1