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

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