Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JShowerPrefit.hh
Go to the documentation of this file.
1 #ifndef JSHOWERPREFIT_INCLUDE
2 #define JSHOWERPREFIT_INCLUDE
3 
4 #include <iostream>
5 #include <vector>
6 #include <algorithm>
7 #include <functional>
8 
11 
12 #include "JTrigger/JHit.hh"
13 #include "JTrigger/JHitR1.hh"
14 #include "JTrigger/JBuildL0.hh"
15 #include "JTrigger/JBuildL2.hh"
16 #include "JTrigger/JAlgorithm.hh"
17 #include "JTrigger/JMatch3G.hh"
18 #include "JTrigger/JBind2nd.hh"
19 
20 #include "JFit/JFitToolkit.hh"
22 #include "JFit/JEstimator.hh"
23 #include "JFit/JPoint4D.hh"
24 
26 
27 #include "JReconstruction/JEvt.hh"
30 
31 #include "JLang/JComparator.hh"
32 
33 #include "JGeometry3D/JTrack3D.hh"
34 
35 /**
36  * \author adomi, gmaggi
37  */
38 
39 namespace JRECONSTRUCTION
40 {
41 
44 
45  /**
46  * class to handle first step of the shower reconstruction in ORCA:
47  * it reconstructs the shower vertex, intended as the shower brightest point, as the barycenter of the hits
48  */
49 
51  {
52 
53  public:
54 
55  /**
56  * Parameterized constructor
57  *
58  * \param parameters struct that holds default-optimized parameters for the reconstruction.
59  * \param router module router, this is built via detector file.
60  * \param debug debug
61  */
62 
64  const JModuleRouter& router,
65  const int debug = 0):
66  JShowerPrefitParameters_t(parameters),
67  router(router)
68  {}
69 
70  /**
71  * Declaration of the operator that performs the reconstruction
72  *
73  * \param event which is a JDAQEvent
74  */
75 
76  JEvt operator()(const KM3NETDAQ::JDAQEvent &event) const
77  {
78 
79  using namespace std;
80  using namespace JPP;
81 
82  const double STANDARD_DEVIATIONS = 3.0;
83 
84  typedef JHitR1 hit_type;
85  typedef vector<hit_type> buffer_type;
86 
87  typedef JEstimator<JPoint4D> JEstimator_t;
88 
89  const JBuildL0<hit_type> buildL0;
91  const JMatch3G<hit_type> match3G(roadWidth_m, TMaxLocal_ns); // causality relation for showers
92  const JHitL1Comparator compare;
93 
94  //define empty vectors to be filled with JHitL0 and JHitL1
95  buffer_type dataL0;
96  buffer_type dataL1;
97 
98  JEvt out;
99 
100  buffer_type buffer;
101 
102  buildL0(JDAQTimeslice(event, true), router, back_inserter(buffer)); // true = snapshot
103  buildL2(JDAQTimeslice(event, false), router, back_inserter(dataL1)); // false = triggered
104 
105  copy(dataL1.begin(), dataL1.end(), back_inserter(dataL0));
106 
107  for (buffer_type::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
108 
109  if (find_if(dataL1.begin(), dataL1.end(), match_t(*i, TMaxLocal_ns)) == dataL1.end()) {
110  dataL0.push_back(*i);
111  }
112  }
113 
114  for (buffer_type::const_iterator root = dataL1.begin(); root != dataL1.end(); ++root) {
115 
116  buffer_type data(1, *root);
117 
118  JBinder2nd<hit_type> matching = JBind2nd(match3G, *root);
119 
120  for (buffer_type::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
121 
122  if(( root->getModuleIdentifier() != i->getModuleIdentifier() ) && matching(*i)){
123  data.push_back(*i);
124  }
125  }
126 
127  buffer_type::iterator __end1 = clusterizeWeight(data.begin() + 1, data.end(), match3G);
128 
129  // 4D fit
130  JEstimator_t fit;
131  JPoint4D vx;
132  double chi2 = numeric_limits<double>::max();
133  int NDF = distance(data.begin(), __end1) - JEstimator_t::NUMBER_OF_PARAMETERS;
134  int N = getCount(data.begin(), __end1);
135 
136  if(distance(data.begin(), __end1) <= factoryLimit){
137 
138  double ymin = numeric_limits<double>::max();
139 
140  buffer_type::iterator __end2 = __end1;
141 
142  for (int n = 0; n <= numberOfOutliers && distance(data.begin(), __end2) >
143  JEstimator_t::NUMBER_OF_PARAMETERS; ++n, --__end2) {
144 
145  sort(data.begin() + 1, __end1, compare);
146 
147  do {
148  try {
149 
150  fit(data.begin(), __end2);
151 
152  double y = getChi2(fit, data.begin(), __end2, sigma_ns);
153 
154  if (y < ymin) {
155  ymin = y;
156  vx = fit;
157  chi2 = ymin;
158  NDF = distance(data.begin(), __end2) - JEstimator_t::NUMBER_OF_PARAMETERS;
159  N = getCount(data.begin(), __end2);
160  }
161  }
162  catch(JException& error) { }
163 
164  } while (next_permutation(data.begin() + 1, __end2, __end1, compare));
165 
166  ymin -= STANDARD_DEVIATIONS * STANDARD_DEVIATIONS;
167  }
168 
169  } else {
170 
171  const int number_of_outliers = distance(data.begin(), __end1) - JEstimator_t::NUMBER_OF_PARAMETERS - 1;
172 
173  buffer_type::iterator __end2 = __end1;
174 
175  for (int n = 0; n <= number_of_outliers; ++n) {
176 
177  try{
178 
179  fit(data.begin(), __end2);
180  vx = fit;
181  chi2 = getChi2(fit, data.begin(), __end2, sigma_ns);
182  NDF = distance(data.begin(), __end2) - JEstimator_t::NUMBER_OF_PARAMETERS;
183  N = getCount(data.begin(), __end2);
184 
185  }
186  catch(const JException& error){ }
187 
188  double ymax = 0;
189  buffer_type::iterator imax = __end2;
190 
191  for (buffer_type::iterator i = data.begin() + 1; i != __end2; ++i) {
192 
193  double y = getChi2(fit, *i, sigma_ns);
194 
195  if (y > ymax) {
196  ymax = y;
197  imax = i;
198  }
199  }
200 
201  if (ymax > STANDARD_DEVIATIONS * STANDARD_DEVIATIONS) {
202  --__end2;
203  swap(*imax, *__end2);
204  } else {
205  break;
206  }
207  }
208  }
209 
210  if (NDF >= 0) {
211 
212  const JGEOMETRY3D::JTrack3D fitResult(vx, JGEOMETRY3D::JVersor3Z());
213 
214  out.push_back(getFit(JHistory(JSHOWERPREFIT), fitResult, getQuality(chi2, N), NDF));
215 
216  out.rbegin()->setW(13, chi2);
217  out.rbegin()->setW(14, N);
218  }
219  }
220 
221  return out;
222 
223  }
224 
226 
227  struct match_t {
228 
229  match_t(const JHitR1& root, const double TMax_ns) :
230  root(root),
231  TMax_ns(TMax_ns)
232  {}
233 
234  bool operator()(const JHitR1& hit) const
235  {
236  return root.getModuleID() == hit.getModuleID() && fabs(root.getT() - hit.getT()) <= TMax_ns;
237  }
238 
239  const JHitR1& root;
240  double TMax_ns;
241  };
242 
243  };
244 }
245 
246 #endif
247 
Auxiliary methods to evaluate Poisson probabilities and chi2.
Linear fit methods.
double getT() const
Get calibrated time of hit.
General exception.
Definition: JException.hh:23
3G match criterion.
Definition: JMatch3G.hh:29
int getModuleID() const
Get module identifier.
JBinder2nd< JHit_t > JBind2nd(const JMatch< JHit_t > &match, const JHit_t &second)
Auxiliary method to create JBinder2nd object.
Definition: JBind2nd.hh:70
Linear fit of bright point (position and time) between hits (objects with position and time)...
Algorithms for hit clustering and sorting.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Data structure for vertex fit.
Definition: JPoint4D.hh:22
then JPlot1D f $WORKDIR postfit[prefit\] root
Router for direct addressing of module data in detector data structure.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Auxiliary class to convert binary JMatch operator and given hit to unary match operator.
Definition: JBind2nd.hh:26
class to handle first step of the shower reconstruction in ORCA: it reconstructs the shower vertex...
Basic data structure for time and time over threshold information of hit.
double getQuality(const double chi2, const int N, const int NDF)
Get quality of fit.
Definition: JEvtToolkit.hh:206
JShowerPrefit(const JShowerPrefitParameters_t &parameters, const JModuleRouter &router, const int debug=0)
Parameterized constructor.
JEvt operator()(const KM3NETDAQ::JDAQEvent &event) const
Declaration of the operator that performs the reconstruction.
static struct JTRIGGER::@71 clusterizeWeight
Anonymous struct for weighed clustering of hits.
bool operator()(const JHitR1 &hit) const
static const int JSHOWERPREFIT
Template L2 builder.
Definition: JBuildL2.hh:45
The template JSharedPointer class can be used to share a pointer to an object.
const JModuleRouter & router
Data time slice.
int debug
debug level
Definition: JSirene.cc:61
Direct access to module in detector data structure.
Data structure for L2 parameters.
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:126
Reduced data structure for L1 hit.
Linear fit of JFIT::JPoint4D.
Auxiliary class for permutations of L1 hits.
Definition: JEvtToolkit.hh:554
JFIT::JHistory JHistory
Definition: JHistory.hh:283
Data structure for set of track fit results.
Definition: JEvt.hh:294
alias put_queue eval echo n
Definition: qlib.csh:19
int getCount(const T &hit)
Get hit count.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:153
match_t(const JHitR1 &root, const double TMax_ns)
Reduced data structure for L1 hit.
Definition: JHitR1.hh:31
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
Template L0 hit builder.
Definition: JBuildL0.hh:35
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:79
Data structure for normalised vector in positive z-direction.
Definition: JVersor3Z.hh:37
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
Match operator for Cherenkov light from shower in any direction.