Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JConvertEvt.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 #include <algorithm>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 
13 
14 #include "JDetector/JDetector.hh"
16 #include "JDetector/JPMTRouter.hh"
20 
21 #include "JDynamics/JDynamics.hh"
22 
23 #include "JCompass/JEvt.hh"
24 #include "JCompass/JSupport.hh"
25 
26 #include "JAcoustics/JEvt.hh"
27 #include "JAcoustics/JSupport.hh"
28 
29 #include "JTrigger/JHit.hh"
30 #include "JTrigger/JHitToolkit.hh"
31 
32 #include "JAAnet/JHead.hh"
33 #include "JAAnet/JHeadToolkit.hh"
34 #include "JAAnet/JAAnetToolkit.hh"
35 
36 #include "JDAQ/JDAQEventIO.hh"
38 
40 #include "JSupport/JTreeScanner.hh"
44 #include "JSupport/JSupport.hh"
45 #include "JSupport/JMeta.hh"
46 
47 #include "JReconstruction/JEvt.hh"
49 
50 #include "Jeep/JeepToolkit.hh"
51 #include "Jeep/JParser.hh"
52 #include "Jeep/JMessage.hh"
53 
54 
55 /**
56  * \file
57  * Auxiliary program to convert fit results to AAnet format.
58  *
59  * \author mdejong
60  */
61 int main(int argc, char **argv)
62 {
63  using namespace std;
64  using namespace JPP;
65  using namespace KM3NETDAQ;
66 
68  JACOUSTICS::JEvt>::typelist calibration_types;
69  typedef JMultipleFileScanner<calibration_types> JCalibration_t;
70 
71  JMultipleFileScanner<> inputFile;
73  JLimit_t& numberOfEvents = inputFile.getLimit();
74  string detectorFile;
75  JCalibration_t calibrationFile;
76  double Tmax_s;
77  JPMTParametersMap pmtParameters;
79  size_t numberOfFits;
80  int debug;
81 
82  try {
83 
84  JParser<> zap("Auxiliary program to convert fit results to AAnet format.\
85  \nThe option -L corresponds to the name of a shared library \
86  \nand function so to rearrange the order of fit results.");
87 
88  zap['f'] = make_field(inputFile);
89  zap['o'] = make_field(outputFile) = "aanet.root";
90  zap['n'] = make_field(numberOfEvents) = JLimit::max();
91  zap['a'] = make_field(detectorFile) = "";
92  zap['+'] = make_field(calibrationFile) = JPARSER::initialised();
93  zap['T'] = make_field(Tmax_s) = 100.0;
94  zap['P'] = make_field(pmtParameters, "PMT simulation data (or corresponding file name)") = JPARSER::initialised();
96  zap['N'] = make_field(numberOfFits) = 0;
97  zap['d'] = make_field(debug) = 2;
98 
99  zap(argc, argv);
100  }
101  catch(const exception& error) {
102  FATAL(error.what() << endl);
103  }
104 
105 
106  typedef JParallelFileScanner< JTypeList<JDAQEvent, JFIT::JEvt> > JParallelFileScanner_t;
107  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
108 
109 
111 
112  if (detectorFile != "") {
113  try {
114  load(detectorFile, detector);
115  }
116  catch(const JException& error) {
117  FATAL(error);
118  }
119  }
120 
121  getMechanics.load(detector.getID());
122 
123  JDynamics dynamics(detector, Tmax_s);
124 
125  dynamics.load(calibrationFile);
126 
127  const JPMTRouter pmt_router(dynamics);
128  const JModuleRouter mod_router(dynamics);
129 
130 
131  outputFile.open();
132 
133  Head header;
134 
135  try {
136  header = getHeader(inputFile);
137  } catch(const exception& error) {}
138  {
139  JAANET::JHead buffer(header);
140 
141  buffer.DAQ.livetime_s = getLivetime(inputFile.begin(), inputFile.end());
142  buffer.push(&JAANET::JHead::DAQ);
143 
144  copy(buffer, header);
145  }
146 
147  outputFile.put(header);
148 
149 
150  outputFile.put(JMeta(argc, argv));
151 
152 
153  counter_type number_of_events = 0;
154 
155  for (JMultipleFileScanner<>::const_iterator i = inputFile.begin(); i != inputFile.end(); ++i) {
156 
157  STATUS("Processing: " << *i << endl);
158 
159  JParallelFileScanner_t in(*i);
160  JTreeScanner<Evt> mc(*i);
161 
162  in.setLimit(inputFile.getLimit());
163 
164  Vec center(0,0,0);
165 
166  int mc_run_id = 0;
167 
168  try {
169 
170  const JAANET::JHead head(getHeader(*i));
171 
172  center = get<Vec>(head);
173  mc_run_id = head.start_run.run_id;
174 
175  } catch(const exception& error) {}
176 
177 
178  while (in.hasNext()) {
179 
180  STATUS("event: " << setw(10) << in.getCounter() << '\r'); DEBUG(endl);
181 
182  multi_pointer_type ps = in.next();
183 
184  JDAQEvent* tev = ps;
185  JFIT::JEvt* evt = ps;
186 
187  dynamics.update(*tev); // update detector
188 
189  JFIT::JEvt::iterator __end = evt->end();
190 
191  if (numberOfFits > 0) {
192  advance(__end = evt->begin(), min(numberOfFits, evt->size()));
193  }
194 
195  if (qualitySorter.is_valid()) {
196  partial_sort(evt->begin(), __end, evt->end(), qualitySorter);
197  }
198 
199  Evt out; // output event
200 
201  if (mc.getEntries() != 0) {
202 
203  Evt* event = mc.getEntry(tev->getCounter());
204 
205  if (event != NULL) {
206 
207  out = *event; // import Monte Carlo true data
208 
209  if (out.mc_run_id == 0) {
210  out.mc_run_id = mc_run_id;
211  }
212 
213  for (vector<Trk>::iterator i = out.mc_trks.begin(); i != out.mc_trks.end(); ++i) {
214  i->pos += center; // offset true positions
215  }
216  }
217  }
218 
219  read(out, *tev); // import DAQ data
220 
221  if (!pmt_router->empty()) { // import calibration data
222 
223  for (vector<Hit>::iterator i = out.mc_hits.begin(); i != out.mc_hits.end(); ++i) {
224 
225  if (pmt_router.hasPMT(i->pmt_id)) {
226 
227  const JPMTAddress address = pmt_router.getAddress(i->pmt_id);
228  const JPMTIdentifier id = pmt_router.getIdentifier(address);
229  const JPMT& pmt = pmt_router.getPMT(address);
230 
231  i->dom_id = id.getID();
232  i->channel_id = id.getPMTAddress();
233  i->pos = getPosition (pmt);
234  i->dir = getDirection(pmt);
235 
236  } else {
237 
238  FATAL("Missing PMT" << i->pmt_id << endl);
239  }
240  }
241  }
242 
243  if (!mod_router->empty()) { // import calibration data
244 
245  for (vector<Hit>::iterator i = out.hits.begin(); i != out.hits.end(); ++i) {
246 
247  if (mod_router.hasModule(i->dom_id)) {
248 
249  const JPMTIdentifier id(i->dom_id, i->channel_id);
250  const JPMTAnalogueSignalProcessor cpu(pmtParameters.getPMTParameters(id));
251 
252  const JPMT& pmt = mod_router.getModule(i->dom_id).getPMT(i->channel_id);
253 
254  const JTRIGGER::JHit hit(getTime(i->tdc, pmt.getCalibration()), i->tot);
255 
256  i->pmt_id = pmt.getID();
257  i->pos = getPosition (pmt);
258  i->dir = getDirection(pmt);
259  i->t = hit.getT();
260  i->tot = hit.getToT();
261  i->a = cpu.getNPE(i->tot);
262 
263  } else {
264 
265  FATAL("Missing module " << i->dom_id << endl);
266  }
267  }
268  }
269 
270  for (JFIT::JEvt::const_iterator fit = evt->begin(); fit != __end; ++fit) { // import reconstruction data
271 
272  Trk trk;
273 
274  trk.id = out.trks.size() + 1;
275  trk.pos = Vec(fit->getX(), fit->getY(), fit->getZ());
276  trk.dir = Vec(fit->getDX(), fit->getDY(), fit->getDZ());
277  trk.t = fit->getT();
278  trk.E = fit->getE();
279  trk.lik = fit->getQ();
281 
282  for (JHistory::const_iterator i = fit->getHistory().begin(); i != fit->getHistory().end(); ++i) {
283  trk.rec_stages.push_back(i->type);
284  }
285 
286  for (int i = 0; i != fit->getN(); ++i) {
287  trk.fitinf.push_back(fit->getW(i));
288  }
289 
290  out.trks.push_back(trk);
291  }
292 
293  out.id = ++number_of_events;
294 
295  outputFile.put(out);
296  }
297  }
298 
299  STATUS(endl);
300 
302 
303  io >> outputFile;
304 
305  outputFile.close();
306 }
Router for direct addressing of PMT data in detector data structure.
Definition: JPMTRouter.hh:33
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
double getT() const
Get calibrated time of hit.
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
const JModule & getModule(const JObjectID &id) const
Get module parameters.
double t
track time [ns] (when the particle is at pos )
Definition: Trk.hh:18
static JDetectorMechanics getMechanics
Function object to get string mechanics.
Definition: JMechanics.hh:243
JPMTIdentifier getIdentifier(const JPMTAddress &address) const
Get identifier of PMT.
Definition: JPMTRouter.hh:126
#define STATUS(A)
Definition: JMessage.hh:63
ROOT TTree parameter settings.
Detector data structure.
Definition: JDetector.hh:80
bool read(Vec &v, std::istream &is)
Read a Vec(tor) from a stream.
Definition: io_ascii.hh:141
Recording of objects on file according a format that follows from the file name extension.
Router for direct addressing of module data in detector data structure.
Vec dir
track direction
Definition: Trk.hh:17
General purpose sorter of fit results.
Hit data structure.
General purpose class for parallel reading of objects from a single file or multiple files...
bool hasPMT(const JObjectID &id) const
Has PMT.
Definition: JPMTRouter.hh:114
const JPMT & getPMT(const JPMTAddress &address) const
Get PMT.
Definition: JPMTRouter.hh:90
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
Long64_t counter_type
Type definition for counter.
double livetime_s
Live time [s].
Definition: JHead.hh:986
double getTime(const Hit &hit)
Get true time of hit.
Basic data structure for time and time over threshold information of hit.
string outputFile
double E
Energy [GeV] (either MC truth or reconstructed)
Definition: Trk.hh:19
const JDetector & update(const double t1_s)
Get detector calibrated at given time.
Definition: JDynamics.hh:477
Data structure for detector geometry and calibration.
Tools for handling different hit types.
ROOT TTree parameter settings.
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
void load(JObjectIterator_t &input)
Load calibration data.
Definition: JDynamics.hh:462
double getToT() const
Get calibrated time over threshold of hit.
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
The Vec class is a straightforward 3-d vector, which also works in pyroot.
Definition: Vec.hh:12
void push(T JHead::*pd)
Push given data member to Head.
Definition: JHead.hh:1238
Detector file.
Definition: JHead.hh:196
JDirection3D getDirection(const Vec &dir)
Get direction.
Acoustic event fit.
int mc_run_id
MC run identifier.
Definition: Evt.hh:26
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
Auxiliary methods for handling file names, type names and environment.
int getID() const
Get identifier.
Definition: JObjectID.hh:50
JAANET::start_run start_run
Definition: JHead.hh:1393
Parallel scanning of objects from a single file or multiple files according a format that follows fro...
Data structure for PMT geometry and calibration.
Definition: JPMT.hh:47
ROOT I/O of application specific meta data.
JPosition3D getPosition(const Vec &pos)
Get position.
Definition of hit and track types and auxiliary methods for handling Monte Carlo data.
std::vector< double > fitinf
place to store additional fit info, for jgandalf, see JFitParameters.hh
Definition: Trk.hh:30
Support methods.
Auxiliary class for map of PMT parameters.
Dynamic detector calibration.
void load(const std::string &file_name)
Load mechanical model parameters from file.
Definition: JMechanics.hh:142
Direct access to PMT in detector data structure.
int debug
debug level
Definition: JSirene.cc:63
const JPMT & getPMT(const int index) const
Get PMT.
Definition: JModule.hh:211
General purpose messaging.
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
Monte Carlo run header.
Definition: JHead.hh:1113
int run_id
MC run number.
Definition: JHead.hh:113
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition: Head.hh:67
#define FATAL(A)
Definition: JMessage.hh:67
int id
track identifier
Definition: Trk.hh:15
Direct access to module in detector data structure.
Dynamic detector calibration.
Definition: JDynamics.hh:59
Vec pos
postion of the track at time t [m]
Definition: Trk.hh:16
Address of PMT in detector data structure.
Definition: JPMTAddress.hh:32
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Data structure for set of track fit results.
std::vector< Trk > trks
list of reconstructed tracks (can be several because of prefits,showers, etc).
Definition: Evt.hh:36
double getLivetime(const std::string &file_name)
Get data taking live time.
std::vector< int > rec_stages
list of identifyers of succesfull fitting stages resulting in this track
Definition: Trk.hh:25
General purpose class for object reading from a list of file names.
double lik
likelihood or lambda value (for aafit, lambda)
Definition: Trk.hh:22
Utility class to parse command line options.
std::vector< Hit > mc_hits
MC: list of MC truth hits.
Definition: Evt.hh:45
static const int JPP_RECONSTRUCTION_TYPE
KM3NeT Data Definitions v2.0.0-14-gbeccebb https://git.km3net.de/common/km3net-dataformat.
bool hasModule(const JObjectID &id) const
Has module.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139
PMT analogue signal processor.
int id
offline event identifier
Definition: Evt.hh:21
Orientation of module.
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
Compass event data types.
std::vector< Hit > hits
list of hits
Definition: Evt.hh:35
do set_variable DETECTOR_TXT $WORKDIR detector
Acoustic event fit.
JAANET::DAQ DAQ
Definition: JHead.hh:1415
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
The Trk class represents a Monte Carlo (MC) particle as well as a reconstructed track/shower.
Definition: Trk.hh:13
JTriggerCounter_t getCounter() const
Get trigger counter.
std::vector< Trk > mc_trks
MC: list of MC truth tracks.
Definition: Evt.hh:46
const JPMTAddress & getAddress(const JObjectID &id) const
Get address of PMT.
Definition: JPMTRouter.hh:78
bool qualitySorter(const JRECONSTRUCTION::JFit &first, const JRECONSTRUCTION::JFit &second)
Comparison of fit results.
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:19
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
int rec_type
identifyer for the overall fitting algorithm/chain/strategy
Definition: Trk.hh:24