Jpp  15.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JFitNB.cc File Reference
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <numeric>
#include "Jeep/JParser.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JModuleRouter.hh"
#include "JTools/JCombinatorics.hh"
#include "km3net-dataformat/online/JDAQ.hh"
#include "JSupport/JMeta.hh"
#include "TH1.h"
#include "TH2.h"
#include "TFile.h"
#include "TKey.h"
#include "TF1.h"
#include "TFitResult.h"
#include "JNanobeacon.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 28 of file JFitNB.cc.

28  {
29 
30  string inputFiles;
31  string detectorFile;
32  string outFile;
33  int Neighbours;
34  bool overwriteDetector;
35 
36  try {
37  JParser<> zap;
38  zap['f'] = make_field(inputFiles ); //The input is the output from JMergeNB, if nothing needs to be merged the output from JCalibrateNB works as well.
39  zap['a'] = make_field(detectorFile );
40  zap['N'] = make_field(Neighbours ) = 1; //Determines the maximum distance between reference and target DOM
41  zap['A'] = make_field(overwriteDetector );
42  zap['o'] = make_field(outFile ); //if you want to write the fitted histograms to an output file, !!WITHOUT EXTENSION!!
43  zap(argc,argv);
44  }
45  catch(const exception &error) {
46  ERROR(error.what() << endl);
47  }
48 
50  load(detectorFile , detector);
51  JModuleRouter moduleRouter(detector);
52 
53  int num_of_floors = getNumberOfFloors(detector);
54  JCombinatorics c(num_of_floors);
56 
57  TFile in(inputFiles.c_str() , "read");
58  string hist_str;
59  string hist_name;
60 
61  string outFile_root = outFile+".root";
62  string outFile_csv = outFile+".csv";
63  string outFile_det = outFile+"_det.csv";
64 
65  TFile output(outFile_root.c_str(),"RECREATE");
66  cout << " Writing histograms to: " << outFile_root << endl;
67  output.cd();
68 
69  ofstream out_csv(outFile_csv.c_str());
70  cout << " Writing fitting data to: " << outFile_csv << endl;
71  out_csv << "String,Ref_floor,Tgt_floor,T0,Error" << endl;
72 
73  // Create two maps of vectors to store the T0s and Errors so that they can be averaged
74  int num_of_strings = getNumberOfStrings(detector);
75  typedef map<int, vector<double> > map_type;
76  vector<double> base_vector(num_of_floors, 0.0);
77 
78  map_type vmap;
79  for( int j = 1; j<=num_of_strings; j++){
80  vmap.insert( make_pair(j,base_vector) );
81  }
82 
83  for(JDetector::iterator module = detector.begin(); module != detector.end(); ++module){ //loop over modules in detector file to fill the maps
84  int string_no = moduleRouter.getModule( module->getID() ).getString();
85  int tgt_floor = moduleRouter.getModule( module->getID() ).getFloor();
86 
87  vector<double> T0s;
88  vector<double> Errors;
89 
90  for( int i = 1; i <= Neighbours; i++){
91  int ref_floor = tgt_floor - i;
92 
93  if(ref_floor < 1){ continue; } //The reference floor can not be negative
94 
95  hist_str = to_string(string_no);
96 
97  if( in.GetListOfKeys()->Contains(hist_str.c_str()) ){ //check if histogram corresponding to the string exists in the input file
98  TH2D* h2D;
99  in.GetObject( hist_str.c_str(), h2D );
100 
101  //floors run between 1 and 18 but the pairs consist of the number 0 to 17. The index is given between 0 and npairs, but the bins run from 1 to npairs+1
102  int bin = c.getIndex(tgt_floor - 1, ref_floor - 1 ) + 1;
103 
104  hist_name = "S"+hist_str+"F"+to_string(ref_floor)+"_S"+hist_str+"F"+to_string(tgt_floor);
105  TH1D* h1D = h2D->ProjectionY( hist_name.c_str(), bin, bin );
106  if( h1D->GetEntries() < 1 ){ continue; } //No point in fitting empty histograms
107 
108  TFitResultPtr fitresult = JNANOBEACON::Fit(h1D);
109  T0s.push_back( fitresult->Parameter(1) );
110  Errors.push_back( fitresult->ParError(1) );
111 
112  //Write the fitting results to a csv file, these are not the same numbers as the ones written to the detector file!!
113  out_csv << string_no << "," << ref_floor << "," << tgt_floor << "," << fitresult->Parameter(1) << "," << fitresult->ParError(1) << endl;
114 
115  if(!outFile.empty()){
116  h1D->Write(); //write the fitted histograms to the root file
117  }
118  }
119  }
120 
121  // if multiple neighbours are included the average of the T0s weighted by the fitting error is taken
122  double weighted_T0 = 0;
123  double Weight;
124  double Sum_of_Weight = 0;
125  for( size_t i = 0, max = T0s.size(); i != max; ++i ){
126  Weight = 1/Errors[i];
127  weighted_T0 += T0s[i]*Weight;
128  Sum_of_Weight += Weight;
129  }
130  if(Sum_of_Weight != 0){
131  weighted_T0 /= Sum_of_Weight;
132  }
133 
134  // Add the fitting results to the map
135  map_type::iterator map_iterator = vmap.find(string_no);
136  map_iterator->second[tgt_floor-1] = weighted_T0;
137  }
138  out_csv.close();
139 
140  //For each DU subtract the average T0 from the list of T0s
141  for( int j = 1; j<=num_of_strings; j++){
142  map_type::iterator map_iterator = vmap.find(j);
143 
144  int n = map_iterator->second.size();
145  double average = accumulate( map_iterator->second.begin(), map_iterator->second.end(), 0.0)/n;
146 
147  for(int j = 0; j<n; j++ ){
148  map_iterator->second[j] = map_iterator->second[j] - average;
149  }
150  }
151 
152  ofstream out_det(outFile_det.c_str());
153  cout << " Writing det changes to: " << outFile_det << endl;
154  out_det << "String,Floor,T0" << endl;
155 
156  //Write the T0s to the detector file and write the combined fitting results to an output file
157  for(JDetector::iterator module = detector.begin(); module != detector.end(); ++module){
158  int string_no = moduleRouter.getModule( module->getID() ).getString();
159  int tgt_floor = moduleRouter.getModule( module->getID() ).getFloor();
160 
161  map_type::iterator map_iterator = vmap.find(string_no);
162  double weighted_T0 = map_iterator->second[tgt_floor-1];
163 
164  if(overwriteDetector){
165  for( int pmt = 0; pmt != KM3NETDAQ::NUMBER_OF_PMTS; ++pmt){
166  module->getPMT(pmt).addT0(weighted_T0); //All PMTs get the same T0 added because we are adding a T0 to the dom
167  }
168  }
169  out_det << string_no << "," << tgt_floor << "," << weighted_T0 << endl;
170  }
171  out_det.close();
172 
173  if(overwriteDetector){
174  cout << " Overwriting the detectorfile" << endl;
175  detector.comment.add( JMeta(argc,argv) );
176  store(detectorFile,detector);
177  }
178 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1500
Auxiliary class to convert pair of indices to unique index and back.
Detector data structure.
Definition: JDetector.hh:89
TFitResultPtr Fit(TH1D *h)
Definition: JNanobeacon.hh:14
Router for direct addressing of module data in detector data structure.
const int n
Definition: JPolint.hh:660
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
#define ERROR(A)
Definition: JMessage.hh:66
int getNumberOfFloors(const JDetector &detector)
Get number of floors.
static const JStringCounter getNumberOfStrings
Function object to count unique strings.
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
std::string to_string(const T &value)
Convert value to string.
int j
Definition: JPolint.hh:666
JComment & add(const std::string &comment)
Add comment.
Definition: JComment.hh:100
bool comparepair(const pair_type &A, const pair_type &B)
Definition: JNanobeacon.hh:10
do set_variable DETECTOR_TXT $WORKDIR detector
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
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:41
void accumulate(T &value, JBool< false > option)
Accumulation of value.