Jpp
JORCAShowerPrefit.hh
Go to the documentation of this file.
1 #ifndef JORCASHOWERPREFIT_INCLUDE
2 #define JORCASHOWERPREFIT_INCLUDE
3 
4 /*TODO-suggested
5  This code erases elements from a vector, that is slow! Try to use another STL container, as list
6  */
7 
8 #include <string>
9 #include <iostream>
10 #include <vector>
11 #include <algorithm>
12 #include <memory>
13 
16 
17 #include "JTrigger/JHit.hh"
18 #include "JTrigger/JTimeslice.hh"
19 #include "JTrigger/JHitL0.hh"
20 #include "JTrigger/JHitL1.hh"
21 #include "JTrigger/JHitR1.hh"
22 #include "JTrigger/JBuildL0.hh"
23 #include "JTrigger/JBuildL2.hh"
24 #include "JTrigger/JAlgorithm.hh"
25 #include "JTrigger/JMatch3G.hh"
26 #include "JTrigger/JBind2nd.hh"
27 
28 #include "JFit/JFitStatus.hh"
29 #include "JFit/JEvt.hh"
30 #include "JFit/JEvtToolkit.hh"
31 #include "JFit/JFitToolkit.hh"
34 #include "JFit/JEstimator.hh"
35 #include "JFit/JPoint4D.hh"
36 #include "JFit/JModel.hh"
37 #include "JFit/JSimplex.hh"
40 
41 #include "JGeometry3D/JVector3D.hh"
42 #include "JTools/JRange.hh"
43 #include "JLang/JSharedPointer.hh"
44 
45 /**
46  * \author adomi, gmaggi
47  */
48 
49 namespace JFIT
50 {
51  /**
52  * class to handle first step of the shower reconstruction, mainly dedicated for ORCA
53  */
54 
56  {
57  private:
59  std::shared_ptr<JRegressor_t> fit2_;
60 
63 
64  public:
65 
66  /**
67  * Default constructor
68  */
70  {}
71 
72  /**
73  * Parameterized constructor
74  *
75  * \param moduleRouter JSharedPointer of JModuleRouter, this is built via detector file.
76  * \param parameters struct that holds default-optimized parameters for the reconstruction, which can be found in $JPP_DATA.
77  */
78 
80  const JFIT::JShowerPrefitParameters_t &parameters):
81  moduleRouter_(moduleRouter),
82  parameters_(parameters)
83  {
86 
87  fit2_ = std::make_shared<JRegressor_t>(true);
88  }
89 
91 
92  /**
93  * Declaration of Member function that actually performs the reconstruction
94  *
95  * \param timeSliceBuildL0 which is contructed via a JDAQEvent
96  * \param timeSliceBuildL2 which is contructed via a JDAQEvent
97  * \param OutFits non const (output) JEvt.
98  */
99 
100  void
101  getJEvt(const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL0,
102  const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL2,
103  JFIT::JEvt &OutFits) const;
104 
105  /**
106  * Member function used in the predicate of std::sort, to sort hits according to ToT and W
107  *
108  * \param first lhs hit
109  * \param second rhs hit
110  */
111 
112  static bool
113  totW(const JTRIGGER::JHitR1& first,
114  const JTRIGGER::JHitR1& second)
115  {
116  return (first.getToT() + first.getW() > second.getToT() + second.getW());
117  }
118 
119  };
120 }
121 
122 #endif
123 
124 /**
125  * member function definition
126  */
127 
128 void
130  const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL1,
131  JFIT::JEvt &OutFits) const
132 {
133  const double Tmax_ns = parameters_.Tmax_ns; // [ns]
134  const double ctMin = parameters_.ctMin; // optimal value
135  const double Dmax_m = parameters_.Dmax_m; // [m] optimal value
137  const JTOOLS::JRange<double> time_grid = parameters_.time_grid;
138  const double pos_step = parameters_.pos_step; // [m] density of the grid
139  const double time_step = parameters_.time_step; // [ns] density of the grid
140  const double sigma_ns = parameters_.sigma_ns;
141  const int numberOfOutliers = parameters_.numberOfOutliers;
142  const bool useL0 = parameters_.useL0;
143 
144  const int FACTORY_LIMIT = 40;
145  const double STANDARD_DEVIATIONS = 1.0;
146  const int NUMBER_OF_L1HITS = (useL0 ? 1 : 4);
147 
148  typedef std::vector<JTRIGGER::JHitL0> JDataL0_t;
149  typedef std::vector<JTRIGGER::JHitL1> JDataL1_t;
150 
152  const JTRIGGER::JBuildL2<JTRIGGER::JHitL1> buildL2(JTRIGGER::JL2Parameters(2, Tmax_ns, ctMin));
153  const JTRIGGER::JMatch3G<JTRIGGER::JHitL1> match3G(Dmax_m, Tmax_ns); // CAUSALITY FOR SHOWERS
154  const JFIT::JHitL1Comparator compare;
155 
156  //define empty vectors to fill with JHitL0 and JHitL1
157  JDataL0_t dataL0;
158  JDataL0_t dataL0_selected;
159  JDataL1_t data;
160 
161  buildL0(timeSliceBuildL0, *moduleRouter_, back_inserter(dataL0));
162  buildL2(timeSliceBuildL1, *moduleRouter_, back_inserter(data));
163 
164  JDataL1_t::iterator __end = clusterizeWeight(data.begin(), data.end(), match3G);
165 
166  if(std::distance(data.begin(), __end) >= NUMBER_OF_L1HITS){
167 
168  JDataL1_t dataL1;
169  std::copy(data.begin(), data.end(), std::back_inserter(dataL1));
170  JDataL1_t::iterator __end1 = dataL1.end();
171 
172  __end1 = clusterizeWeight(dataL1.begin(), __end1, match3G);
173 
174  if (useL0) {
175 
176  dataL1.erase(__end1, dataL1.end());
177  dataL1.reserve(dataL1.size() + dataL0.size());
178  __end1 = dataL1.end();
179 
180  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
181 
182  const JTRIGGER::JHitL1 hit(*i);
183 
184  if (std::count_if(dataL1.begin(), __end1, JTRIGGER::JBind2nd(match3G,hit)) >= NUMBER_OF_L1HITS){
185  dataL1.push_back(hit);
186  }
187  }
188 
189  std::sort(__end1, dataL1.end(), compare);
190  __end1 = clusterize(__end1, dataL1.end(), match3G);
191 
192  }
193 
194  // 4D fit
196  JFIT::JPoint4D vx;
197  double chi2 = std::numeric_limits<double>::max();
198  int NDF = 0;
199  int N = 0;
200 
201  if(std::distance(dataL1.begin(), __end1) <= FACTORY_LIMIT){
202 
203  double ymin = std::numeric_limits<double>::max();
204 
205  JDataL1_t::iterator __end2 = __end1;
206 
207  for (int n = 0; n <= numberOfOutliers && std::distance(dataL1.begin(), __end2) >
209 
210  std::sort(dataL1.begin(), __end1, compare);
211 
212  do {
213  try {
214 
215  fit(dataL1.begin(), __end2);
216  double y = JFIT::getChi2(fit, dataL1.begin(), __end2, sigma_ns);
217 
218  if (y <= 0.0) {
219  std::cout << "chi2(1) " << y << std::endl; // <- it was used WARNING, which gives a bug.
220  } else if (y < ymin) {
221  ymin = y;
222  vx = fit;
224  N = JFIT::getCount(dataL1.begin(), __end2);
225  chi2 = y;
226  }
227  }
228  catch(JLANG::JException& error) {}
229 
230  } while (JTOOLS::next_permutation(dataL1.begin(), __end2, __end1, compare));
231 
232  ymin -= STANDARD_DEVIATIONS * STANDARD_DEVIATIONS;
233  }
234 
235  } else {
236 
237  try{
238  fit(dataL1.begin(), __end1);
239  NDF = std::distance(dataL1.begin(), __end1) - JEstimator<JFIT::JPoint4D>::NUMBER_OF_PARAMETERS;
240  N = JFIT::getCount(dataL1.begin(), __end1);
241  vx = fit;
242  chi2 = JFIT::getChi2(fit, dataL1.begin(), __end1, sigma_ns);
243  }
244  catch(const JLANG::JException& error){}
245  }
246 
247  if (NDF >= 0) {
248 
249  for(double x = pos_grid.getLowerLimit(); x <= pos_grid.getUpperLimit(); x += pos_step){
250  for(double y = pos_grid.getLowerLimit(); y <= pos_grid.getUpperLimit(); y += pos_step){
251  for(double z = pos_grid.getLowerLimit(); z <= pos_grid.getUpperLimit(); z += pos_step){
252  for(double t = time_grid.getLowerLimit(); t <= time_grid.getUpperLimit(); t += time_step){
253 
254  const JGEOMETRY3D::JVector3D pos_shifted(vx.getX() + x, vx.getY() + y, vx.getZ() + z);
255  const JFIT::JPoint4D point_shifted(pos_shifted, vx.getT() + t);
256 
257  dataL0_selected.clear();
258 
259  //select a subcluster of L0s
260  const JFIT::JModel<JFIT::JPoint4D> match(vx, 80, JTOOLS::JTimeRange(-100, +100));
261 
262  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
263 
264  const JHitL0 hit(*i);
265 
266  if(match(hit)){
267  dataL0_selected.push_back(hit);
268  }
269  }
270 
271  double simplex_step = 2; // [m]
272  fit2_->step.resize(4);
273  fit2_->step[0] = JFIT::JPoint4D(JVector3D(simplex_step, 0.0, 0.0), 0.0);
274  fit2_->step[1] = JFIT::JPoint4D(JVector3D(0.0, simplex_step, 0.0), 0.0);
275  fit2_->step[2] = JFIT::JPoint4D(JVector3D(0.0, 0.0, simplex_step), 0.0);
276  fit2_->step[3] = JFIT::JPoint4D(JVector3D(0.0, 0.0, 0.0), 20);
277 
278  chi2 = (*fit2_)(point_shifted, dataL0_selected.begin(), dataL0_selected.end());
279 
280  const JGEOMETRY3D::JVector3D fitPos(fit2_->value);
281  const JGEOMETRY3D::JTrack3D fitResult(fitPos, JGEOMETRY3D::JVersor3Z(), fit2_->value.getT());
282 
283  const double energy(0);
285  fitResult, getQuality(chi2, (useL0 ? N : NDF)),
286  NDF, energy,JFIT::JFitStatus_t::OKAY );
287 
288  OutFits.push_back(outFit);
289  OutFits.rbegin()->setW(13, chi2);
290  OutFits.rbegin()->setW(14, NDF);
291 
292  }
293  }
294  }
295  }
296  }
297  std::sort(OutFits.begin(), OutFits.end(), qualitySorter);
298  }else{
299  std::cout<<"Too few hits " << std::endl;// <- it was used DEBUG, which gives a bug.
300  }
301 
302 }
303 
JTRIGGER::JBind2nd
JBinder2nd< JHit_t > JBind2nd(const JMatch< JHit_t > &match, const JHit_t &second)
Auxiliary method to create JBinder2nd object.
Definition: JBind2nd.hh:70
JTOOLS::JRange::getLowerLimit
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:215
JFIT::JShowerPrefitParameters_t::useL0
bool useL0
Definition: JShowerPrefitParameters_t.hh:22
JTOOLS::JRange::getUpperLimit
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:226
JVector3D.hh
JFIT::JShowerPrefitParameters_t::Dmax_m
double Dmax_m
Definition: JShowerPrefitParameters_t.hh:26
JFIT::JORCAShowerPrefit::moduleRouter_
JLANG::JSharedPointer< const JDETECTOR::JModuleRouter > moduleRouter_
Definition: JORCAShowerPrefit.hh:61
JFIT::JHitL1Comparator
Auxiliary class for permutations of L1 hits.
Definition: JEvtToolkit.hh:501
JFIT::JShowerPrefitParameters_t
Definition: JShowerPrefitParameters_t.hh:18
JFIT
Auxiliary classes and methods for linear and iterative data regression.
Definition: JEnergy.hh:15
JTOOLS::next_permutation
bool next_permutation(T __begin, T __last, T __end, JComparator_t compare, std::bidirectional_iterator_tag)
Implementation of method next_permutation for bidirectional iterators.
Definition: JPermutation.hh:20
JTRIGGER::JL2Parameters
Data structure for L2 parameters.
Definition: JTriggerParameters.hh:33
JFitToolkit.hh
JGEOMETRY3D::JVersor3Z
Data structure for normalised vector in positive z-direction.
Definition: JVersor3Z.hh:37
JPoint4D.hh
JFitStatus.hh
JTimeslice.hh
JGEOMETRY3D::JVector3D::getZ
double getZ() const
Get z position.
Definition: JVector3D.hh:114
JTOOLS::n
const int n
Definition: JPolint.hh:628
JSharedPointer.hh
JSimplex.hh
std::vector
Definition: JSTDTypes.hh:12
JPoint3DEstimator.hh
JTRIGGER::clusterize
JHitIterator_t clusterize(JHitIterator_t __begin, JHitIterator_t __end, const JMatch< JHit_t > &match, const int Nmin=1)
Partition data according given binary match operator.
Definition: JAlgorithm.hh:45
JTOOLS::JRange< double >
JPoint4DEstimator.hh
JEvtToolkit.hh
KM3NETDAQ::JDAQTimeslice
Data time slice.
Definition: JDAQTimeslice.hh:30
JFIT::JORCAShowerPrefit::fit2_
std::shared_ptr< JRegressor_t > fit2_
Definition: JORCAShowerPrefit.hh:59
distance
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Definition: PhysicsEvent.hh:434
JPoint4DRegressor.hh
JDAQTimeslice.hh
JFIT::JORCAShowerPrefit
class to handle first step of the shower reconstruction, mainly dedicated for ORCA
Definition: JORCAShowerPrefit.hh:55
JFIT::JEvt
Data structure for set of track fit results.
Definition: JEvt.hh:293
JFIT::JShowerPrefitParameters_t::time_grid
JTOOLS::JRange< double > time_grid
Definition: JShowerPrefitParameters_t.hh:28
JAANET::copy
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:152
JDAQSummaryslice.hh
JShowerParameters.hh
JEvt.hh
JFIT::JModel
Auxiliary class to match data points with given model.
Definition: JModel.hh:27
JTRIGGER::JBuildL0
Template L0 hit builder.
Definition: JBuildL0.hh:34
JRange.hh
JBuildL0.hh
JGEOMETRY3D::JVector3D
Data structure for vector in three dimensions.
Definition: JVector3D.hh:33
JFIT::qualitySorter
bool qualitySorter(const JFit &first, const JFit &second)
Comparison of fit results.
Definition: JEvtToolkit.hh:223
JMatch3G.hh
JEstimator.hh
JTRIGGER::clusterizeWeight
JHitIterator_t clusterizeWeight(JHitIterator_t __begin, JHitIterator_t __end, const JMatch< JHit_t > &match)
Partition data according given binary match operator.
Definition: JAlgorithm.hh:310
JFIT::JORCAShowerPrefit::~JORCAShowerPrefit
~JORCAShowerPrefit()
Definition: JORCAShowerPrefit.hh:90
JFIT::JShowerPrefitParameters_t::pos_step
double pos_step
Definition: JShowerPrefitParameters_t.hh:29
JHit.hh
JFIT::JEstimator
Template definition of linear fit.
Definition: JEstimator.hh:25
JBind2nd.hh
JHitR1.hh
JFIT::JShowerPrefitParameters_t::time_step
double time_step
Definition: JShowerPrefitParameters_t.hh:30
JTRIGGER::JMatch3G
3G match criterion.
Definition: JMatch3G.hh:29
JBuildL2.hh
JFIT::JPoint4D
Data structure for vertex fit.
Definition: JPoint4D.hh:22
JFIT::JRegressor< JPoint4D, JSimplex >
Regressor function object for JPoint4D fit using JSimplex minimiser.
Definition: JPoint4DRegressor.hh:47
JFIT::JORCAShowerPrefit::JRegressor_t
JFIT::JRegressor< JPoint4D, JSimplex > JRegressor_t
Definition: JORCAShowerPrefit.hh:58
JHitL1.hh
JModel.hh
JTRIGGER::JHitR1
Reduced data structure for L1 hit.
Definition: JHitR1.hh:31
JFIT::JShowerPrefitParameters_t::ctMin
double ctMin
Definition: JShowerPrefitParameters_t.hh:25
JFIT::getChi2
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:80
JFIT::JORCAShowerPrefit::getJEvt
void getJEvt(const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL0, const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL2, JFIT::JEvt &OutFits) const
Declaration of Member function that actually performs the reconstruction.
Definition: JORCAShowerPrefit.hh:129
JTRIGGER::JHitL0
Data structure for L0 hit.
Definition: JHitL0.hh:27
JFIT::JShowerPrefitParameters_t::numberOfOutliers
int numberOfOutliers
Definition: JShowerPrefitParameters_t.hh:23
JGEOMETRY3D::JVector3D::getY
double getY() const
Get y position.
Definition: JVector3D.hh:103
JGEOMETRY3D::JTrack3D
3D track.
Definition: JTrack3D.hh:30
JFIT::JORCAShowerPrefit::JORCAShowerPrefit
JORCAShowerPrefit()
Default constructor.
Definition: JORCAShowerPrefit.hh:69
JFIT::JORCAShowerPrefit::JORCAShowerPrefit
JORCAShowerPrefit(const JLANG::JSharedPointer< const JDETECTOR::JModuleRouter > &moduleRouter, const JFIT::JShowerPrefitParameters_t &parameters)
Parameterized constructor.
Definition: JORCAShowerPrefit.hh:79
JFIT::JShowerPrefitParameters_t::Tmax_ns
double Tmax_ns
Definition: JShowerPrefitParameters_t.hh:24
JFIT::JShowerPrefitParameters_t::pos_grid
JTOOLS::JRange< double > pos_grid
Definition: JShowerPrefitParameters_t.hh:27
JSHOWERPREFIT
static const int JSHOWERPREFIT
Definition: reconstruction.hh:23
JTRIGGER::JHitR1::getW
double getW() const
Get weight.
Definition: JHitR1.hh:194
JFIT::getFit
JFit getFit(const JHistory &history, const JTrack3D &track, const double Q, const int NDF, const double energy=0.0, const int status=0)
Get fit.
Definition: JEvtToolkit.hh:116
JFIT::JHistory
Container for historical events.
Definition: JHistory.hh:95
JGEOMETRY3D::JVector3D::getX
double getX() const
Get x position.
Definition: JVector3D.hh:93
JEEP::JMessage< JSimplex< JPoint4D > >::debug
static int debug
debug level (default is off).
Definition: JMessage.hh:45
JHitL0.hh
JGEOMETRY3D::JVertex3D::getT
double getT(const JVector3D &pos) const
Get arrival time of Cherenkov light at given position.
Definition: JVertex3D.hh:144
JFIT::getQuality
double getQuality(const double chi2, const int NDF)
Get quality of fit.
Definition: JEvtToolkit.hh:195
JFIT::JORCAShowerPrefit::totW
static bool totW(const JTRIGGER::JHitR1 &first, const JTRIGGER::JHitR1 &second)
Member function used in the predicate of std::sort, to sort hits according to ToT and W.
Definition: JORCAShowerPrefit.hh:113
JFIT::JORCAShowerPrefit::parameters_
JFIT::JShowerPrefitParameters_t parameters_
Definition: JORCAShowerPrefit.hh:62
JLANG::JException
General exception.
Definition: JException.hh:23
JTRIGGER::JHitL1
Data structure for L1 hit.
Definition: JHitL1.hh:34
JFIT::JSimplex< JPoint4D >::MAXIMUM_ITERATIONS
static int MAXIMUM_ITERATIONS
maximal number of iterations
Definition: JSimplex.hh:226
JTRIGGER::JHit::getToT
double getToT() const
Get calibrated time over threshold of hit.
Definition: JHit.hh:157
JFIT::getCount
int getCount(const JHitL0 &hit)
Get hit count.
Definition: JEvtToolkit.hh:728
JLANG::JSharedPointer< const JDETECTOR::JModuleRouter >
JFIT::JShowerPrefitParameters_t::sigma_ns
double sigma_ns
Definition: JShowerPrefitParameters_t.hh:21
JFIT::JFit
Data structure for track fit results.
Definition: JEvt.hh:31
JFIT::OKAY
Definition: JFitStatus.hh:22
JTRIGGER::JBuildL2
Template L2 builder.
Definition: JBuildL2.hh:45
JAlgorithm.hh