RooParticle
Particle scattering simulator with ROOT
Loading...
Searching...
No Matches
inspector.cpp
Go to the documentation of this file.
1#include "iostream"
2
3#include "TROOT.h"
4#include "TString.h"
5#include "TVectorT.h"
6#include "TMath.h"
7
8#include "inspector.hpp"
9#include "asymptoteN.hpp"
10
11#include <limits>
12
13Double_t XVCosine(particle* p1, particle* p2){
14 TVector3 dX = (p1->GetX())-(p2->GetX());
15 TVector3 dV = (p1->GetV())-(p2->GetV());
16 Double_t cc = (dX*dV)/(dX.Mag() * dV.Mag());
17 return cc;
18}
19
20// Double_t XVCosine(TVector3 dX, TVector3 dV){
21// return (dX*dV)/(TMath::Sqrt(dX.Norm2Sqr() * dV.Norm2Sqr()));
22// }
23
24// The name is parsed once (SetMethod) ; names and enumerators are listed here only.
25static const struct {const char * name; inspector::methodID id;} gMethodTable[] = {
26 {"COS", inspector::kCOS}, {"DEG", inspector::kDEG}, {"RAD", inspector::kRAD},
27 {"CNT", inspector::kCNT}, {"SPDG", inspector::kSPDG}, {"SPDL", inspector::kSPDL},
28 {"DSTG", inspector::kDSTG}, {"DSTL", inspector::kDSTL}, {"VRG", inspector::kVRG},
29 {"VRL", inspector::kVRL},
30 {"KEG", inspector::kKEG}, {"KEL", inspector::kKEL}, {"PEG", inspector::kPEG},
31 {"PEL", inspector::kPEL}, {"PKG", inspector::kPKG}, {"PKL", inspector::kPKL},
32 {"EDG", inspector::kEDG}, {"EDL", inspector::kEDL}, {"TMG", inspector::kTMG},
33 {"TML", inspector::kTML}, {"DXG", inspector::kDXG}, {"DXL", inspector::kDXL},
34 {"DNG", inspector::kDNG}, {"DNL", inspector::kDNL}, {"DSG", inspector::kDSG},
35 {"DSL", inspector::kDSL}, {"ACG", inspector::kACG}, {"ACL", inspector::kACL},
36 {"VOUT", inspector::kVOUT}, {"VOUTA",inspector::kVOUTA},
37 {"ASYD", inspector::kASYD}, {"ASYV", inspector::kASYV}
38};
39
41 for(const auto & e : gMethodTable){
42 if(name==e.name) return e.id;
43 }
44 return kInvalid;
45}
46
48 for(const auto & e : gMethodTable){
49 if(e.id==id) return TString(e.name);
50 }
51 return TString("");
52}
53
54inspectorP::inspectorP(particle *p1_, particle *p2_, TString method_, Double_t val_): inspector(){
55 SetParticles(p1_,p2_);
56 SetMethod(method_);
57 SetValue(val_);
58}
59
60inspectorP::inspectorP(particle *p1_, particle *p2_, methodID method_, Double_t val_): inspector(){
61 SetParticles(p1_,p2_);
62 SetMethod(method_);
63 SetValue(val_);
64}
65
67 switch(mtd){
68 case kCOS: return CalculateCosine();
69 case kDEG: return CalculateAngle(kFALSE);
70 case kRAD: return CalculateAngle(kTRUE);
71 case kCNT: return CountDeriving();
72 case kSPDG: case kSPDL: return CaculateSPD();
73 case kDSTG: case kDSTL: return CalculateDistance();
74 case kVRG: case kVRL: return CalculateRadialVelocity();
75 default:
76 std::cout<<"No Function Name Defined / "<< ftn << std::endl;
77 return kFALSE;
78 }
79}
80
82 return mtd>=kCOS && mtd<=kVRL;
83}
84
86 if(!IsValid()){
87 return kFALSE;
88 }
89 // Undefined angle / cosine / radial velocity : never satisfied.
90 // (not left to NaN comparisons ; atan2(0,0) would give 0 degree and satisfy "<")
91 if((mtd==kCOS || mtd==kDEG || mtd==kRAD) && IsDegenerate(kTRUE)){
92 return kFALSE;
93 }
94 if((mtd==kVRG || mtd==kVRL) && IsDegenerate(kFALSE)){
95 return kFALSE;
96 }
97 if(mtd==kCNT || mtd==kSPDG || mtd==kDSTG || mtd==kVRG){
98 return Evaluate()>val;
99 }
100 return Evaluate()<val;
101}
102
103Bool_t inspectorP::IsDegenerate(Bool_t needV){
104 // exact zero : the direction does not exist
105 if(((p1->GetX())-(p2->GetX())).Mag2()==0.) return kTRUE;
106 if(needV && ((p1->GetV())-(p2->GetV())).Mag2()==0.) return kTRUE;
107 return kFALSE;
108}
109
111 return ((p1->GetX())-(p2->GetX())).Mag();
112}
113
115 // positive : the two particles are receding from each other
116 TVector3 dX = (p1->GetX())-(p2->GetX());
117 TVector3 dV = (p1->GetV())-(p2->GetV());
118 if(dX.Mag2()==0.) return TMath::QuietNaN();
119 return (dX*dV)/dX.Mag();
120}
121
123 if(IsDegenerate(kTRUE)) return TMath::QuietNaN();
124 TVector3 dX = (p1->GetX())-(p2->GetX());
125 TVector3 dV = (p1->GetV())-(p2->GetV());
126 Double_t cc = (dX*dV)/(dX.Mag() * dV.Mag());
127 // rounding can push it slightly out of [-1, 1]
128 return TMath::Max(-1., TMath::Min(1., cc));
129}
130
131Double_t inspectorP::CalculateAngle(Bool_t rad){
132 if(IsDegenerate(kTRUE)) return TMath::QuietNaN();
133 // atan2(|dX x dV|, dX.dV) keeps the precision near 0 and pi, where acos(cos) does not
134 TVector3 dX = (p1->GetX())-(p2->GetX());
135 TVector3 dV = (p1->GetV())-(p2->GetV());
136 Double_t angle = TMath::ATan2(dX.Cross(dV).Mag(), dX.Dot(dV));
137 return rad ? angle : angle * TMath::RadToDeg();
138}
139
141 return ((p1->GetV())-(p2->GetV())).Mag();
142}
143
145 return min(p1->GetECNT(), p2->GetECNT());
146
147}
148
149Bool_t inspectorP::SetInitial_D(Bool_t index, TString mtd,Double_t var){ //set primary condition from present condition
150 // if index=0(false) -> move p1, index=1(true) -> move p2
151
152 // Initial position solver
153 // with dot product
154 // Go around signal (x, v)
155 // angle between x and v -> less than epsilon
156
157 // Approaching signal
158 // angle between x and v -> larger than 1 (or pi) - eps
159 cerr<<"inspector::SetInitial_D function needs to be fixed. do not use without any instruction "<<endl;
160
161 if(mtd==""){
162 mtd = ftn;
163 }
164
165
166 //DEFAULT, BASED ON COS_VALUE
167 Double_t objval;
168 if(mtd=="COS"){
169 objval = -1+var;
170 }else if(mtd=="DEG"){
171 objval = TMath::Cos((180-var)*TMath::DegToRad());
172 }else if(mtd=="RAD"){
173 objval = TMath::Cos((180-var));
174 }else{
175 std::cerr<<"No Function Name Defined / "<< ftn << std::endl;
176 return kFALSE;
177 }
178
179 particle *p0_, *p1_;
180 if(index){
181 p0_ = p1;
182 p1_ = p2;
183 }else{
184 p0_ = p2;
185 p1_ = p1;
186 }
187
188 Double_t cos0, cos1;
189 cos0 = XVCosine(p0_, p1_);
190 cos1 = objval;
191 Double_t A1 = (p0_->GetX().Mag());
192 Double_t A2 = cos0-TMath::Sqrt(1-TMath::Power(cos0,2))*(cos1/TMath::Sqrt(1-TMath::Power(cos1,2)));
193 Double_t A = A1*A2;
194
195 p0_->x -= A*(p0_->GetV());
196
197
198 return kTRUE;
199
200 // A = (|x0|/|v0|)*(cos(th0)-cos(th1)*sin(th0)*sqrt(1-(cos(th1))^2))
201 // cos th1 :: settedvalue
202}
203
204// Bool_t inspectorP::SetInitial(Bool_t index, TString mtd, Double_t var){
205// return kTRUE;
206// }
207
208TString inspector::Print(Bool_t element, Bool_t mechanics, Bool_t mute, Bool_t pprint){
209 return TString("");
210}
211
212
213
214TString inspectorP::Print(Bool_t particles, Bool_t mechanics, Bool_t mute, Bool_t pprint){
215 TString result = TString("");
216 result += TString("[ Inspector Particle: ");
217 if(particles){
218 result += GetParticle1()->Print(kTRUE, kTRUE);
219 result += GetParticle2()->Print(kTRUE, kTRUE);
220 }
221
222 result += ", MTD: ";
223 result += ftn;
224
225 result += ", CRT: ";
226 result += GetCriterion();
227
228 result += ", VAL: ";
229 result += Evaluate();
230
231 result += ", RST: ";
232 result += Inspect();
233
234 result += " ]";
235
236 if(!mute){
237 cout<<result<<endl;
238 }
239 return result;
240
241}
242
243
244//========================================//
245
246inspectorE::inspectorE(event* event_, TString method_, Double_t val_): inspector(){
247 SetEvent(event_);
248 SetMethod(method_);
249 SetValue(val_);
250}
251
252inspectorE::inspectorE(event* event_, methodID method_, Double_t val_): inspector(){
253 SetEvent(event_);
254 SetMethod(method_);
255 SetValue(val_);
256}
257
259 return mtd>=kKEG && mtd<=kASYV;
260}
261
263 switch(mtd){
264 case kPEG: return (evt->GetNetPE()>val);
265 case kPEL: return (evt->GetNetPE()<val);
266 case kKEG: case kPKG: case kEDG: case kTMG: case kDSG: case kACG:
267 return (Evaluate()>val);
268 case kKEL: case kPKL: case kEDL: case kTML: case kDSL: case kACL:
269 return (Evaluate()<val);
270 case kVOUT: return !(evt->IsInsideVolume(kTRUE)); // not all inside : somebody is out
271 case kVOUTA: return !(evt->IsInsideVolume(kFALSE)); // nobody inside : all are out
272 case kASYD: case kASYV: {
273 // The estimate only means "what is left" once every interacting pair
274 // is receding : before that the collision is still ahead.
275 Bool_t separating = kFALSE;
276 Double_t rest = (mtd==kASYD) ? asymptoteN::RemainingDeflection(evt, &separating)
277 : asymptoteN::RemainingChange(evt, &separating);
278 return separating && (rest<val);
279 }
280 case kDXG: return (evt->GetDeriveN(kFALSE)>val);
281 case kDXL: return (evt->GetDeriveN(kTRUE)<val);
282 case kDNG: return (evt->GetDeriveN(kFALSE)>val);
283 case kDNL: return (evt->GetDeriveN(kTRUE)<val);
284 default:
285 std::cerr<<"inspectorE::Inspect : inspecting method isn't defined well." << std::endl;
286 return kFALSE;
287 }
288}
289
291 switch(mtd){
292 // Kinetic energy : the relativistic one when the relativistic mode is
293 // really in effect (Taylor ignores the setting), as for "EDG"/"EDL".
294 case kKEG: case kKEL:
296 case kPEG: case kPEL: return evt->GetNetPE();
297 case kDXG: case kDXL: return evt->GetDeriveN(kFALSE);
298 case kDNG: case kDNL: return evt->GetDeriveN(kTRUE);
299 case kPKG: case kPKL: {
300 Double_t ke = (evt->IsRelativistic() && evt->GetIntegrator()!=event::kTaylor) ? evt->GetNetKERel() : evt->GetNetKE();
301 if(ke==0.){
302 return std::numeric_limits<Double_t>::max();
303 }
304 return TMath::Abs(evt->GetNetPE()/ke);
305 }
306 case kEDG: case kEDL: {
307 // In relativistic mode only the relativistic energy is conserved.
308 // (Taylor ignores the relativistic setting.)
309 Bool_t rel = evt->IsRelativistic() && evt->GetIntegrator()!=event::kTaylor;
310 Double_t e = rel ? evt->GetNetERel() : evt->GetNetE();
311 if(!hasRef){
312 ref = e;
313 hasRef = kTRUE;
314 }
315 if(ref==0.){
316 return TMath::Abs(e);
317 }
318 return TMath::Abs((e-ref)/ref);
319 }
320 case kTMG: case kTML: return evt->GetTime();
321 case kDSG: case kDSL: return evt->GetMinDistance();
322 case kACG: case kACL: return evt->GetMaxAcceleration();
323 case kVOUT: return !(evt->IsInsideVolume(kTRUE));
324 case kVOUTA: return !(evt->IsInsideVolume(kFALSE));
327 default:
328 std::cerr<<"inspectorE::Inspect : inspecting method isn't defined well." << std::endl;
329 return kFALSE;
330 }
331}
332
333TString inspectorE::Print(Bool_t element, Bool_t mechanics, Bool_t mute, Bool_t pprint){
334 TString result = TString("");
335 result += TString("[ Inspector Event: ");
336
337 result += "MTD: ";
338 result += ftn;
339
340 result += ", CRT: ";
341 result += GetCriterion();
342
343 result += ", VAL: ";
344 result += Evaluate();
345
346 result += ", RST: ";
347 result += Inspect();
348
349 if(IsAbort()){
350 result += ", ABORT";
351 }
352
353 result += " ]";
354
355 if(!mute){
356 cout<<result<<endl;
357 }
358 return result;
359}
360
361
static Double_t RemainingChange(event *ev, Bool_t *separating=nullptr)
static Double_t RemainingDeflection(event *ev, Bool_t *separating=nullptr)
One simulated system: particles, pairwise forces, inspectors and the time integration.
Definition event.hpp:93
Double_t GetMaxAcceleration()
The largest acceleration (force / rest mass) of the non-invincible particles (amu c^2/fm/amu = c^2/fm...
Definition event.cpp:929
Bool_t IsInsideVolume(Bool_t all=kTRUE)
Whether the non-invincible particles are inside the volume (SetVolume()).
Definition event.cpp:940
Double_t GetNetKERel()
Relativistic kinetic energy, the sum of over the non-invincible particles (amu c^2).
Definition event.cpp:711
Double_t GetNetPE()
Potential energy of the whole event (amu c^2).
Definition event.cpp:779
Long_t GetDeriveN(const Bool_t minima=kFALSE)
Number of derived steps (particle::GetECNT()) over all particles, invincible ones included.
Definition event.cpp:684
Double_t GetMinDistance()
The smallest distance between two particles, invincible ones included (fm). The largest double for fe...
Definition event.cpp:918
@ kTaylor
"TAYLOR", original scheme (default)
Definition event.hpp:114
integrator GetIntegrator()
The integrator in use (default kTaylor).
Definition event.hpp:198
Double_t GetNetE(const Bool_t SR=kFALSE)
GetNetKE(SR) + GetNetPE(); conserved in the non-relativistic mode only (see GetNetERel()).
Definition event.hpp:411
Double_t GetNetKE(const Bool_t SR=kFALSE)
Kinetic energy of the non-invincible particles (amu c^2).
Definition event.cpp:697
Double_t GetTime()
The largest evolved time among the particles (fm/c). Invincible particles do not advance their time,...
Definition event.cpp:909
Bool_t IsRelativistic()
Whether the relativistic flag is set (the flag only; kTaylor ignores it).
Definition event.hpp:265
Double_t GetNetERel()
GetNetKERel() + GetNetPE(); the conserved energy of the relativistic mode.
Definition event.hpp:419
TString Print(Bool_t element=kFALSE, Bool_t mechanics=kFALSE, Bool_t mute=kFALSE, Bool_t pprint=kFALSE)
Describe as text "[ Inspector Event: MTD, CRT, VAL, RST (, ABORT) ]".
Bool_t hasRef
E0 is set.
Bool_t IsValid()
Whether the method is one of the inspectorE methods.
Double_t ref
reference energy E0 of "EDG"/"EDL"
inspectorE(event *event_, TString method_, Double_t val_=-1)
Make an inspector on an event, method by name.
Bool_t Inspect()
Whether the condition is satisfied now (kFALSE for an invalid method, with a message on stderr).
event * evt
the watched event (not owned)
void SetEvent(event *event_)
Set the event to watch (forgets the reference energy E0).
Double_t Evaluate()
The compared quantity, see the table (the first "EDG"/"EDL" call sets E0 and gives 0).
Double_t Evaluate()
The compared quantity: cosine, angle (degree or radian), step count, relative speed (c),...
Definition inspector.cpp:66
TString Print(Bool_t particles=kFALSE, Bool_t mechanics=kFALSE, Bool_t mute=kFALSE, Bool_t pprint=kFALSE)
Describe as text "[ Inspector Particle: MTD, CRT (criterion), VAL (value), RST (result) ]".
Bool_t SetInitial_D(Bool_t index=0, TString mtd="", Double_t var=0.1)
Move one particle back along its velocity so that the present condition is the one of the given angle...
Double_t CalculateRadialVelocity()
dX.dV/norm(dX) (c), positive when receding; NaN if dX = 0.
inspectorP(particle *p1_, particle *p2_, TString method_, Double_t val_=-1)
Make an inspector on two particles, method by name.
Definition inspector.cpp:54
Double_t CalculateAngle(Bool_t rad=kFALSE)
Angle between dX and dV in degree (radian if rad), NaN for a zero vector.
particle * GetParticle2()
The second particle.
Bool_t IsValid()
Whether the method is one of the inspectorP methods.
Definition inspector.cpp:81
void SetParticles(particle *p1_, particle *p2_)
Set both particles.
particle * p1
particle * p2
the two particles (not owned)
particle * GetParticle1()
The first particle.
Bool_t Inspect()
Whether the condition is satisfied now.
Definition inspector.cpp:85
Double_t CalculateCosine()
Cosine of the angle between dX and dV, clamped to [-1, 1] (NaN for a zero vector).
Double_t CaculateSPD()
Relative speed, norm of dV (c).
Bool_t IsDegenerate(Bool_t needV=kTRUE)
Whether dX (or, if needV, dV) is the zero vector.
Double_t CalculateDistance()
Distance, norm of dX (fm).
Long_t CountDeriving()
Smaller of the step counts of the two particles.
Inspector : one terminating condition of event::DeriveInspect().
Definition inspector.hpp:28
void SetValue(Double_t val_)
Set the criterion value to compare with (in the unit of the quantity).
Definition inspector.hpp:96
static methodID ParseMethod(TString name)
Parse a method name.
Definition inspector.cpp:40
Double_t GetCriterion()
The criterion value (default -1).
methodID mtd
parsed method
void SetMethod(TString methodname)
Set the method by name.
methodID
Method identifier.
Definition inspector.hpp:35
@ kPEL
"PEL": net potential energy < value
Definition inspector.hpp:52
@ kACG
"ACG": largest acceleration > value
Definition inspector.hpp:65
@ kKEG
"KEG": net kinetic energy > value
Definition inspector.hpp:49
@ kDSTL
"DSTL": distance < value
Definition inspector.hpp:45
@ kASYD
"ASYD": remaining deflection of every moving particle < value (rad), all pairs receding
Definition inspector.hpp:69
@ kCNT
"CNT": number of derived steps > value
Definition inspector.hpp:41
@ kDNL
"DNL": smallest step count < value
Definition inspector.hpp:62
@ kTML
"TML": evolved time < value
Definition inspector.hpp:58
@ kRAD
"RAD": angle between dX and dV in radian < value
Definition inspector.hpp:40
@ kDSTG
"DSTG": distance > value
Definition inspector.hpp:44
@ kDSL
"DSL": smallest distance between two particles < value
Definition inspector.hpp:64
@ kKEL
"KEL": net kinetic energy < value
Definition inspector.hpp:50
@ kVRL
"VRL": radial velocity < value
Definition inspector.hpp:47
@ kASYV
"ASYV": remaining relative velocity change of every moving particle < value, all pairs receding
Definition inspector.hpp:70
@ kDSG
"DSG": smallest distance between two particles > value
Definition inspector.hpp:63
@ kInvalid
unknown method name
Definition inspector.hpp:36
@ kVOUT
"VOUT": any moving particle is outside the volume
Definition inspector.hpp:67
@ kVRG
"VRG": radial velocity > value
Definition inspector.hpp:46
@ kCOS
"COS": cosine of the angle between dX and dV < value
Definition inspector.hpp:38
@ kDNG
"DNG": largest step count > value (Evaluate() gives the smallest, see inspectorE)
Definition inspector.hpp:61
@ kTMG
"TMG": evolved time > value
Definition inspector.hpp:57
@ kVOUTA
"VOUTA": all moving particles are outside the volume
Definition inspector.hpp:68
@ kEDL
"EDL": relative drift of the net energy < value
Definition inspector.hpp:56
@ kPKL
"PKL": abs(net PE / net KE) < value
Definition inspector.hpp:54
@ kDXG
"DXG": largest step count > value
Definition inspector.hpp:59
@ kPEG
"PEG": net potential energy > value
Definition inspector.hpp:51
@ kSPDL
"SPDL": relative speed < value
Definition inspector.hpp:43
@ kDEG
"DEG": angle between dX and dV in degree < value
Definition inspector.hpp:39
@ kPKG
"PKG": abs(net PE / net KE) > value
Definition inspector.hpp:53
@ kACL
"ACL": largest acceleration < value
Definition inspector.hpp:66
@ kSPDG
"SPDG": relative speed > value
Definition inspector.hpp:42
@ kEDG
"EDG": relative drift of the net energy > value
Definition inspector.hpp:55
@ kDXL
"DXL": smallest step count < value (Evaluate() gives the largest, see inspectorE)
Definition inspector.hpp:60
static TString MethodName(methodID id)
Name of a method identifier.
Definition inspector.cpp:47
Double_t val
criterion value
TString ftn
method name as given
virtual TString Print(Bool_t element=kFALSE, Bool_t mechanics=kFALSE, Bool_t mute=kFALSE, Bool_t pprint=kFALSE)
Describe the inspector as text (the base class : empty).
Bool_t IsAbort()
Whether this is an abort inspector.
A point body with mass, position, velocity, step count, time and an optional recorded path.
Definition particle.hpp:36
TVector3 GetV()
Velocity in c (a copy).
Definition particle.hpp:164
TVector3 x
position in fm
Definition particle.hpp:226
Long_t GetECNT()
Number of derived steps ("evolved count"), counted also for an invincible particle.
Definition particle.hpp:176
TString Print(Bool_t onlymechanic=kFALSE, Bool_t mute=kFALSE)
Describe the particle as text "[ Particle x: [..], v: [..] ]".
Definition particle.cpp:173
TVector3 GetX()
Position in fm (a copy).
Definition particle.hpp:161
static const struct @0 gMethodTable[]
inspector::methodID id
Definition inspector.cpp:25
Double_t XVCosine(particle *p1, particle *p2)
Definition inspector.cpp:13
const char * name
Definition inspector.cpp:25