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

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