ToolDAQFramework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
PrepareSubSamples.cpp
Go to the documentation of this file.
1 #include "PrepareSubSamples.h"
2 
4 
5 
6 bool PrepareSubSamples::Initialise(std::string configfile, DataModel &data){
7 
8  m_data= &data;
9 
10  if(configfile!="") m_variables.Initialise(configfile);
11  //m_variables.Print();
12 
13  m_verbose = 0;
14  m_variables.Get("verbose", m_verbose);
15 
16  //Setup and start the stopwatch
17  bool use_stopwatch = false;
18  m_variables.Get("use_stopwatch", use_stopwatch);
19  m_stopwatch = use_stopwatch ? new util::Stopwatch("PrepareSubSamples") : 0;
20 
21  m_stopwatch_file = "";
22  m_variables.Get("stopwatch_file", m_stopwatch_file);
23 
25 
26  double temp_width, temp_overlap;
27  m_variables.Get("sample_width", temp_width);
28  m_variables.Get("sample_overlap", temp_overlap);
29 
30  // Configuration is in ns
31  m_sample_width = temp_width * TimeDelta::ns;
32  m_sample_overlap = temp_overlap * TimeDelta::ns;
33 
34  if(m_stopwatch) Log(m_stopwatch->Result("Initialise"), INFO, m_verbose);
35 
36  return true;
37 }
38 
39 void PrepareSubSamples::SortSubSampleVector(std::vector<SubSample> &samples){
40  for (std::vector<SubSample>::iterator it = samples.begin(); it != samples.end(); ++it){
41  m_ss << "DEBUG: Sorting sample";
43  it->SortByTime();
44  }
45 }
46 
47 bool PrepareSubSamples::CheckSubSampleVectorNeedsSplitting(const std::vector<SubSample> &samples){
48  for (std::vector<SubSample>::const_iterator it = samples.begin(); it != samples.end(); ++it){
50  return true;
51  }
52  }
53  return false;
54 }
55 
57  int N = sample.m_time.size();
58  if (N == 0)
59  return false;
60  return (TimeDelta(sample.m_time[N-1]) - TimeDelta(sample.m_time[0])) > m_sample_width;
61 }
62 
63 std::vector<SubSample> PrepareSubSamples::SplitSubSampleVector(std::vector<SubSample> &samples){
64  std::vector<SubSample> new_samples;
65  for (std::vector<SubSample>::iterator it = samples.begin(); it != samples.end(); ++it){
66  m_ss << "DEBUG: Splitting sample";
68  std::vector<SubSample> temp_samples = it->Split(m_sample_width, m_sample_overlap);
69  new_samples.insert(new_samples.end(), temp_samples.begin(), temp_samples.end());
70  m_ss << "DEBUG: Created " << temp_samples.size() << " samples at times (timestamp unit != hit time unit):";
72  for (std::vector<SubSample>::iterator it2 = temp_samples.begin(); it2 != temp_samples.end(); ++it2){
73  m_ss << "DEBUG: " << it2->m_timestamp / TimeDelta::s << " First hit: " << (it2->m_time.size()==0 ? -999 : it2->m_time.at(0));
75  if(m_verbose >= DEBUG3) {
76  m_ss << "DEBUG: First unique hit is at " << it2->m_first_unique;
78  for(size_t ihit = 0; ihit < it2->m_time.size(); ihit++) {
79  m_ss << "DEBUG: Hit " << ihit << " is at time " << it2->m_time[ihit] << " absolute " << it2->AbsoluteDigitTime(ihit);
81  }//ihit
82  }//DEBUG3
83  }//Loop over the splits for this SubSample
84  }//Loop over initial SubSamples
85  return new_samples;
86 }
87 
90 
91  // Split ID samples
92  m_ss << "DEBUG: Preparing " << m_data->IDSamples.size() << " ID samples";
94  SortSubSampleVector(m_data->IDSamples);
95  if (CheckSubSampleVectorNeedsSplitting(m_data->IDSamples)){
96  m_data->IDSamples = SplitSubSampleVector(m_data->IDSamples);
97  }
98 
99  // Split OD samples
100  m_ss << "DEBUG: Preparing " << m_data->ODSamples.size() << " OD samples";
102  SortSubSampleVector(m_data->ODSamples);
103  if (CheckSubSampleVectorNeedsSplitting(m_data->ODSamples)){
104  m_data->ODSamples = SplitSubSampleVector(m_data->ODSamples);
105  }
106 
108 
109  return true;
110 }
111 
112 
114  if(m_stopwatch) {
116  m_stopwatch->Start();
117  }
118 
119  if(m_stopwatch) {
120  Log(m_stopwatch->Result("Finalise"), INFO, m_verbose);
121  delete m_stopwatch;
122  }
123 
124  return true;
125 }
std::string Result(std::string method_name, std::string output_file="")
Definition: Stopwatch.cpp:38
util::Stopwatch * m_stopwatch
The stopwatch, if we&#39;re using one.
bool Initialise(std::string configfile, DataModel &data)
void Start()
Start the stopwatch.
Definition: Stopwatch.cpp:18
std::vector< SubSample > SplitSubSampleVector(std::vector< SubSample > &samples)
Split all SubSamples.
StopwatchTimes Stop()
Stop the stopwatch, returning the CPU time.
Definition: Stopwatch.cpp:24
std::stringstream m_ss
void StreamToLog(int level)
std::string m_stopwatch_file
Image filename to save the histogram to, if required.
TimeDelta m_sample_width
The desired maximum SubSample length.
bool CheckSubSampleNeedsSplitting(const SubSample &sample)
Check whether the SubSample needs to be split.
static const TimeDelta ns
TimeDelta of 1 ns.
Definition: TimeDelta.h:57
void Log(const std::string &message, const int message_level)
Format messages in the same way as for tools.
Definition: Utilities.cpp:276
std::vector< TimeDelta::short_time_t > m_time
Vector of hit times relative to timestamp for all hits in SubSample. Unit: ns.
Definition: SubSample.h:42
bool CheckSubSampleVectorNeedsSplitting(const std::vector< SubSample > &samples)
Check whether any of the SubSamples needs to be split.
static const TimeDelta s
TimeDelta of 1 s.
Definition: TimeDelta.h:63
TimeDelta m_sample_overlap
The desired SubSample overlap time.
void SortSubSampleVector(std::vector< SubSample > &samples)
Sort the digits in all SubSamples by time.