Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JShowerFit.cc File Reference

Program to perform EM Shower Fit for ORCA with I/O of JFIT::JEvt data. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include "evt/Head.hh"
#include "evt/Det.hh"
#include "JDAQ/JDAQEvent.hh"
#include "JDAQ/JDAQTimeslice.hh"
#include "JDAQ/JDAQSummaryslice.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JModuleRouter.hh"
#include "JTrigger/JHit.hh"
#include "JTrigger/JFrame.hh"
#include "JTrigger/JTimeslice.hh"
#include "JTrigger/JHitL0.hh"
#include "JTrigger/JBuildL0.hh"
#include "JTrigger/JTriggerParameters.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JParallelFileScanner.hh"
#include "JSupport/JTreeScanner.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JSupportToolkit.hh"
#include "JSupport/JSupport.hh"
#include "JSupport/JMeta.hh"
#include "JFit/JHitW0.hh"
#include "JFit/JPMTW0.hh"
#include "JFit/JGandalf.hh"
#include "JFit/JFitToolkit.hh"
#include "JFit/JEvt.hh"
#include "JFit/JEvtToolkit.hh"
#include "JFit/JFitParameters.hh"
#include "JFit/JShower3EZ.hh"
#include "JFit/JRegressor.hh"
#include "JFit/JShower3EZRegressor.hh"
#include "JTools/JConstants.hh"
#include "JTools/JFunction1D_t.hh"
#include "JTools/JFunctionalMap_t.hh"
#include "JPhysics/JPDFTable.hh"
#include "JPhysics/JPDFToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include <JGeometry3D/JOmega3D.hh>
#include "JAAnet/JAAnetToolkit.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to perform EM Shower Fit for ORCA with I/O of JFIT::JEvt data.

The reconstruction is made at the PMT level.

Author
adomi

Definition in file JShowerFit.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 67 of file JShowerFit.cc.

