ToolDAQFramework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
SupernovaDirectionCalculator.cpp
Go to the documentation of this file.
2 
4 
5 
6 bool SupernovaDirectionCalculator::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("SupernovaDirectionCalculator") : 0;
18 
19  m_stopwatch_file = "";
20  m_variables.Get("stopwatch_file", m_stopwatch_file);
21 
23 
24  m_data= &data;
25 
26 
27  if(!m_variables.Get("input_filter_name", m_input_filter_name)) {
28  Log("INFO: input_filter_name not given. Using ALL", WARN, m_verbose);
29  m_input_filter_name = "ALL";
30  }
31  m_in_filter = m_data->GetFilter(m_input_filter_name, false);
32  if(!m_in_filter) {
33  m_ss << "FATAL: no filter named " << m_input_filter_name << " found. Returning false";
35  return false;
36  }
37 
38  // Apply weights?
39  m_weight_events = false;
40  m_variables.Get("weight_events", m_weight_events);
41  // Load weights for interpolation
42  if (m_weight_events){
43  std::string weights_file, line;
44  m_variables.Get("weights_file", weights_file);
45  std::ifstream myfile(weights_file);
46  if (myfile.is_open()){
47  // Skip first line
48  getline(myfile,line);
49  double logE, weight;
50  char _; // Dump variable to ignore commas
51  while (myfile >> logE >> _ >> weight){
52  m_log10_energy.push_back(logE);
53  m_weight.push_back(weight);
54  }
55  myfile.close();
56  } else {
57  Log("Could not open weights file!", ERROR, m_verbose);
58  return false;
59  }
60  }
61 
62 
63  if(m_stopwatch) Log(m_stopwatch->Result("Initialise"), INFO, m_verbose);
64 
65  return true;
66 }
67 
68 
70 
72 
73  float direction[3] = {0,0,1};
74 
75  // First round
76  // (Weighted) average over all directions
77  CalculateDirection(direction, -1.);
78  m_ss << "First pass: average over all events\n";
79  m_ss << "First pass SN neutrino direction x, y, z: " << direction[0] << ", " << direction[1] << ", " << direction[2];
81 
82  // Second round
83  // (Weighted) average over all directions w/ cos(theta) w.r.t to previous estimate >= 0.0
84  // Other events are used to estimate uniform contribution
85  CalculateDirection(direction, 0.0);
86  m_ss << "Second pass: average over events w/ cos(theta) >= 0.0\n";
87  m_ss << "Second pass SN neutrino direction x, y, z: " << direction[0] << ", " << direction[1] << ", " << direction[2];
89 
90  // Third round
91  // (Weighted) average over all directions w/ cos(theta) w.r.t to previous estimate >= 0.5
92  // Other events are used to estimate uniform contribution
93  CalculateDirection(direction, 0.5);
94  m_ss << "Third pass: average over events w/ cos(theta) >= 0.5\n";
95  m_ss << "Third pass SN neutrino direction x, y, z: " << direction[0] << ", " << direction[1] << ", " << direction[2];
97 
99 
100  return true;
101 }
102 
103 
105 
106  if(m_stopwatch) {
108  m_stopwatch->Start();
109  }
110 
111 
112  if(m_stopwatch) {
113  Log(m_stopwatch->Result("Finalise"), INFO, m_verbose);
114  delete m_stopwatch;
115  }
116 
117  return true;
118 }
119 
121  double weight = 0;
122  int i = -1;
123  // Find interpolation position
124  for(int j=0; j<m_log10_energy.size(); ++j){
125  if (m_log10_energy[j] > log10_energy) break;
126  i = j;
127  }
128 
129  // Out of range
130  if (i < 0) return m_weight[0];
131  if (i == m_log10_energy.size() - 1) return m_weight[i];
132 
133  // Interpolate
134  double x0 = m_log10_energy[i];
135  double x1 = m_log10_energy[i+1];
136  double x = log10_energy;
137  double y0 = m_weight[i];
138  double y1 = m_weight[i+1];
139  return y0 + (y1 - y0) * (x - x0) / (x1 - x0);
140 }
141 
142 void SupernovaDirectionCalculator::CalculateDirection(float direction[3], float costheta_cut){
143  const int N = m_in_filter->GetNRecons();
144  double temp_direction[3] = {0,0,0};
145  double flat_density = 0.;
146  for(int irecon = 0; irecon < N; irecon++) {
147  //get the event direction
149  double dir_z = cos(dir.theta);
150  double dir_y = sin(dir.theta) * sin(dir.phi);
151  double dir_x = sin(dir.theta) * cos(dir.phi);
152 
153  double weight = 1.;
154  if (m_weight_events){
155  double E = m_in_filter->GetEnergy(irecon);
156  weight = GetEventWeight(log10(E));
157  }
158 
159  double costheta = dir_x * direction[0] + dir_y * direction[1] + dir_z * direction[2];
160  if (costheta < costheta_cut){
161  // Add those events to the flat weight density estimtor
162  flat_density += weight;
163  } else {
164  temp_direction[0] += dir_x * weight;
165  temp_direction[1] += dir_y * weight;
166  temp_direction[2] += dir_z * weight;
167  }
168  }//irecon
169 
170  // Calculate density
171  if (flat_density > 0) {
172  flat_density /= 2 * M_PI * (1.+costheta_cut);
173 
174  // Subtract expected flat contribution from vector
175  // Expectation = flat_density * surface_of_sperical_cap * centre_of_mass_of_spherical_cap
176  double h = (1.-costheta_cut);
177  double expected = flat_density * (2.*M_PI*h) * (3*pow(2-h, 2)) / (4*(3-h));
178  temp_direction[0] -= direction[0] * expected;
179  temp_direction[1] -= direction[1] * expected;
180  temp_direction[2] -= direction[2] * expected;
181  }
182 
183  // Normalise direction vector
184  double r = temp_direction[0]*temp_direction[0];
185  r += temp_direction[1]*temp_direction[1];
186  r += temp_direction[2]*temp_direction[2];
187  r = sqrt(r);
188  temp_direction[0] /= r;
189  temp_direction[1] /= r;
190  temp_direction[2] /= r;
191  direction[0] = temp_direction[0];
192  direction[1] = temp_direction[1];
193  direction[2] = temp_direction[2];
194 }
std::string Result(std::string method_name, std::string output_file="")
Definition: Stopwatch.cpp:38
std::vector< double > m_log10_energy
Vector of log10(energy) for weight interpolation.
double GetEventWeight(double log10_energy)
Return the weight for the event.
DirectionEuler GetDirectionEuler(int irecon)
Definition: ReconInfo.h:98
void Start()
Start the stopwatch.
Definition: Stopwatch.cpp:18
StopwatchTimes Stop()
Stop the stopwatch, returning the CPU time.
Definition: Stopwatch.cpp:24
std::stringstream m_ss
For easy formatting of Log messages.
double phi
Definition: ReconInfo.h:43
std::vector< double > m_weight
Vector of weights for interpolation.
util::Stopwatch * m_stopwatch
The stopwatch, if we&#39;re using one.
int m_verbose
Verbosity level, as defined in tool parameter file.
void CalculateDirection(float direction[3], float costheta_cut)
void Log(const std::string &message, const int message_level)
Format messages in the same way as for tools.
Definition: Utilities.cpp:276
std::string m_stopwatch_file
Image filename to save the histogram to, if required.
bool m_weight_events
Enable weighting of events.
double GetEnergy(int irecon)
Definition: ReconInfo.h:90
double theta
Definition: ReconInfo.h:43
int GetNRecons()
Definition: ReconInfo.h:84
bool Initialise(std::string configfile, DataModel &data)