LCOV - code coverage report
Current view: top level - include/crpropa/module - Observer.h (source / functions) Coverage Total Hit
Test: coverage.info.cleaned Lines: 43.8 % 16 7
Test Date: 2026-06-18 09:49:19 Functions: 100.0 % 2 2

            Line data    Source code
       1              : #ifndef CRPROPA_OBSERVER_H
       2              : #define CRPROPA_OBSERVER_H
       3              : 
       4              : #include <fstream>
       5              : #include <limits>
       6              : #include <string>
       7              : #include <vector>
       8              : 
       9              : #include "../Candidate.h"
      10              : #include "../Module.h"
      11              : #include "../Referenced.h"
      12              : #include "../Vector3.h"
      13              : #include "../Geometry.h"
      14              : 
      15              : namespace crpropa {
      16              : 
      17              : enum DetectionState {
      18              :         DETECTED, VETO, NOTHING
      19              : };
      20              : 
      21              : /** \addtogroup Observer
      22              :  * @{
      23              :  */
      24              : 
      25              : 
      26              : /**
      27              :  @class ObserverFeature
      28              :  @brief Abstract base class for features of observers
      29              :  */
      30            5 : class ObserverFeature: public Referenced {
      31              : protected:
      32              :         std::string description;
      33              : public:
      34              :         virtual DetectionState checkDetection(Candidate *candidate) const;
      35              :         virtual void onDetection(Candidate *candidate) const;
      36              :         virtual std::string getDescription() const;
      37              : };
      38              : 
      39              : 
      40              : /**
      41              :  @class Observer
      42              :  @brief General particle observer
      43              :  */
      44              : class Observer: public Module {
      45              :         std::string flagKey;
      46              :         std::string flagValue;
      47              : private:
      48              :         std::vector<ref_ptr<ObserverFeature> > features;
      49              :         ref_ptr<Module> detectionAction;
      50              :         bool clone;
      51              :         bool makeInactive;
      52              : public:
      53              :         /** Default observer constructor
      54              :          */
      55              :         Observer();
      56              :         /** Add a feature to the observer
      57              :          @param feature         observer feature to be added to the Observer object
      58              :          */
      59              :         void add(ObserverFeature *feature);
      60              :         /** Perform some specific actions upon detection of candidate
      61              :          @param action          module that performs a given action when candidate is detected
      62              :          @param clone           if true, clone candidate
      63              :          */
      64              :         void onDetection(Module *action, bool clone = false);
      65              :         void process(Candidate *candidate) const;
      66              :         std::string getDescription() const;
      67              :         void setFlag(std::string key, std::string value);
      68              :         /** Determine whether candidate should be deactivated on detection
      69              :          @param deactivate      if true, deactivate detected particles; if false, continue tracking them
      70              :          */
      71              :         void setDeactivateOnDetection(bool deactivate);
      72              : };
      73              : 
      74              : 
      75              : /**
      76              :  @class ObserverDetectAll
      77              :  @brief Detects all particles
      78              :  */
      79            2 : class ObserverDetectAll: public ObserverFeature {
      80              : public:
      81              :         DetectionState checkDetection(Candidate *candidate) const;
      82              :         std::string getDescription() const;
      83              : };
      84              : 
      85              : 
      86              : /**
      87              :  @class ObserverSurface
      88              :  @brief Detects particles crossing the boundaries of a defined surface (see, e.g., `Geometry` module)
      89              :  */
      90              : class ObserverSurface: public ObserverFeature {
      91              : private:
      92              :         ref_ptr<Surface> surface;
      93              : public:
      94              :         /** Constructor
      95              :          @param surface         object with some specific geometric (see Geometry.h)
      96              :         */
      97              :         ObserverSurface(Surface* surface);
      98              :         DetectionState checkDetection(Candidate *candidate) const;
      99              :         std::string getDescription() const;
     100              : };
     101              : 
     102              : 
     103              : /**
     104              :  @class ObserverTracking
     105              :  @brief Tracks particles inside a sphere
     106              :  */
     107              : class ObserverTracking: public ObserverFeature {
     108              : private:
     109              :         Vector3d center;
     110              :         double radius;
     111              :     double stepSize;
     112              : public:
     113              :         /** Constructor
     114              :          @param center          vector containing the coordinates of the center of the sphere
     115              :          @param radius          radius of the sphere
     116              :          @param stepSize        observer will keep track of particles at every step with this size
     117              :         */
     118              :         ObserverTracking(Vector3d center, double radius, double stepSize = 0);
     119              :         DetectionState checkDetection(Candidate *candidate) const;
     120              :         std::string getDescription() const;
     121              : };
     122              : 
     123              : 
     124              : /**
     125              :  @class Observer1D
     126              :  @brief Detects particles when reaching x = 0
     127              : 
     128              :  This module detects particles when reaching x = 0 and also limits the next step size to prevent candidates from overshooting.
     129              :  */
     130            3 : class Observer1D: public ObserverFeature {
     131              : public:
     132              :         DetectionState checkDetection(Candidate *candidate) const;
     133              :         std::string getDescription() const;
     134              : };
     135              : 
     136              : 
     137              : /**
     138              :  @class ObserverRedshiftWindow
     139              :  @brief Detects particles in a given redshift window
     140              : 
     141              :  When added to an observer, this feature generalizes it to four dimensions.
     142              :  The fourth dimension is the redshift, a proxy for time. This is particularly
     143              :  useful in "4D" studies, including either time-dependence (e.g. flaring objects),
     144              :  or in 3D studies including cosmological evolution.
     145              :  Note that redshifts should be assigned to sources when using this feature.
     146              :  This can be done with: SourceRedshift, SourceRedshift1D, SourceUniformRedshift,
     147              :  and SourceRedshiftEvolution.
     148              :  */
     149              : class ObserverRedshiftWindow: public ObserverFeature {
     150              : private:
     151              :         double zmin, zmax;
     152              : public:
     153              :         /** Constructor
     154              :          @param zmin    lower bound of redshift interval
     155              :          @param zmax    upper bound of redshift interval
     156              :          */
     157              :         ObserverRedshiftWindow(double zmin = 0, double zmax = 0.1);
     158              :         DetectionState checkDetection(Candidate *candidate) const;
     159              :         std::string getDescription() const;
     160              : };
     161              : 
     162              : 
     163              : /**
     164              :  @class ObserverInactiveVeto
     165              :  @brief Veto for inactive candidates
     166              :  */
     167            0 : class ObserverInactiveVeto: public ObserverFeature {
     168              : public:
     169              :         DetectionState checkDetection(Candidate *candidate) const;
     170              :         std::string getDescription() const;
     171              : };
     172              : 
     173              : 
     174              : /**
     175              :  @class ObserverNucleusVeto
     176              :  @brief Veto for nuclei (including protons and neutrons)
     177              :  */
     178            0 : class ObserverNucleusVeto: public ObserverFeature {
     179              : public:
     180              :         DetectionState checkDetection(Candidate *candidate) const;
     181              :         std::string getDescription() const;
     182              : };
     183              : 
     184              : 
     185              : /**
     186              :  @class ObserverNeutrinoVeto
     187              :  @brief Veto for neutrinos
     188              :  */
     189            0 : class ObserverNeutrinoVeto: public ObserverFeature {
     190              : public:
     191              :         DetectionState checkDetection(Candidate *candidate) const;
     192              :         std::string getDescription() const;
     193              : };
     194              : 
     195              : 
     196              : /**
     197              :  @class ObserverPhotonVeto
     198              :  @brief Veto for photons
     199              :  */
     200            0 : class ObserverPhotonVeto: public ObserverFeature {
     201              : public:
     202              :         DetectionState checkDetection(Candidate *candidate) const;
     203              :         std::string getDescription() const;
     204              : };
     205              : 
     206              : 
     207              : /**
     208              :  @class ObserverElectronVeto
     209              :  @brief Veto for electrons and positrons
     210              :  */
     211            0 : class ObserverElectronVeto: public ObserverFeature {
     212              : public:
     213              :         DetectionState checkDetection(Candidate *candidate) const;
     214              :         std::string getDescription() const;
     215              : };
     216              : 
     217              : 
     218              : /**
     219              :  @class ObserverParticleIdVeto
     220              :  @brief Custom veto for user-defined particle types
     221              :  Vetoes for more than one type of particle can be added by calling this
     222              :  feature multiple times.
     223              :  */
     224              : class ObserverParticleIdVeto: public ObserverFeature {
     225              : private:
     226              :         int vetoParticleId;
     227              : public:
     228              :         /** Constructor
     229              :          @param id              id of the particle following the PDG numbering scheme
     230              :          */
     231              :         ObserverParticleIdVeto(int id);
     232              :         DetectionState checkDetection(Candidate *candidate) const;
     233              :         std::string getDescription() const;
     234              : };
     235              : 
     236              : 
     237              : /**
     238              :  @class ObserverTimeEvolution
     239              :  @brief Observes the time evolution of the candidates (phase-space elements)
     240              :  This observer is very useful if the time evolution of the particle density is needed. It detects all candidates in lin-spaced, log-spaced, or user-defined time intervals and limits the nextStep of candidates to prevent overshooting of detection intervals.
     241              :  */
     242              : class ObserverTimeEvolution: public ObserverFeature {
     243              : protected:
     244              :         int nIntervals;  // number of time invervals
     245              :         bool isLogarithmicScaling = false;  // enables or disables logarithmic scaling for the intervals
     246              :         bool doDetListConstruction = true;  // enables the construction of detList in the relevant functions (addTime, addTimeRange)
     247              :         double minimum;  // the minimum time
     248              :         double maximum;  // the maximum time
     249              :         /** Vector containing all used times. 
     250              :          It is only constructed by the user manually.
     251              :          If it is not empty, the vector will be used instead of the getTime function.
     252              :          (leave empty if you want to rather use functions)
     253              :         */
     254              :         std::vector<double> detList;
     255              :         /**
     256              :          A temporary storage for detList, this enables the return of a List in getTimes
     257              :          without risking to modify detList
     258              :          */
     259              :         mutable std::vector<double> tempDetList;
     260              :         
     261              : public:
     262              :         /** Default constructor
     263              :          */
     264              :         ObserverTimeEvolution();
     265              :         /** Constructor
     266              :          @param min             minimum time
     267              :          @param dist    time interval for detection
     268              :          @param numb    number of time intervals
     269              : 
     270              :          This constructor calculates the maximum from max = min + (numb - 1) * dist
     271              :          */
     272              :         ObserverTimeEvolution(double min, double dist, double numb);
     273              :         /** Constructor
     274              :          @param min             minimum time
     275              :          @param max         maximum time
     276              :          @param numb    number of time intervals
     277              :          @param log     log (input: true) or lin (input: false) scaling between min and max with numb steps
     278              :          
     279              :          This constructor sets the maximum directly and gets numb automatically.
     280              :          You need to set the log parameter, since an overload for the first three doubles exist.
     281              :          */
     282              :         ObserverTimeEvolution(double min, double max, double numb, bool log);
     283              :         /** Constructor
     284              :          @param detList user defined vector<double> with times to check
     285              : 
     286              :          This constructor uses a predefined vector containing the times that should be observed.
     287              :          The so created detList can then be modified via addTime, addTimeRange and setTimes.
     288              :          */
     289              :         ObserverTimeEvolution(const std::vector<double> &detList);
     290              :         /** Destructor
     291              :          */
     292            6 :         ~ObserverTimeEvolution(){}
     293              : 
     294              :         /** Function
     295              :          Generates the detList if it is empty when for example the 
     296              :          ObserverTimeEvolution(const std::vector<double> &detList) constructor
     297              :          was not used.
     298              :          Use this function to create a detList with can then be modified.
     299              :          When detList is not empty its entries are used instead of a runtime calculation of the times.   
     300              :          */
     301              :         void constructDetListIfEmpty();
     302              :         /** Function
     303              :          @param candidate       Candidate usally given by a module list
     304              : 
     305              :          Checks whether to make a detection at the current step of candidate or not.
     306              :          This function is called in Observer.process with the simulated Candidate.
     307              :          */
     308              :         DetectionState checkDetection(Candidate *candidate) const;
     309              :         /** Function
     310              :          @param enableConstruction      if true, constructs detList from range of min, max, numb
     311              :          when calling addTime
     312              :          Clears the content of detList
     313              :          */
     314              :         void clear();
     315              :         /** Function
     316              :          Checks if detList is empty
     317              :          */
     318              :         bool empty(){return detList.empty();}
     319              : 
     320              :         // setter functions:
     321              :         /** Function
     322              :          @param time    Time that should be appended to detList
     323              : 
     324              :          Makes a push_back on detList with the given time.       
     325              :          */
     326              :         void addTime(const double &time);
     327              :         /** Function
     328              :          @param min             minimum time
     329              :          @param max         maximum time
     330              :          @param numb    number of time intervals
     331              :          @param log     log (input: true) or lin (input: false) scaling between min and max with numb steps
     332              : 
     333              :          Appends a linear or logarithmic time range to detList via repeatedly calling addTime
     334              :          */
     335              :         void addTimeRange(double min, double max, double numb, bool log = false);
     336              :         /** Function
     337              :          @param detList vector<double> containing times when to observe
     338              : 
     339              :          Sets this->detList to detList, with this it is possible to fully modify detList
     340              :          */
     341              :         void setTimes(const std::vector<double> &detList);
     342              :         void setMinimum(double min);
     343            3 :         void setMaximum(double max){this->maximum = max;}
     344            6 :         void setNIntervals(int numb){this->nIntervals = numb;}
     345            2 :         void setIsLogarithmicScaling(bool log){this->isLogarithmicScaling = log;}
     346              : 
     347              :         // getter functions:
     348              :         /** Function
     349              :          @param index   index of the required time
     350              : 
     351              :          Replaces the previous preconstructed detList and return the time for a specific index
     352              :          */
     353              :         virtual double getTime(std::size_t index) const;
     354            0 :         double getMinimum() const {return minimum;}
     355            0 :         double getMaximum() const {return maximum;}
     356            0 :         int getNIntervals() const {return nIntervals;}
     357            0 :         bool getIsLogarithmicScaling() const {return isLogarithmicScaling;}
     358              :         /** Function 
     359              :          Returns a vector<double> containing all times between min and max generated by getTime.
     360              :          This function does not return detList directly, it rather appends a new vector with
     361              :          getTime numb times.
     362              :          */
     363              :         const std::vector<double>& getTimes() const;
     364              :         /** Function
     365              :          Returns a string containing a representation of all times.
     366              :          This function does not create a detList.
     367              :          */
     368              :         std::string getDescription() const;
     369              : };
     370              : 
     371              : /** @} */
     372              : 
     373              : }
     374              : 
     375              : #endif // CRPROPA_OBSERVER_H
        

Generated by: LCOV version 2.0-1