ToolDAQFramework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ReconRandomiser.cpp
Go to the documentation of this file.
1 #include "ReconRandomiser.h"
2 
4 
5 
6 bool ReconRandomiser::Initialise(std::string configfile, DataModel &data){
7 
8  if(configfile!="") m_variables.Initialise(configfile);
9  //m_variables.Print();
10 
11  m_verbose = 0;
12  m_variables.Get("verbose", m_verbose);
13 
14  //Setup and start the stopwatch
15  bool use_stopwatch = false;
16  m_variables.Get("use_stopwatch", use_stopwatch);
17  m_stopwatch = use_stopwatch ? new util::Stopwatch("ReconRandomiser") : 0;
18 
19  m_stopwatch_file = "";
20  m_variables.Get("stopwatch_file", m_stopwatch_file);
21 
23 
24  m_data= &data;
25 
26  //set number of events
27  if(! m_variables.Get("nevents", fNEvents) ) {
28  Log("WARN: nevents configuration not found. Producing 1 event", WARN, m_verbose);
29  fNEvents = 1;
30  }
31  else if(fNEvents <= 0) {
32  Log("WARN: Given negative or 0 nevents. Producing 1 event", WARN, m_verbose);
33  fNEvents = 1;
34  }
35  fCurrEvent = 0;
36 
37  //Random distribution variables
38  if(!m_variables.Get("n_vertices_mean", fNVerticesMean)) {
39  Log("FATAL: Must specify n_vertices_mean", FATAL, m_verbose);
40  return false;
41  }
42 
43  if(!m_variables.Get("x_mean_pos", fXMean)) {
44  Log("FATAL: Must specify x_mean_pos", FATAL, m_verbose);
45  return false;
46  }
47  if(!m_variables.Get("x_width", fXWidth)) {
48  Log("FATAL: Must specify x_width", FATAL, m_verbose);
49  return false;
50  }
51  if(!m_variables.Get("y_mean_pos", fYMean)) {
52  Log("FATAL: Must specify y_mean_pos", FATAL, m_verbose);
53  return false;
54  }
55  if(!m_variables.Get("y_width", fYWidth)) {
56  Log("FATAL: Must specify y_width", FATAL, m_verbose);
57  return false;
58  }
59  if(!m_variables.Get("z_mean_pos", fZMean)) {
60  Log("FATAL: Must specify z_mean_pos", FATAL, m_verbose);
61  return false;
62  }
63  if(!m_variables.Get("z_width", fZWidth)) {
64  Log("FATAL: Must specify z_width", FATAL, m_verbose);
65  return false;
66  }
67 
68  if(!m_variables.Get("max_z_pos", fMaxZPos)) {
69  Log("FATAL: Must specify max_z_pos", FATAL, m_verbose);
70  return false;
71  }
72  if(!m_variables.Get("max_r_pos", fMaxRPos)) {
73  Log("FATAL: Must specify max_r_pos", FATAL, m_verbose);
74  return false;
75  }
76 
77  if(!m_variables.Get("flat_r", fFlatR)) {
78  Log("FATAL: Must specify flat_r", FATAL, m_verbose);
79  return false;
80  }
81 
82  // work out the type of distribution for each axis
86 
87  if(!m_variables.Get("t_min", fTMin)) {
88  Log("FATAL: Must specify t_min", FATAL, m_verbose);
89  return false;
90  }
91  if(!m_variables.Get("t_max", fTMax)) {
92  Log("FATAL: Must specify t_max", FATAL, m_verbose);
93  return false;
94  }
95 
96  int seed = 0;
97  if(!m_variables.Get("seed", seed)) {
98  Log("WARN: No seed specified. Using default 0. Your results are not reproducable!", WARN, m_verbose);
99  }
100  fRand = new TRandom3(seed);
101 
102  fRandomDirection = false;
103  //TODO set this to true if the user wants random directions
104 
105  if(m_stopwatch) Log(m_stopwatch->Result("Initialise"), INFO, m_verbose);
106 
107  return true;
108 }
109 
110 
113 
114  //Determine how many vertices to generate
115  const int N = fRand->Poisson(fNVerticesMean);
116  const double likelihood = 9E7;
117  const int nhits = 1E4;
118  double pos[3];
119  for(int iv = 0; iv < N; iv++) {
120  CreateVertex(pos);
121  double time = fRand->Uniform(fTMin, fTMax);
122 
123  ss << "DEBUG: Created event at x,y,z";
124  for(int i = 0; i < 3; i++)
125  ss << " " << pos[i];
126  ss << " time " << time;
128 
129  if(!fRandomDirection) {
130  //fill the transient data model
131  m_data->RecoInfo.AddRecon(kReconRandomNoDirection, iv, nhits, time, pos, likelihood, likelihood);
132  }
133  else {
134  //m_data->RecoInfo.AddRecon(kReconRandom, iv, nhits, time, pos, likelihood, likelihood, ....);
135  }
136  }//iv
137 
138  //and finally, increment event counter
139  fCurrEvent++;
140 
141  //and flag to exit the Execute() loop, if appropriate
142  if(fCurrEvent >= fNEvents)
143  m_data->vars.Set("StopLoop",1);
144 
146 
147  return true;
148 }
149 
150 
152  if(m_stopwatch) {
154  m_stopwatch->Start();
155  }
156 
157  delete fRand;
158 
159  if(m_stopwatch) {
160  Log(m_stopwatch->Result("Finalise"), INFO, m_verbose);
161  delete m_stopwatch;
162  }
163 
164  return true;
165 }
166 
170 // The following are randomising functions
174 
176 {
177  double x, y, r, z, xdir, ydir;
178  const int maxcount = 100;
179 
180  //create a flat distribution in r and phi
181  if(fFlatR
182  && (fXDistribution == kUniform)
183  && (fYDistribution == kUniform)) {
184  r = fRand->Uniform(0, +fMaxRPos);
185  fRand->Circle(xdir, ydir, 1);
186  x = r * xdir;
187  y = r * ydir;
188  }
189  //create a flat distribution in x and y
190  else {
191  r = fMaxRPos + 1;
192  while(r > fMaxRPos) {
193  //x
195  //y
197  //r
198  r = TMath::Sqrt(TMath::Power(x, 2) + TMath::Power(y, 2));
199  }
200  }
201  //z
203 
204  pos[0] = x;
205  pos[1] = y;
206  pos[2] = z;
207 }
208 
209 double ReconRandomiser::GetRandomNumber(Distribution_t dist, double max, double mean, double width, const int maxcount)
210 {
211  switch(dist) {
212  case (kUniform):
213  return fRand->Uniform(-max, +max);
214  break;
215  case(kGauss): {
216  double x = max + 1;
217  int count = 0; //don't get stuck in the loop forever
218  while(abs(x) > max) {
219  fRand->Gaus(mean, width);
220  if(count > maxcount) break;
221  }
222  //if we've not got a sensible value after enough tries, return the appropriate max
223  if(abs(x) > max) {
224  ss << "WARN: Could not produce random number within limit. Returning appropriately signed " << max;
225  StreamToLog(WARN);
226  if(mean >= 0)
227  return +max;
228  else
229  return -max;
230  }
231  else
232  return x;
233  break;
234  }
235  case(kFixed):
236  return mean;
237  break;
238  default:
239  ss << "WARN: Unknown Distribution_t value " << dist << " Returning 0";
240  StreamToLog(WARN);
241  return 0;
242  break;
243  }
244  return 0;
245 }
246 
247 Distribution_t ReconRandomiser::GetDistributionType(double width, const char * axis)
248 {
249  Distribution_t dist;
250  if(abs(width) < 1E-6) {
251  dist = kFixed;
252  }
253  else if(width < 0) {
254  dist = kUniform;
255  }
256  else {
257  dist = kGauss;
258  }
259  ss << "INFO: Will generate " << axis << " axis as " << EnumAsString(dist);
260  StreamToLog(INFO);
261  return dist;
262 }
263 
265 
std::string Result(std::string method_name, std::string output_file="")
Definition: Stopwatch.cpp:38
enum EDistribution Distribution_t
std::string m_stopwatch_file
Image filename to save the histogram to, if required.
void Start()
Start the stopwatch.
Definition: Stopwatch.cpp:18
StopwatchTimes Stop()
Stop the stopwatch, returning the CPU time.
Definition: Stopwatch.cpp:24
Distribution_t fYDistribution
std::stringstream ss
Distribution_t fXDistribution
void StreamToLog(int level)
static std::string EnumAsString(Distribution_t dist)
Distribution_t GetDistributionType(double width, const char *axis)
void CreateVertex(double *pos)
void Log(const std::string &message, const int message_level)
Format messages in the same way as for tools.
Definition: Utilities.cpp:276
util::Stopwatch * m_stopwatch
The stopwatch, if we&#39;re using one.
Distribution_t fZDistribution
bool Initialise(std::string configfile, DataModel &data)
TRandom3 * fRand
double GetRandomNumber(Distribution_t dist, double max, double mean, double width, const int maxcount)