source: trunk/Mars/hawc/callisto.C@ 20084

Last change on this file since 20084 was 20042, checked in by tbretz, 4 years ago
Make sure that this change does not go unnoticed.
File size: 12.8 KB
Line 
1#include "MLogManip.h"
2
3/*****************************************************************
4
5 CALLISTO -- CALibrate LIght Signals and Time Offsets
6
7 datafile:
8 A data file written by the fadctrl, e.g.
9 20170727_006.fits.fz or
10 00000015.003_D_MonteCarlo019_Events.fits.fz
11
12 drsfile:
13 Usually the third of the three .drs.fits files from a
14 DRS calibration sequence, e.g. 20170727_004.drs.fits
15
16 drstime:
17 An input file with the constants for the DRS timing calibration
18 [Not used right now, keep it 0]
19
20 delays:
21 A file with the delays of the individual channels
22 [Not available right now, keep it 0]
23
24 outpath:
25 The path to which the output files are written
26
27
28 To run the macro from the command line (assuming you are in a directory
29 Mars/build where you have built your Mars environment) you can do
30
31 root ../hawc/callisto.C\(\"20191002_018.fits.fz\",\"20191002_013.drs.fits\"\)
32
33 or from within root
34
35 [0] .x ../hawc/callisto.C("20191002_018.fits.fz", "20191002_013.drs.fits")
36
37******************************************************************/
38int callisto(const char *datafile, const char *drsfile, bool ismc,
39 const char *outpath=".")
40{
41 // ======================================================
42
43 const char *drstime = 0;
44 const char *delays = 0;
45
46 // ======================================================
47
48 // true: Display correctly mapped pixels in the camera displays
49 // but the value-vs-index plot is in software/spiral indices
50 // false: Display pixels in hardware/linear indices,
51 // but the order is the camera display is distorted.
52 // false is assumed automatically for files with the ISMC flag set.
53 bool usemap = true;
54
55 // mapping file (found in Mars/hawc)
56 const char *mmap = usemap ? "../hawc/FAMOUSmap170719.txt": NULL;
57
58 //const char *pulse_template = "template-pulse.root";
59
60 // Define a list of defect pixels here (software index)
61 // They get interpolated from their neighbors
62 // (if there are at least three valid neighbors)
63 // The bahaviour can be altered in the corresponding task
64 // (MBadPixelsTreat)
65 MBadPixelsCam badpixels;
66 badpixels.InitSize(64);
67 badpixels[61].SetUnsuitable(MBadPixelsPix::kUnsuitable);
68 badpixels[62].SetUnsuitable(MBadPixelsPix::kUnsuitable);
69 badpixels[63].SetUnsuitable(MBadPixelsPix::kUnsuitable);
70
71 // Range in which the pulse extraction searches for the
72 // pulse (in samples).
73 const int first_slice = 250;
74 const int last_slice = 330;
75
76 // Calibration constant (for the moment a single constant to
77 // convert the extracted charge to photo-electrons)
78 //double scale = 1./22.553;//0.2;
79 double scale = 1./17.557;
80
81 // Three pixels have a slightly different gain
82 // so there signal must be attenuated
83 // THIS ONLY HAS TO BE USED FOR DATA WITH THE RESISTORS IN PLACE!!
84 MArrayD calib(64);
85 calib.Reset(1);
86 if (!ismc)
87 {
88 calib[33] = 1./0.82;
89 calib[37] = 1./0.82;
90 calib[43] = 1./0.82;
91 }
92
93 // ------------------------------------------------------
94
95 Long_t max = 0; // All
96
97 // ======================================================
98
99 // Check if the requested mapping file is available
100 if (mmap && gSystem->AccessPathName(mmap, kFileExists))
101 {
102 gLog << err << "ERROR - Cannot access mapping file '" << mmap << "'" << endl;
103 return 11;
104 }
105
106 // -------------------------------------------------------
107
108 // Check if the requested DRS time calibration constants are available
109 MDrsCalibrationTime timecam;
110 if (drstime && !timecam.ReadFits(drstime))
111 {
112 gLog << err << "ERROR - Could not get MDrsCalibrationTime from " << drstime << endl;
113 return 21;
114 }
115
116 if (delays)
117 {
118 TGraph g(delays);
119 if (g.GetN()!=1440)
120 {
121 gLog << err << "Error reading file " << delays << endl;
122 return 22;
123 }
124
125 timecam.SetDelays(g);
126 }
127
128 // ------------------------------------------------------
129
130 // Check and read the channel delays
131 if (drstime && delays)
132 {
133 TGraph g(delays);
134 if (g.GetN()!=1440)
135 {
136 gLog << err << "Error reading file " << delays << endl;
137 return 41;
138 }
139
140 timecam.SetDelays(g);
141 }
142
143 // -------------------------------------------------------
144
145 // Check and read the DRS calibration constants
146 MDrsCalibration drscalib300;
147 if (drsfile && !drscalib300.ReadFits(drsfile))
148 return 31;
149
150 // ======================================================
151
152 gLog.Separator("Callisto");
153 gLog << all;
154 gLog << "Data file: " << datafile << '\n';
155 if (drsfile)
156 gLog << "DRS Calib: " << drsfile << '\n';
157 if (drstime)
158 gLog << "DRS Time: " << drstime << '\n';
159 if (delays)
160 gLog << "Delays: " << delays << '\n';
161 gLog << "Outpath: " << outpath << '\n';
162 gLog << endl;
163
164 // ------------------------------------------------------
165
166 gStyle->SetOptFit(kTRUE);
167
168 // ===================================================================
169
170 gLog << endl;
171 gLog.Separator("Calibrating data");
172
173 // Instantiate the list of tasks
174 MTaskList tlist5;
175
176 // Instantiate the list of data containers
177 MParList plist5;
178 plist5.AddToList(&tlist5);
179 plist5.AddToList(&badpixels);
180 if (drsfile)
181 plist5.AddToList(&drscalib300);
182 if (drstime)
183 plist5.AddToList(&timecam);
184
185 // Instantiate the camera geometry
186 MGeomCamFAMOUS geom;
187 plist5.AddToList(&geom);
188
189 // Instantiate the status display
190 MStatusDisplay *d = new MStatusDisplay;
191
192 // Instantiate the event loop
193 MEvtLoop loop5("CalibratingData");
194 loop5.SetDisplay(d);
195 loop5.SetParList(&plist5);
196
197 // ------------------ Setup the tasks ---------------
198
199 MDirIter files(datafile);
200
201 // Instantiate the reading task
202 // You can use
203 // read5.AddFiles("*.fits.fz")
204 // for example to read more than one file at once
205 MRawFitsRead read5;
206 read5.AddFiles(files);
207 if (mmap)
208 read5.LoadMap(mmap);
209
210 // Instantiate the histogram to be filled with the rates
211 MH3 hrate("MRawRunHeader.GetFileID", "int(MRawEvtHeader.GetTriggerID)&0xff00");
212 hrate.SetWeight("1./TMath::Max(MRawRunHeader.GetRunLength,1.)");
213 hrate.SetName("Rate");
214 hrate.SetTitle("Event rate [Hz];File Id;Trigger Type;");
215 hrate.InitLabels(MH3::kLabelsXY);
216 hrate.DefineLabelY( 0, "Data"); // What if TriggerID==0 already???
217 hrate.DefineLabelY(0x100, "Cal");
218 hrate.DefineLabelY(0x400, "Ped");
219 // hrate.DefaultLabelY("ERROR");
220
221 // Instantiate the task to fill therate histogram
222 MFillH fill5a(&hrate);
223
224 // Instantiate the task to ensure proper sizing of all containers
225 MGeomApply apply5;
226
227 // Instantiate the task to apply the drs calibration constants
228 MDrsCalibApply drsapply5;
229 drsapply5.SetMaxNumPrevEvents(0); // Switched off step-correction -> crashes due to the requirement of N*9 ch
230
231 // Instantiate the filter to select only physics events
232 MFDataPhrase filterdat("(int(MRawEvtHeader.GetTriggerID)&0xff00)==0", "SelectDat");
233
234 // ---
235/*
236 MExtractTimeAndChargeSpline extractor5dat;
237 extractor5dat.SetRange(first_slice, last_slice);
238 extractor5dat.SetRiseTimeHiGain(rise_time_dat);
239 extractor5dat.SetFallTimeHiGain(fall_time_dat);
240 extractor5dat.SetHeightTm(heighttm);
241 extractor5dat.SetChargeType(type);
242 extractor5dat.SetSaturationLimit(600000);
243 extractor5dat.SetNoiseCalculation(kFALSE);
244*/
245
246 // Instantiate the digital filter (weighted sliding average) to clean the data
247 // Note that the weights are not adapted yet to HAWC's Eye
248 // A better filter might be worth to think about
249 MFilterData filterdata1;
250
251 // Instatiate task to extract the maximum of the pulse (correspondins to
252 // a weighted integral) and the position of the leading edge between
253 // first_slice and last_slice as charge and arrival time
254 MExtractFACT extractor5dat("ExtractPulse");
255 extractor5dat.SetRange(first_slice, last_slice);
256 extractor5dat.SetNoiseCalculation(kFALSE);
257
258 // Instantiate task which applies the calibration constants
259 MCalibrateFact conv5;
260 conv5.SetScale(scale);
261 conv5.SetCalibConst(calib);
262
263 // Instantiate task which applies the DRS time calibration
264 MCalibrateDrsTimes calctm5;
265 calctm5.SetNameUncalibrated("UncalibratedTimes");
266
267 // Instantiate task to interpolate bad pixels
268 MBadPixelsTreat treat5;
269 treat5.SetProcessPedestalRun(kFALSE);
270 treat5.SetProcessPedestalEvt(kFALSE);
271
272 // Instantiate histograms to display charges
273 MHCamEvent evt5b(0, "ExtSig", "Extracted signal;;S [mV#cdot sl]");
274 MHCamEvent evt5c(0, "CalSig", "Calibrated and interpolated signal;;S [~phe]");
275 MHCamEvent evt5d(4, "ExtSigTm", "Extracted time;;T [sl]");
276 MHCamEvent evt5e(6, "CalSigTm", "Calibrated and interpolated time;;T [ns]");
277
278 // Instantiate task to fill those histograms
279 MFillH fill5b(&evt5b, "MExtractedSignalCam", "FillExtSig");
280 MFillH fill5c(&evt5c, "MSignalCam", "FillCalSig");
281 MFillH fill5d(&evt5d, "MArrivalTimeCam", "FillExtTm");
282 MFillH fill5e(&evt5e, "MSignalCam", "FillCalTm");
283
284 // Setup those histograms to fit a gaussian to the distribution
285 fill5c.SetDrawOption("gaus");
286 fill5d.SetDrawOption("gaus");
287 fill5e.SetDrawOption("gaus");
288
289 // Instantiate the task to apply the software trigger
290 MSoftwareTriggerCalc swtrig;
291
292 // This is an alternative if more than one file should be read in a single loop
293 // In this case the output file name of each file is created with this rule
294 // from the input file name. Note that the filename of the output file
295 // with the status display must then be explicitly given.
296 //
297 //const TString fname(Form("s/([0-9]+_[0-9]+)[.]fits([.][fg]z)?$/%s\\/$1_C.root/",
298 // MJob::Esc(outpath).Data()));
299 //MWriteRootFile write5(2, fname, "RECREATE", "Calibrated Data");
300
301 /*
302 // Convert the name of the input file to the name of the output file
303 TString fname = gSystem->ConcatFileName(outpath, gSystem->BaseName(datafile));
304 fname.ReplaceAll(".fits.gz", ".root");
305 fname.ReplaceAll(".fits.fz", ".root");
306 fname.ReplaceAll(".fits", ".root");
307 fname.ReplaceAll("_D_", "_C_");
308
309 gSystem->ExpandPathName(fname);
310 */
311
312 const TString rule(Form("s/([0-9]+)([._])([0-9]+)(_[DPC])?([_].*)?([.]fits([.]fz)?)$/%s\\/$1$2$3_Y$5.root/",
313 MJob:: Esc(outpath).Data()));
314
315 // Instantitate the writing task and setup the writing
316 MWriteRootFile write5(2, rule, "RECREATE", "Calibrated Data");
317 write5.AddContainer("MSignalCam", "Events");
318 write5.AddContainer("MRawEvtHeader", "Events");
319 write5.AddContainer("MSoftwareTrigger","Events");
320 write5.AddContainer("MRawRunHeader", "RunHeaders");
321 write5.AddContainer("MGeomCam", "RunHeaders");
322
323 write5.AddContainer("MMcCorsikaRunHeader", "RunHeaders", kFALSE);
324 write5.AddContainer("MCorsikaRunHeader", "RunHeaders", kFALSE);
325 write5.AddContainer("MMcRunHeader", "RunHeaders", kFALSE);
326 write5.AddContainer("MCorsikaEvtHeader", "Events", kFALSE);
327 write5.AddContainer("MMcEvt", "Events", kFALSE);
328 write5.AddContainer("IncidentAngle", "Events", kFALSE);
329 write5.AddContainer("MPointingPos", "Events", kFALSE);
330 write5.AddContainer("MSimSourcePos", "Events", kFALSE);
331 write5.AddContainer("MTime", "Events", kFALSE);
332 write5.AddContainer("MRawBoardsFACT", "Events", kFALSE);
333 write5.AddContainer("MSrcPosCam", "Events", kFALSE);
334
335 // ------------------ Setup histograms and fill tasks ----------------
336
337 // Instantitate the task list and setp the list
338 MTaskList tlist5dat;
339 tlist5dat.AddToList(&fill5b);
340 tlist5dat.AddToList(&fill5c);
341 tlist5dat.AddToList(&fill5d);
342 tlist5dat.AddToList(&fill5e);
343 tlist5dat.SetFilter(&filterdat);
344
345 tlist5.AddToList(&read5);
346 tlist5.AddToList(&apply5);
347 tlist5.AddToList(&drsapply5);
348 tlist5.AddToList(&fill5a);
349 tlist5.AddToList(&swtrig);
350 tlist5.AddToList(&filterdata1);
351 tlist5.AddToList(&extractor5dat);
352 tlist5.AddToList(&calctm5);
353 tlist5.AddToList(&conv5);
354 tlist5.AddToList(&treat5);
355 tlist5.AddToList(&filterdat);
356 tlist5.AddToList(&tlist5dat);
357 tlist5.AddToList(&write5);
358
359 // run the eventloop
360 if (!loop5.Eventloop(max))
361 return 18;
362
363 // Check if the display was closed (deleted) by the user
364 if (!loop5.GetDisplay())
365 return 19;
366
367 // ============================================================
368
369 TString fname = write5.GetFileName();
370 fname.ReplaceAll(".root", "-display.root");
371
372 // Write the status display to the file
373 TString title = "-- Calibrated signal [";
374 title += datafile;
375 title += "] --";
376 d->SetTitle(title, kFALSE);
377
378 d->SaveAsRoot(fname);
379
380 return 0;
381}
Note: See TracBrowser for help on using the repository browser.