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