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

Last change on this file since 20025 was 20025, checked in by tbretz, 4 years ago
Added updated calibration factor for the three 'special' pixels.
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,
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 ? "/home/hawc/FACT++-src/famous/resources/hawcseye/HAWCsEyemap181214.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 calib[33] = 1./0.82;
87 calib[37] = 1./0.82;
88 calib[43] = 1./0.82;
89
90 // ------------------------------------------------------
91
92 Long_t max = 0; // All
93
94 // ======================================================
95
96 // Check if the requested mapping file is available
97 if (mmap && gSystem->AccessPathName(mmap, kFileExists))
98 {
99 gLog << err << "ERROR - Cannot access mapping file '" << mmap << "'" << endl;
100 return 11;
101 }
102
103 // -------------------------------------------------------
104
105 // Check if the requested DRS time calibration constants are available
106 MDrsCalibrationTime timecam;
107 if (drstime && !timecam.ReadFits(drstime))
108 {
109 gLog << err << "ERROR - Could not get MDrsCalibrationTime from " << drstime << endl;
110 return 21;
111 }
112
113 if (delays)
114 {
115 TGraph g(delays);
116 if (g.GetN()!=1440)
117 {
118 gLog << err << "Error reading file " << delays << endl;
119 return 22;
120 }
121
122 timecam.SetDelays(g);
123 }
124
125 // ------------------------------------------------------
126
127 // Check and read the channel delays
128 if (drstime && delays)
129 {
130 TGraph g(delays);
131 if (g.GetN()!=1440)
132 {
133 gLog << err << "Error reading file " << delays << endl;
134 return 41;
135 }
136
137 timecam.SetDelays(g);
138 }
139
140 // -------------------------------------------------------
141
142 // Check and read the DRS calibration constants
143 MDrsCalibration drscalib300;
144 if (drsfile && !drscalib300.ReadFits(drsfile))
145 return 31;
146
147 // ======================================================
148
149 gLog.Separator("Callisto");
150 gLog << all;
151 gLog << "Data file: " << datafile << '\n';
152 if (drsfile)
153 gLog << "DRS Calib: " << drsfile << '\n';
154 if (drstime)
155 gLog << "DRS Time: " << drstime << '\n';
156 if (delays)
157 gLog << "Delays: " << delays << '\n';
158 gLog << "Outpath: " << outpath << '\n';
159 gLog << endl;
160
161 // ------------------------------------------------------
162
163 gStyle->SetOptFit(kTRUE);
164
165 // ===================================================================
166
167 gLog << endl;
168 gLog.Separator("Calibrating data");
169
170 // Instantiate the list of tasks
171 MTaskList tlist5;
172
173 // Instantiate the list of data containers
174 MParList plist5;
175 plist5.AddToList(&tlist5);
176 plist5.AddToList(&badpixels);
177 if (drsfile)
178 plist5.AddToList(&drscalib300);
179 if (drstime)
180 plist5.AddToList(&timecam);
181
182 // Instantiate the camera geometry
183 MGeomCamFAMOUS geom;
184 plist5.AddToList(&geom);
185
186 // Instantiate the status display
187 MStatusDisplay *d = new MStatusDisplay;
188
189 // Instantiate the event loop
190 MEvtLoop loop5("CalibratingData");
191 loop5.SetDisplay(d);
192 loop5.SetParList(&plist5);
193
194 // ------------------ Setup the tasks ---------------
195
196 MDirIter files(datafile);
197
198 // Instantiate the reading task
199 // You can use
200 // read5.AddFiles("*.fits.fz")
201 // for example to read more than one file at once
202 MRawFitsRead read5;
203 read5.AddFiles(files);
204 if (mmap)
205 read5.LoadMap(mmap);
206
207 // Instantiate the histogram to be filled with the rates
208 MH3 hrate("MRawRunHeader.GetFileID", "int(MRawEvtHeader.GetTriggerID)&0xff00");
209 hrate.SetWeight("1./TMath::Max(MRawRunHeader.GetRunLength,1.)");
210 hrate.SetName("Rate");
211 hrate.SetTitle("Event rate [Hz];File Id;Trigger Type;");
212 hrate.InitLabels(MH3::kLabelsXY);
213 hrate.DefineLabelY( 0, "Data"); // What if TriggerID==0 already???
214 hrate.DefineLabelY(0x100, "Cal");
215 hrate.DefineLabelY(0x400, "Ped");
216 // hrate.DefaultLabelY("ERROR");
217
218 // Instantiate the task to fill therate histogram
219 MFillH fill5a(&hrate);
220
221 // Instantiate the task to ensure proper sizing of all containers
222 MGeomApply apply5;
223
224 // Instantiate the task to apply the drs calibration constants
225 MDrsCalibApply drsapply5;
226 drsapply5.SetMaxNumPrevEvents(0); // Switched off step-correction -> crashes due to the requirement of N*9 ch
227
228 // Instantiate the filter to select only physics events
229 MFDataPhrase filterdat("(int(MRawEvtHeader.GetTriggerID)&0xff00)==0", "SelectDat");
230
231 // ---
232/*
233 MExtractTimeAndChargeSpline extractor5dat;
234 extractor5dat.SetRange(first_slice, last_slice);
235 extractor5dat.SetRiseTimeHiGain(rise_time_dat);
236 extractor5dat.SetFallTimeHiGain(fall_time_dat);
237 extractor5dat.SetHeightTm(heighttm);
238 extractor5dat.SetChargeType(type);
239 extractor5dat.SetSaturationLimit(600000);
240 extractor5dat.SetNoiseCalculation(kFALSE);
241*/
242
243 // Instantiate the digital filter (weighted sliding average) to clean the data
244 // Note that the weights are not adapted yet to HAWC's Eye
245 // A better filter might be worth to think about
246 MFilterData filterdata1;
247
248 // Instatiate task to extract the maximum of the pulse (correspondins to
249 // a weighted integral) and the position of the leading edge between
250 // first_slice and last_slice as charge and arrival time
251 MExtractFACT extractor5dat("ExtractPulse");
252 extractor5dat.SetRange(first_slice, last_slice);
253 extractor5dat.SetNoiseCalculation(kFALSE);
254
255 // Instantiate task which applies the calibration constants
256 MCalibrateFact conv5;
257 conv5.SetScale(scale);
258 conv5.SetCalibConst(calib);
259
260 // Instantiate task which applies the DRS time calibration
261 MCalibrateDrsTimes calctm5;
262 calctm5.SetNameUncalibrated("UncalibratedTimes");
263
264 // Instantiate task to interpolate bad pixels
265 MBadPixelsTreat treat5;
266 treat5.SetProcessPedestalRun(kFALSE);
267 treat5.SetProcessPedestalEvt(kFALSE);
268
269 // Instantiate histograms to display charges
270 MHCamEvent evt5b(0, "ExtSig", "Extracted signal;;S [mV#cdot sl]");
271 MHCamEvent evt5c(0, "CalSig", "Calibrated and interpolated signal;;S [~phe]");
272 MHCamEvent evt5d(4, "ExtSigTm", "Extracted time;;T [sl]");
273 MHCamEvent evt5e(6, "CalSigTm", "Calibrated and interpolated time;;T [ns]");
274
275 // Instantiate task to fill those histograms
276 MFillH fill5b(&evt5b, "MExtractedSignalCam", "FillExtSig");
277 MFillH fill5c(&evt5c, "MSignalCam", "FillCalSig");
278 MFillH fill5d(&evt5d, "MArrivalTimeCam", "FillExtTm");
279 MFillH fill5e(&evt5e, "MSignalCam", "FillCalTm");
280
281 // Setup those histograms to fit a gaussian to the distribution
282 fill5c.SetDrawOption("gaus");
283 fill5d.SetDrawOption("gaus");
284 fill5e.SetDrawOption("gaus");
285
286 // Instantiate the task to apply the software trigger
287 MSoftwareTriggerCalc swtrig;
288
289 // This is an alternative if more than one file should be read in a single loop
290 // In this case the output file name of each file is created with this rule
291 // from the input file name. Note that the filename of the output file
292 // with the status display must then be explicitly given.
293 //
294 //const TString fname(Form("s/([0-9]+_[0-9]+)[.]fits([.][fg]z)?$/%s\\/$1_C.root/",
295 // MJob::Esc(outpath).Data()));
296 //MWriteRootFile write5(2, fname, "RECREATE", "Calibrated Data");
297
298 /*
299 // Convert the name of the input file to the name of the output file
300 TString fname = gSystem->ConcatFileName(outpath, gSystem->BaseName(datafile));
301 fname.ReplaceAll(".fits.gz", ".root");
302 fname.ReplaceAll(".fits.fz", ".root");
303 fname.ReplaceAll(".fits", ".root");
304 fname.ReplaceAll("_D_", "_C_");
305
306 gSystem->ExpandPathName(fname);
307 */
308
309 const TString rule(Form("s/([0-9]+)([._])([0-9]+)(_[DPC])?([_].*)?([.]fits([.]fz)?)$/%s\\/$1$2$3_Y$5.root/",
310 MJob:: Esc(outpath).Data()));
311
312 // Instantitate the writing task and setup the writing
313 MWriteRootFile write5(2, rule, "RECREATE", "Calibrated Data");
314 write5.AddContainer("MSignalCam", "Events");
315 write5.AddContainer("MRawEvtHeader", "Events");
316 write5.AddContainer("MSoftwareTrigger","Events");
317 write5.AddContainer("MRawRunHeader", "RunHeaders");
318 write5.AddContainer("MGeomCam", "RunHeaders");
319
320 write5.AddContainer("MMcCorsikaRunHeader", "RunHeaders", kFALSE);
321 write5.AddContainer("MCorsikaRunHeader", "RunHeaders", kFALSE);
322 write5.AddContainer("MMcRunHeader", "RunHeaders", kFALSE);
323 write5.AddContainer("MCorsikaEvtHeader", "Events", kFALSE);
324 write5.AddContainer("MMcEvt", "Events", kFALSE);
325 write5.AddContainer("IncidentAngle", "Events", kFALSE);
326 write5.AddContainer("MPointingPos", "Events", kFALSE);
327 write5.AddContainer("MSimSourcePos", "Events", kFALSE);
328 write5.AddContainer("MTime", "Events", kFALSE);
329 write5.AddContainer("MRawBoardsFACT", "Events", kFALSE);
330 write5.AddContainer("MSrcPosCam", "Events", kFALSE);
331
332 // ------------------ Setup histograms and fill tasks ----------------
333
334 // Instantitate the task list and setp the list
335 MTaskList tlist5dat;
336 tlist5dat.AddToList(&fill5b);
337 tlist5dat.AddToList(&fill5c);
338 tlist5dat.AddToList(&fill5d);
339 tlist5dat.AddToList(&fill5e);
340 tlist5dat.SetFilter(&filterdat);
341
342 tlist5.AddToList(&read5);
343 tlist5.AddToList(&apply5);
344 tlist5.AddToList(&drsapply5);
345 tlist5.AddToList(&fill5a);
346 tlist5.AddToList(&swtrig);
347 tlist5.AddToList(&filterdata1);
348 tlist5.AddToList(&extractor5dat);
349 tlist5.AddToList(&calctm5);
350 tlist5.AddToList(&conv5);
351 tlist5.AddToList(&treat5);
352 tlist5.AddToList(&filterdat);
353 tlist5.AddToList(&tlist5dat);
354 tlist5.AddToList(&write5);
355
356 // run the eventloop
357 if (!loop5.Eventloop(max))
358 return 18;
359
360 // Check if the display was closed (deleted) by the user
361 if (!loop5.GetDisplay())
362 return 19;
363
364 // ============================================================
365
366 TString fname = write5.GetFileName();
367 fname.ReplaceAll(".root", "-display.root");
368
369 // Write the status display to the file
370 TString title = "-- Calibrated signal [";
371 title += datafile;
372 title += "] --";
373 d->SetTitle(title, kFALSE);
374
375 d->SaveAsRoot(fname);
376
377 return 0;
378}
Note: See TracBrowser for help on using the repository browser.