67  {
68 
69  using namespace std;
70  using namespace JFIT;
71  using namespace JSUPPORT;
72  using namespace KM3NETDAQ;
73  using namespace JAANET;
74 
75  typedef JParallelFileScanner< JTypeList<JDAQEvent, JEvt> > JParallelFileScanner_t;
76  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
78 
79  JParallelFileScanner_t inputFile;
81  JLimit_t& numberOfEvents = inputFile.getLimit();
82  string detectorFile;
83  string pdfFile;
84  double Tmax_ns;
85  double Dmax_m;
86  double R_Hz;
87  size_t numberOfPrefits;
88  int debug;
89 
90 
91  try {
92 
93  JParser<> zap;
94 
95  zap['f'] = make_field(inputFile);
96  zap['o'] = make_field(outputFile) = "gandalf.root";
97  zap['a'] = make_field(detectorFile);
98  zap['F'] = make_field(pdfFile) = "quickPDF_elec-CC.root";
99  zap['n'] = make_field(numberOfEvents) = JLimit::max();
100  zap['N'] = make_field(Tmax_ns) = 20; // [ns]
101  zap['D'] = make_field(Dmax_m) = 80; // [m]
102  zap['B'] = make_field(R_Hz) = 10.0e3;
103  zap['R'] = make_field(numberOfPrefits) = 1;
104  zap['d'] = make_field(debug) = 2;
105 
106  zap(argc, argv);
107  }
108  catch(const exception& error) {
109  FATAL(error.what() << endl);
110  }
111 
112  using namespace JTRIGGER;
113  using namespace JDETECTOR;
114  using namespace JGEOMETRY3D;
115  using namespace JTOOLS;
116 
118 
119  try {
120  load(detectorFile, detector);
121  }
122  catch(const JException& error) {
123  FATAL(error);
124  }
125 
126  const JModuleRouter moduleRouter(detector);
127 
128  typedef vector<JHitL0> JDataL0_t;
129  typedef vector<JPMTW0> JPMTW0_t;
130  const JBuildL0<JHitL0> buildL0;
131 
132  typedef JRegressor<JShower3EZ, JSimplex> JRegressor_t;
133 
135  JRegressor_t::T_ns.setRange(-Tmax_ns, Tmax_ns);
136  JRegressor_t::Vmax_npe = 20.0;
137  JRegressor_t::MAXIMUM_ITERATIONS = 1000;
138  JRegressor_t fit(pdfFile);
139 
140  outputFile.open();
141  outputFile.put(JMeta(argc, argv));
142 
143  JTreeScanner<Evt> in(inputFile);
144 
145  while (inputFile.hasNext()) {
146 
147  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
148 
149  multi_pointer_type ps = inputFile.next();
150 
151  JDAQEvent* tev = ps;
152  JEvt* evt = ps;
153  JEvt out;
154 
155  if(!evt->empty()) {
156 
157  JEvt::iterator __end = evt->end();
158 
159  copy(evt->begin(), __end, back_inserter(out));
160 
161  if (numberOfPrefits > 0) {
162  advance(__end = evt->begin(), min(numberOfPrefits, evt->size()));
163  }
164 
165  partial_sort(evt->begin(), __end, evt->end(), qualitySorter);
166 
167  JDataL0_t dataL0;
168  JDAQTimeslice timeslice(*tev, true);
169 
170  buildL0(*tev, moduleRouter, true, back_inserter(dataL0));
171 
172  for (JEvt::const_iterator shower = evt->begin(); shower != __end; ++shower) {
173 
174  JVector3D pos(getPosition(*shower)); //.rotate(R));
175 
176  JPoint4D pt(pos, shower->getT());
177  JVector3D start_dir(0,0,0);
178 
179  JVector3D DetC(0.0, 0.0, 117);
180  double radius = pos.getDistance(DetC);
181 
183 
184  double Nhits = 0;
185 
186  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
187 
188  JHitW0 hit(*i, R_Hz);
189  JPosition3D hit_pos(hit.getPosition());
190  double D = hit_pos.getDistance(pt.getPosition());
191  double t_res = hit.getT() - pt.getT(hit_pos);
192  JDirection3D photonDir(hit_pos - pt.getPosition());
193  const double cosT = photonDir.getDot(hit.getDirection());
194 
195  if(D < Dmax_m && (t_res >= -25 && t_res <= 25) && (cosT >= -1 && cosT <= 0.1)){
196  top.insert(hit.getPMTIdentifier());
197  JVector3D d(photonDir.getDX(), photonDir.getDY(), photonDir.getDZ());
198  start_dir.add(d);
199  }
200  if(D < Dmax_m && (t_res > -30 && t_res < 20)){
201  Nhits++;
202  }
203  }
204 
205  double start_E = exp((Nhits + 26)/26); // starting energy [GeV]
206  if(start_E > 100) start_E = 100;
207 
208  vector<double> LogLik, Energies;
209  vector<JVector3D> start_directions;
210  vector<JPMTW0> buffer;
211  double chi2 = 0.0;
212  double max_theta = 0, max_phi = 0, scan_step = 0;
213  double Nmin = 0;
214 
215  if(start_E > 10 && radius < 100){
216  max_theta = 30;
217  max_phi = 30;
218  scan_step = 5;
219  Nmin = 10;
220  } else {
221  max_theta = 35;
222  max_phi = 35;
223  scan_step = 5;
224  Nmin = 20;
225  }
226 
227  JAngle3D start_dir_angles(start_dir.getX(), start_dir.getY(), start_dir.getZ());
228 
229  for(double E = -15; E <= 5; E += 7.5){ // scan in energy
230  for(double th = -max_theta; th <= max_theta; th += scan_step){
231  for(double ph = -max_phi; ph <= max_phi; ph += scan_step){
232 
233  buffer.clear();
234  chi2 = 0.0;
235 
236  double theta = th * 3.14 / 180;
237  double phi = ph * 3.14 / 180;
238  double scan_E = start_E + E; // [GeV]
239 
240  JAngle3D dir_shifted_angles(start_dir_angles.getTheta() + theta, start_dir_angles.getPhi() + phi);
241  JVector3D dir_shifted(dir_shifted_angles.getDX(), dir_shifted_angles.getDY(), dir_shifted_angles.getDZ());
242  JDirection3D conversion(dir_shifted.getX(), dir_shifted.getY(), dir_shifted.getZ());
243  JRotation3D R(conversion);
244  pt.rotate(R);
245 
246  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
247  JPosition3D pos(module->getPosition());
248  pos.rotate(R);
249  if (pt.getDistance(pos) < Dmax_m) {
250  for (unsigned int i = 0; i != module->size(); ++i) {
251  JDAQPMTIdentifier id(module->getID(), i);
252  JPMT pmt(module->getPMT(i));
253  pmt.rotate(R);
254  buffer.push_back(JPMTW0(pmt, R_Hz, top.count(id)));
255  }
256  }
257  }
258 
259  for(JPMTW0_t::iterator pmt = buffer.begin(); pmt != buffer.end(); ++pmt ){
260  chi2 += fit(JShower3EZ(pt, JVersor3Z(), JEnergy(log10(scan_E))), *pmt);
261  }
262 
263  LogLik.push_back(chi2);
264  start_directions.push_back(dir_shifted);
265  Energies.push_back(scan_E);
266  pt.rotate_back(R);
267 
268  }
269  }
270  }
271 
272  while(LogLik.size() > Nmin){
273 
275  it = max_element(LogLik.begin(), LogLik.end());
276  int pos = it - LogLik.begin();
277  LogLik.erase(LogLik.begin() + pos);
278  start_directions.erase(start_directions.begin() + pos);
279  Energies.erase(Energies.begin() + pos);
280 
281  }
282 
283  int n = 0;
284  for(vector<JVector3D>::iterator dir = start_directions.begin(); dir != start_directions.end(); ++dir){
285 
286  fit.step.resize(3);
287  double dir_step = 0.05;
288  fit.step[0] = JShower3EZ(JPoint4D(JVector3D(), 0.0), JVersor3Z(dir_step, 0.0), JEnergy());
289  fit.step[1] = JShower3EZ(JPoint4D(JVector3D(), 0.0), JVersor3Z(0.0, dir_step), JEnergy());
290  fit.step[2] = JShower3EZ(JPoint4D(JVector3D(), 0.0), JVersor3Z(), 0.05);
291 
292  double scan_E = Energies[n];
293  n++;
294  buffer.clear();
295 
296  JVersor3D startVersor(dir->getX(), dir->getY(), dir->getZ());
297 
298  JDirection3D conversion(startVersor.getDX(), startVersor.getDY(), startVersor.getDZ());
299  JRotation3D R(conversion);
300  pt.rotate(R);
301 
302  for(JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
303  JPosition3D pos(module->getPosition());
304  pos.rotate(R);
305  if (pt.getDistance(pos) < Dmax_m) {
306  for (unsigned int i = 0; i != module->size(); ++i) {
307  JDAQPMTIdentifier id(module->getID(), i);
308  JPMT pmt(module->getPMT(i));
309  pmt.rotate(R);
310  buffer.push_back(JPMTW0(pmt, R_Hz, top.count(id)));
311  }
312  }
313  }
314 
315  int NDF = buffer.size() - fit.step.size();
316 
317  chi2 = fit(JShower3EZ(pt, JVersor3Z(), JEnergy(log10(scan_E))), buffer.begin(), buffer.end());
318 
319  pt.rotate_back(R);
320 
321  JVector3D resPos(fit.value);
322  JVersor3D resDir(fit.value.getDX(), fit.value.getDY(), fit.value.getDZ());
323 
324  JTrack3D td(resPos, resDir, shower->getT());
325  double E_reco_corrected = fit.value.getE() - 4;
326  JTrack3E tb(td, E_reco_corrected);
327  tb.rotate_back(R);
328 
329  out.push_back(getFit(JHistory(shower->getHistory()).add(JSHOWERCOMPLETEFIT), tb, -chi2, NDF));
330  out.rbegin()->setE(E_reco_corrected);
331  }
332  }
333  sort(out.begin(), out.end(), qualitySorter);
334  }
335  outputFile.put(out);
336  }
337  STATUS(endl);
338 
340  io >> outputFile;
341  outputFile.close();
342 
343 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:71
Data structure for angles in three dimensions.
Definition: JAngle3D.hh:30
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1410
Data structure for direction in three dimensions.
Definition: JDirection3D.hh:32
Template specialisation of L0 builder for JHitL0 data type.
Definition: JBuildL0.hh:102
Auxiliary class for handling PMT geometry, rate and response.
Definition: JPMTW0.hh:22
Data structure for vertex fit.
Definition: JPoint4D.hh:22
#define STATUS(A)
Definition: JMessage.hh:61
Detector data structure.
Definition: JDetector.hh:77
Router for direct addressing of module data in detector data structure.
Rotation matrix.
Definition: JRotation3D.hh:108
General purpose class for parallel reading of objects from a single file or multiple files...
Container for historical events.
Definition: JHistory.hh:95
string outputFile
double getDistance(const JVector3D &pos) const
Get distance to point.
Definition: JVector3D.hh:266
3D track with energy.
Definition: JTrack3E.hh:29
double getDot(const JAngle3D &angle) const
Get dot product.
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
Type list.
Definition: JTypeList.hh:22
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
Data structure for fit of straight line in positive z-direction with energy.
Definition: JShower3EZ.hh:25
JAxis3D & rotate(const JRotation3D &R)
Rotate axis.
Definition: JAxis3D.hh:235
Detector file.
Definition: JHead.hh:126
Data structure for vector in three dimensions.
Definition: JVector3D.hh:32
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
Data structure for PMT geometry and calibration.
Definition: JPMT.hh:52
Auxiliary class for a hit with background rate value.
Definition: JHitW0.hh:21
Data time slice.
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
int debug
debug level
Definition: JSirene.cc:59
Regressor function object for JShower3EZ fit using JSimplex minimiser.
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
Definition: JCounter.hh:35
#define FATAL(A)
Definition: JMessage.hh:65
Data structure for set of track fit results.
Definition: JEvt.hh:312
General purpose class for object reading from a list of file names.
bool qualitySorter(const JFIT::JFit &first, const JFIT::JFit &second)
Comparison of fit results.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:40
Data structure for fit of energy.
Definition: JEnergy.hh:24
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:72
Data structure for normalised vector in three dimensions.
Definition: JVersor3D.hh:23
Data structure for normalised vector in positive z-direction.
Definition: JVersor3Z.hh:36
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:140
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
JPosition3D getPosition(const Vec &v)
Get position.