ThresholdCalculator.cc
Go to the documentation of this file.
1 /*
2  Module to calculate the threshold values for FD-Simulation
3  \author Joachim Debatin
4  */
5 
6 #include "ThresholdCalculator.h"
7 
8 #include <utl/ErrorLogger.h>
9 #include <utl/Reader.h>
10 #include <utl/TabulatedFunction.h>
11 #include <utl/MathConstants.h>
12 #include <utl/AugerUnits.h>
13 #include <utl/TimeStamp.h>
14 #include <utl/UTCDateTime.h>
15 #include <utl/Md5Sum.h>
16 
17 #include <fwk/CentralConfig.h>
18 
19 #include <evt/Event.h>
20 
21 #include <fevt/FEvent.h>
22 #include <fevt/Eye.h>
23 #include <fevt/Telescope.h>
24 #include <fevt/TelescopeSimData.h>
25 #include <fevt/PixelSimData.h>
26 #include <fevt/Pixel.h>
27 #include <fevt/ChannelSimData.h>
28 #include <fevt/Channel.h>
29 
30 #include <det/Detector.h>
31 
32 #include <fdet/FDetector.h>
33 #include <fdet/Eye.h>
34 #include <fdet/Telescope.h>
35 #include <fdet/Channel.h>
36 #include <fdet/Pixel.h>
37 
38 #include <TFile.h>
39 
40 #include <string>
41 
42 using namespace ThresholdCalculatorKG;
43 using namespace std;
44 using namespace utl;
45 using namespace fwk;
46 using namespace fevt;
47 using namespace det;
48 using namespace fdet;
49 
51  fVerbosityLevel(0),
52  fRootFileName("out.root")
53 {
54 }
55 
57 {
58 }
59 
62 {
63 
64  CentralConfig* cc = CentralConfig::GetInstance();
65  Branch topB = cc->GetTopBranch("ThresholdCalculator");
66 
67  fVerbosityLevel = 0;
68  if (topB.GetChild("verbosityLevel"))
69  topB.GetChild("verbosityLevel").GetData(fVerbosityLevel);
70 
71  //Get Npe from MeanPeFiller
72  Branch topB2 = cc->GetTopBranch("MeanNpeFiller");
73  if (topB2.GetChild("Npe")) {
74  topB2.GetChild("Npe").GetData(fNpe);
75  }
76 
77  if (topB2.GetChild("Eye"))
78  topB2.GetChild("Eye").GetData(fEye);
79 
80  if (topB2.GetChild("Tel"))
81  topB2.GetChild("Tel").GetData(fTel);
82 
83  Detector::GetInstance().Update(UTCDateTime(2012,1,1,0,0,0).GetTimeStamp());
84  const Camera& detCamera = Detector::GetInstance().GetFDetector().GetEye(fEye).GetTelescope(fTel).GetCamera();
86 
87  //Get Baseline form ElectronicsSimulator
88  Branch topB3 = cc->GetTopBranch("FdElectronicsSimulator");
89  topB3.GetChild("baseline").GetData(fBaseline);
90 
91  if (topB.GetChild("RootFileName"))
92  topB.GetChild("RootFileName").GetData(fRootFileName);
93  fHistoFile = new TFile(fRootFileName.c_str(), "RECREATE");
94 
95  //Calculate histogram boundaries
96  const double gain = detCamera.GetElectronicsGain();
97  const double electronicVariance = detCamera.GetElectronicNoiseVariance();
98 
99  for (unsigned int i=0; i<fNpe.size(); ++i){
100  ostringstream sampleName;
101  sampleName << "BinSum" << i+1;
102 
103  int NSamplesMin = int(fBoxcarSumLength * fBaseline);
104 
105  //Factor of 3 is manually optimised. If yout get warnigs enlarge.
106  int NSamplesMax = int(fBoxcarSumLength * (fBaseline + electronicVariance + 3 * sqrt(fNpe[i] * gain)) + .5);
107 
108  fSamples.push_back(new TH1D(sampleName.str().c_str(), sampleName.str().c_str(), NSamplesMax - NSamplesMin + 1, NSamplesMin-.5, NSamplesMax+.5));
109 
110  //Histogram maximum bin
111  fSampleMaximum.push_back(NSamplesMax);
112 
113  fNumberOfTraces.push_back(0);
114  }
115 
116  // info output
117  ostringstream info;
118  info << "Version: "
119  << GetVersionInfo(VModule::eRevisionNumber) << "\n";
120 
121  if (fVerbosityLevel)
122  info << "Verbosity level: " << fVerbosityLevel << '\n';
123 
124  if (fBoxcarSumLength)
125  info << "Used NSamples value: " << fBoxcarSumLength << "\n";
126 
127  INFO(info);
128 
129  return eSuccess;
130 }
131 
132 
135 {
136 
137  FEvent& fEvent = event.GetFEvent();
138  for (fevt::FEvent::EyeIterator iEye =
141  ++iEye) {
142 
143  if (iEye->GetStatus()==fevt::ComponentSelector::eDeSelected)
144  continue;
145 
146  fevt::Eye& eyeEvent = *iEye;
147 
148  for (fevt::Eye::TelescopeIterator iTel =
151  ++iTel) {
152 
153  //GetFDetector
154  const fdet::FDetector& detFD = Detector::GetInstance().GetFDetector();
155  fevt::Telescope& telEvent = *iTel;
156  const fdet::Telescope& detTel = detFD.GetTelescope(telEvent);
157 
158  //Needed to correct for scaling in ElectronicsSimulator
159  const double normWavelength = detFD.GetReferenceLambda();
160 
161  //Loop pixels/channels
162  for (unsigned int channelId=1; channelId<=detTel.GetLastPixelId(); ++channelId) {
163 
164  const fdet::Channel& detChannel = detTel.GetChannel(channelId);
165  unsigned int pixelId = detChannel.GetPixelId();
166 
167  if (!telEvent.HasChannel(channelId))
168  continue;
169  fevt::Channel& channelEvent = telEvent.GetChannel(channelId);
170 
171  if (!channelEvent.HasSimData())
172  continue;
173  fevt::ChannelSimData& channel_sim = channelEvent.GetSimData();
174 
175  if (!channel_sim.HasFADCTrace (fevt::FdConstants::eTotal))
176  continue;
177 
178  if (!telEvent.HasPixel(pixelId))
179  continue;
180  fevt::Pixel& pixelEvent = telEvent.GetPixel(pixelId);
181 
182  if (pixelEvent.GetStatus() == ComponentSelector::eDeSelected) {
183  continue;
184  }
185 
186  if (!pixelEvent.HasSimData())
187  continue;
188  fevt::PixelSimData& simPixel = pixelEvent.GetSimData();
189 
190  const fdet::Pixel& detPixel = detFD.GetPixel(pixelEvent);
191  const TabulatedFunction& qEff = detPixel.GetQEfficiency();
192  const double qEffAtNormWavelength = qEff.Y(normWavelength);
193 
194  //Get meanPe cleaned of scaling in MeanPeFiller
195  const double meanPe = simPixel.GetMeanBgPhotonFlux() * qEffAtNormWavelength;
196 
197  int meanPeBin = 0;
198  for(unsigned int i = 0; i < fNpe.size(); ++i){
199  if(abs(meanPe-fNpe[i])<.1){
200  meanPeBin = i;
201  }
202  }
203 
204  //Calculate boxcarsums
205  const TraceI& fadc_trace = channel_sim.GetFADCTrace(fevt::FdConstants::eTotal);
206  vector<int> boxcar(fBoxcarSumLength, 0);
207  fNumberOfTraces[meanPeBin]+=1;
208 
209  for (unsigned int bin=0; bin<fadc_trace.GetSize(); ++bin){
210  // build boxcar
211  for (unsigned int i=0; i<fBoxcarSumLength-1; ++i)
212  boxcar[i] = boxcar[i+1];
213  boxcar[fBoxcarSumLength-1] = fadc_trace [bin];
214  int sum = 0;
215  for (int i=fBoxcarSumLength-1; i >= 0; --i){
216  if(boxcar[i]==0){
217  sum = 0;
218  break;
219  }else{
220  sum+=boxcar[i];
221  }
222  }
223  if(sum){
224  fSamples[meanPeBin]->Fill(sum);
225  if(fVerbosityLevel>=1){
226  if(sum > fSampleMaximum[meanPeBin]){
227  ostringstream msg;
228  msg << "BoxCarSum bigger than maximum of histogram. Calculation is wrong!! " << sum << " " << fSampleMaximum[meanPeBin];
229  WARNING(msg);
230  }
231  }
232  }
233  }
234  }
235  }
236  }
237  return eSuccess;
238 }
239 
240 
243 {
244 
245  const fdet::Camera & detCamera = Detector::GetInstance().GetFDetector().GetEye(fEye).GetTelescope(fTel).GetCamera();
246 
247  //Cumulative histograms
248  for (unsigned int i=0; i<fSamples.size(); ++i){
249  fSummedSamples.push_back(fSamples[i]->GetCumulative(false));
250  }
251 
252  //convert count to frequency
253  for (unsigned int i=0; i<fSummedSamples.size(); ++i){
254  fSummedSamples[i]->Scale(1. / (fNumberOfTraces[i] * detCamera.GetFADCTraceLength() * detCamera.GetFADCBinSize()));
255  }
256 
257  //Create threshold signature
258  const double FLTTriggerRate = detCamera.GetFLTTriggerRate();
259  ostringstream ThresholdSignature;
260  ThresholdSignature << "Threshold:"
261  << " Gain = " << detCamera.GetElectronicsGain()
262  << " GainVariance = " << detCamera.GetGainVariance()
263  << " ElectronicNoise = " << detCamera.GetElectronicNoiseVariance()
264  << " CutoffFrequency = " << detCamera.GetCutoffFrequency()
265  << " FLTTriggerRate = " << detCamera.GetFLTTriggerRate()
266  << " FLTBoxcarSize = " << fBoxcarSumLength;
267 
268  if (fEye>0 && fEye<5){
269  if (fTel>0 && fTel<7){
270  ThresholdSignature << " Telescopetype = standard";
271  }
272  }else if (fEye==5){
273  if (fTel>0 && fTel<4){
274  ThresholdSignature << " Telescopetype = HEAT";
275  }
276  }
277 
278  //Start OutputFile
279  ofstream thresholdXml("FSimulationThreshold.generated.xml.in");
280  thresholdXml << "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n\n"
281  << "<FSimulationThreshold xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
282  << " xsi:noNamespaceSchemaLocation='@XMLSCHEMALOCATION@/FSimulationThreshold.xsd'>\n\n"
283  << " <Threshold signature=\"" << Md5Sum(ThresholdSignature.str()).GetHexDigest() << "\"> \n"
284  << " <!--" << ThresholdSignature.str() <<"-->\n";
285 
286  //Calculate Threshold
287  double xValues[fSummedSamples.size()];
288  int yValues[fSummedSamples.size()];
289  for (unsigned int i=0; i<fSummedSamples.size(); ++i){
290  int x = 0;
291  int& binnumber = x;
292  const double dummy = fSummedSamples[i]->GetBinWithContent(FLTTriggerRate, binnumber, 0, fSummedSamples[i]->GetNbinsX(), 100);
293  const int threshold = int(fSummedSamples[i]->GetBinCenter(binnumber) - fBoxcarSumLength * fBaseline - .5);
294 
295  if (fVerbosityLevel>=1){
296  ostringstream info;
297  info << "Result of Threshold Calculation for"
298  << " meanPe = " << fNpe[i]
299  << " Threshold = " << threshold
300  << " Uncertainty = " << dummy
301  << " Binnumber = " << x;
302  INFO(info);
303  }
304 
305  xValues[i] = fNpe[i];
306  yValues[i] = threshold;
307  }
308 
309  //Write threshold to xml
310  thresholdXml << " <x>";
311  for(unsigned int i = 0; i < fSummedSamples.size(); ++i)
312  thresholdXml << " " << xValues[i];
313  thresholdXml << "</x>\n";
314 
315  thresholdXml << " <y>";
316  for(unsigned int i = 0; i < fSummedSamples.size(); ++i)
317  thresholdXml << " " << yValues[i];
318  thresholdXml << "</y>\n";
319 
320  thresholdXml << " </Threshold>\n\n"
321  << "</FSimulationThreshold>";
322 
323  fHistoFile->cd();
324  fHistoFile->Write();
325  fHistoFile->Close();
326 
327  return eSuccess;
328 }
329 
330 
fwk::VModule::ResultFlag Init()
Initialize: invoked at beginning of run (NOT beginning of event)
fwk::VModule::ResultFlag Run(evt::Event &event)
Run: invoked once per event.
Class to compute MD5 checksum Based on the RSA C code, wrapped in an OO fashion.
Definition: Md5Sum.h:27
ComponentSelector::Status GetStatus() const
Definition: FEvent/Pixel.h:54
double GetFADCBinSize() const
Description of the electronic channel for the 480 channels of the crate.
Report success to RunController.
Definition: VModule.h:62
Fluorescence Detector Eye Event.
Definition: FEvent/Eye.h:29
double GetElectronicsGain() const
const Pixel & GetPixel(const fevt::Pixel &eventPixel) const
Get fdet::Pixel from fevt::Channel.
Definition: FDetector.cc:198
Class to hold collection (x,y) points and provide interpolation between them.
double GetCutoffFrequency() const
double GetMeanBgPhotonFlux() const
Get mean bg photon flux.
Definition: PixelSimData.h:137
EyeIterator EyesEnd(const ComponentSelector::Status status)
Definition: FEvent.h:66
double GetGainVariance() const
const Channel & GetChannel(const unsigned int channelId) const
Get Channel by id, throw utl::NonExistentComponentException if n.a.
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
PixelSimData & GetSimData()
Definition: FEvent/Pixel.h:35
bool HasFADCTrace(const FdConstants::LightSource source) const
Check that source /par source is present.
boost::filter_iterator< ComponentSelector, AllEyeIterator > EyeIterator
selective Eye iterators
Definition: FEvent.h:55
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
Fluorescence Detector Channel Simulated Data Event.
int gain
Definition: dump1090.h:241
std::string GetVersionInfo(const VersionInfoType v) const
Retrieve different sorts of module version info.
Definition: VModule.cc:26
double GetFLTTriggerRate() const
ChannelSimData & GetSimData()
int GetFLTBoxcarSumLength() const
Detector description interface for FDetector-related data.
Definition: FDetector.h:44
bool HasSimData() const
static int binnumber
Definition: XbAlgo.cc:25
bool HasPixel(const unsigned int pixelId, const ComponentSelector::Status status=ComponentSelector::eHasData) const
Check if the pixel is in the event.
Class representing a document branch.
Definition: Branch.h:107
unsigned int GetLastPixelId() const
Channel & GetChannel(const unsigned int channelId)
Fluorescence Detector Pixel event.
Definition: FEvent/Pixel.h:28
const utl::TabulatedFunction & GetQEfficiency() const
Average quantum efficiency as a function of the wavelength.
TelescopeIterator TelescopesEnd()
first available tel of status eHasData (DEPRECATED)
Definition: FEvent/Eye.cc:230
double abs(const SVector< n, T > &v)
EyeIterator EyesBegin(const ComponentSelector::Status status)
Definition: FEvent.h:58
utl::TraceI & GetFADCTrace(const FdConstants::LightSource source=FdConstants::eTotal)
bool HasSimData() const
Definition: FEvent/Pixel.h:38
boost::filter_iterator< ComponentSelector, AllTelescopeIterator > TelescopeIterator
selective Telescope iterators
Definition: FEvent/Eye.h:72
SizeType GetSize() const
Definition: Trace.h:156
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
Fluorescence Detector Channel Event.
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
Top of Fluorescence Detector event hierarchy.
Definition: FEvent.h:33
TelescopeIterator TelescopesBegin()
first available tel of status eHasData (DEPRECATED)
Definition: FEvent/Eye.cc:207
double GetElectronicNoiseVariance() const
double GetReferenceLambda() const
Definition: FDetector.cc:332
fwk::VModule::ResultFlag Finish()
Finish: invoked at end of the run (NOT end of the event)
Pixel & GetPixel(const unsigned int pixelId, const ComponentSelector::Status status=ComponentSelector::eHasData)
Retrieve Pixel by Id, throw exception if not existent.
Detector description interface for Telescope-related data.
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
bool HasChannel(const unsigned int channelId) const
const Telescope & GetTelescope(const fevt::Telescope &eventTel) const
Get fdet::Telescope from fevt::Telescope.
Definition: FDetector.cc:150
Description of a pixel.
Main configuration utility.
Definition: CentralConfig.h:51
Fluorescence Detector Telescope Event.
double Y(const double x) const
Get or interpolate the Y value that corresponds to parameter x.
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)
Fluorescence Detector Pixel Simulated Data.
Definition: PixelSimData.h:28
Description of a camera.
unsigned int GetPixelId() const

, generated on Tue Sep 26 2023.