Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JFitNB.cc
Go to the documentation of this file.
1#include <iostream>
2#include <string>
3#include <vector>
4#include <map>
5#include <numeric>
6
7#include "Jeep/JParser.hh"
13#include "JSupport/JMeta.hh"
14
15#include "TH1.h"
16#include "TH2.h"
17#include "TFile.h"
18#include "TKey.h"
19#include "TF1.h"
20#include "TFitResult.h"
21
22#include "JNanobeacon.hh"
23
24using namespace std;
25using namespace JPP;
26using namespace KM3NETDAQ;
27
28int main(int argc , char** argv){
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);
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
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}
KM3NeT DAQ constants, bit handling, etc.
Data structure for detector geometry and calibration.
int main(int argc, char **argv)
Definition JFitNB.cc:28
ROOT I/O of application specific meta data.
Direct access to module in detector data structure.
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
Detector data structure.
Definition JDetector.hh:96
int getFloor() const
Get floor number.
Definition JLocation.hh:146
int getString() const
Get string number.
Definition JLocation.hh:135
Router for direct addressing of module data in detector data structure.
const JModule & getModule(const JObjectID &id) const
Get module parameters.
Utility class to parse command line options.
Definition JParser.hh:1698
Auxiliary class to convert pair of indices to unique index and back.
int getIndex(const int first, const int second) const
Get index of pair of indices.
void sort(JComparator_t comparator)
Sort address pairs.
int getNumberOfFloors(const JDetector &detector)
Get number of floors.
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
static const JStringCounter getNumberOfStrings
Function object to count unique strings.
std::string to_string(const T &value)
Convert value to string.
TFitResultPtr Fit(TH1D *h)
bool comparepair(const pair_type &A, const pair_type &B)
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
void accumulate(T &value, JBool< false > option)
Accumulation of value.
const int n
Definition JPolint.hh:791
int j
Definition JPolint.hh:801
KM3NeT DAQ data structures and auxiliaries.
Definition DataQueue.cc:39
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition JDAQ.hh:26
std::map< int, range_type > map_type
Detector file.
Definition JHead.hh:227
Auxiliary class for ROOT I/O of application specific meta data.
Definition JMeta.hh:72