Jpp  18.0.0-rc.4
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
software/JTimeslice/JRandomTimesliceWriter.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <fstream>
5 
6 #include "TRandom3.h"
7 
10 
11 #include "JAAnet/JHead.hh"
12 #include "JAAnet/JHeadToolkit.hh"
13 
14 #include "JDetector/JDetector.hh"
22 #include "JDAQ/JDAQTimesliceIO.hh"
29 #include "JSupport/JSupport.hh"
31 #include "JSupport/JRunByRun.hh"
32 #include "JSupport/JMeta.hh"
33 
34 #include "Jeep/JTimer.hh"
35 #include "Jeep/JParser.hh"
36 #include "Jeep/JMessage.hh"
37 
38 
39 /**
40  * \file
41  *
42  * Auxiliary program to write KM3NETDAQ::JDAQTimeslice with random data.\n
43  * To pipe data to application JTriggerProcessor.cc whilst maintaining header and meta data, two output files can be specified.\n
44  * The file names should have extensions ".root" and ".dat", respectively. \n
45  * The first corresponds to a ROOT formatted file with header and meta data and the second to the pipe with timeslice data.
46  * \author mdejong
47  */
48 int main(int argc, char **argv)
49 {
50  using namespace std;
51  using namespace JPP;
52  using namespace KM3NETDAQ;
53 
55 
57  string detectorFile;
58  Long64_t numberOfSlices;
59  JDAQHit::JTDC_t TCLB_ns;
60  JPMTParametersMap pmtParameters;
61  JK40Rates rates_Hz;
62  int run;
63  JRunByRun runbyrun;
64  pair<size_t, double> recycling;
65  double sigma_ns;
66  bool fast;
67  UInt_t seed;
68  int debug;
69 
70  try {
71 
72  JParser<> zap("Auxiliary program to write time slices with random data.");
73 
74  zap['o'] = make_field(outputFile, "output file");
75  zap['n'] = make_field(numberOfSlices);
76  zap['a'] = make_field(detectorFile, "detector.");
77  zap['R'] = make_field(run, "run number") = -1;
78  zap['r'] = make_field(runbyrun, "option for run-by-run mode") = JPARSER::initialised();
79  zap['P'] = make_field(pmtParameters, "PMT simulation data (or corresponding file name)") = JPARSER::initialised();
80  zap['B'] = make_field(rates_Hz, "background rates [Hz]") = JPARSER::initialised();
81  zap['T'] = make_field(TCLB_ns, "CLB state-machine time jitter") = 256; // [ns]
82  zap['N'] = make_field(recycling, "number of recycles / time interval for sampling data [ns]") = make_pair(0, 0.0);
83  zap['s'] = make_field(sigma_ns, "intrinsic time smearing of K40 coincidences [ns]") = JK40DefaultSimulatorInterface::getSigma();
84  zap['F'] = make_field(fast, "fast - disable PMT simulation");
85  zap['S'] = make_field(seed, "seed") = 0;
86  zap['d'] = make_field(debug, "debug") = 0;
87 
88  zap(argc, argv);
89  }
90  catch(const exception &error) {
91  FATAL(error.what() << endl);
92  }
93 
94 
95  if (outputFile.size() != 1 &&
96  outputFile.size() != 2) {
97  FATAL("Invalid number of output files; should be 1 or 2." << endl);
98  }
99 
100  gRandom->SetSeed(seed);
101 
102  JK40DefaultSimulatorInterface::setSigma(sigma_ns);
103 
104 
106 
107  if (pmtParameters.getQE() != 1.0) {
108 
109  WARNING("Correct background rates with global efficiency " << pmtParameters.getQE() << endl);
110 
111  rates_Hz.correct(pmtParameters.getQE());
112  }
113 
114  DEBUG("PMT parameters: " << endl << pmtParameters << endl);
115  DEBUG("K40 rates: " << endl << rates_Hz << endl);
116 
118 
119  try {
120  load(detectorFile, detector);
121  }
122  catch(const JException& error) {
123  FATAL(error);
124  }
125 
126  JPMTParametersMap::Throw(false);
127 
129  JSummaryRouter summaryRouter;
130 
131  if (runbyrun.is_valid()) {
132 
133  NOTICE("Using run-by-run:" << endl << runbyrun << endl);
134 
135  if (!runbyrun.hasNext()) {
136  FATAL("Run-by-run simulation yields no input." << endl);
137  }
138 
139  if (rates_Hz.getSinglesRate() != 0.0) {
140  WARNING("Run-by-run simulation discards singles rate [Hz] " << rates_Hz.getSinglesRate() << endl);
141  }
142 
143  try {
144  simbad.reset(new JK40RunByRunSimulator(summaryRouter, rates_Hz));
145  simbad.reset(new JPMTRunByRunSimulator(summaryRouter, pmtParameters, detector));
146  simbad.reset(new JCLBDefaultSimulator());
147  }
148  catch(const JException& error) {
149  FATAL(error.what() << endl);
150  }
151 
152  } else {
153 
154  NOTICE("Using fixed rates [Hz]: " << rates_Hz << endl);
155 
156  try {
157  simbad.reset(new JK40DefaultSimulator(rates_Hz));
158  simbad.reset(fast ?
160  new JPMTDefaultSimulator(pmtParameters, detector));
161  simbad.reset(new JCLBDefaultSimulator());
162  }
163  catch(const JException& error) {
164  FATAL(error.what() << endl);
165  }
166  }
167 
168 
169  JTimer timerco("constructor");
170  JTimer timerrc("recycle");
171  JTimer timerIO("I/O");
172 
173  for (size_t i = 0; i != outputFile.size(); ++i) {
174 
175  outputFile[i].open();
176 
177  if (!outputFile[i].is_open()) {
178  FATAL("Error opening file " << outputFile[i] << endl);
179  }
180  }
181 
182  outputFile[0].put(JMeta(argc, argv));
183  outputFile[0].put(*gRandom);
184 
185  int counter = 0;
186 
187  for ( ; counter != numberOfSlices; ) {
188 
189  STATUS("slice: " << setw(10) << counter << '\r'); DEBUG(endl);
190 
191  int frame_index = counter + 1;
192 
193  if (runbyrun.hasNext()) {
194 
195  summaryRouter.update(runbyrun.next());
196 
197  summaryRouter.correct(dynamic_cast<const JPMTDefaultSimulatorInterface&>(simbad.getPMTSimulator()));
198 
199  frame_index = summaryRouter.getFrameIndex();
200  run = summaryRouter.getRunNumber();
201  }
202 
203  const JDAQChronometer chronometer(detector.getID(), run, frame_index, JDAQUTCExtended(getTimeOfFrame(frame_index)));
204 
205  timerco.start();
206 
207  JRandomTimeslice timeslice(chronometer, simbad);
208 
209  timerco.stop();
210 
211  timerIO.start();
212 
213  outputFile.rbegin()->put(timeslice); ++counter;
214 
215  timerIO.stop();
216 
217  for (size_t i = 1; i <= recycling.first && counter != numberOfSlices; ++i) {
218 
219  STATUS("slice: " << setw(10) << counter << '\r'); DEBUG(endl);
220 
221  timerrc.start();
222 
223  timeslice.recycle(recycling.second);
224 
225  timerrc.stop();
226 
227  timerIO.start();
228 
229  outputFile.rbegin()->put(timeslice); ++counter;
230 
231  timerIO.stop();
232  }
233  }
234  STATUS(endl);
235 
236  if (debug >= notice_t) {
237  timerco.print(cout, true);
238  timerrc.print(cout, true);
239  timerIO.print(cout, true);
240  }
241 
242  Head header;
243 
244  {
245  JHead buffer;
246 
247  buffer.DAQ.livetime_s = getLivetime(runbyrun->getFilelist());
248  buffer.K40.livetime_s = counter * getFrameTime() * 1.0e-9;
249 
250  buffer.push(&JHead::DAQ);
251  buffer.push(&JHead::K40);
252 
253  copy(buffer, header);
254  }
255 
256  outputFile[0].put(header);
257  outputFile[0].put(*gRandom);
258 
259  for (size_t i = 0; i != outputFile.size(); ++i) {
260  outputFile[i].close();
261  }
262 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Auxiliary class to select summary data (KM3NETDAQ::JDAQSummaryslice) from the specified raw data file...
Definition: JRunByRun.hh:32
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1514
General exception.
Definition: JException.hh:23
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
Default implementation of the simulation of K40 background.
#define STATUS(A)
Definition: JMessage.hh:63
Detector data structure.
Definition: JDetector.hh:89
double getSigma(vector< double > &v)
get standard deviation of vector content
Recording of objects on file according a format that follows from the file name extension.
void print(std::ostream &out, const JScale_t scale=milli_t) const
Print timer data.
Definition: JTimer.hh:161
notice
Definition: JMessage.hh:32
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
double livetime_s
Live time [s].
Definition: JHead.hh:1040
const JPMTSimulator & getPMTSimulator() const
Get PMT simulator.
string outputFile
Data structure for UTC time.
Data structure for detector geometry and calibration.
unsigned int JTDC_t
leading edge [ns]
Definition: JDAQHit.hh:39
void stop()
Stop timer.
Definition: JTimer.hh:113
double getTimeOfFrame(const int frame_index)
Get start time of frame in ns since start of run for a given frame index.
Definition: JDAQClock.hh:185
K40 simulation based on run-by-run information.
void setDAQLongprint(const bool option)
Set DAQ print option.
Definition: JDAQPrint.hh:28
Scanning of objects from a single file according a format that follows from the extension of each fil...
void push(T JHead::*pd)
Push given data member to Head.
Definition: JHead.hh:1360
Detector file.
Definition: JHead.hh:226
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
Auxiliary class for CPU timing and usage.
Definition: JTimer.hh:32
ROOT I/O of application specific meta data.
#define NOTICE(A)
Definition: JMessage.hh:64
Support methods.
Q DAQ
Definition: JDataQuality.sh:59
Auxiliary class for map of PMT parameters.
JAANET::K40 K40
Definition: JHead.hh:1590
Router for fast addressing of summary data in KM3NETDAQ::JDAQSummaryslice data structure as a functio...
General purpose messaging.
Monte Carlo run header.
Definition: JHead.hh:1221
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition: Head.hh:65
#define FATAL(A)
Definition: JMessage.hh:67
void reset(JK40Simulator *k40Simulator)
Reset K40 simulator.
PMT simulation based on run-by-run information.
virtual const char * what() const override
Get error message.
Definition: JException.hh:48
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
double getLivetime(const std::string &file_name)
Get data taking live time.
Utility class to parse command line options.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
do set_variable DETECTOR_TXT $WORKDIR detector
JAANET::DAQ DAQ
Definition: JHead.hh:1589
then echo WARNING
Definition: JTuneHV.sh:91
Timeslice with random data.
double livetime_s
Live time [s].
Definition: JHead.hh:1094
int debug
debug level
Auxiliary class for K40 rates.
Definition: JK40Rates.hh:41
void start()
Start timer.
Definition: JTimer.hh:89
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
void recycle(const double T_ns)
Recycle time slice by randomly shuffling time intervals of data.