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

Last change on this file since 19717 was 19704, checked in by tbretz, 6 years ago
Minor fixed and simplification.
File size: 12.2 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 *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 = 100;
69 const int last_slice = 500;
70
71 // Calibration constant (for the moment a single constant to
72 // convert the extracted charge to photo-electrons)
73 double scale = 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 // Instantiate the reading task
182 // You can use
183 // read5.AddFiles("*.fits.fz")
184 // for example to read more than one file at once
185 MRawFitsRead read5(datafile);
186 if (mmap)
187 read5.LoadMap(mmap);
188
189 // Instantiate the histogram to be filled with the rates
190 MH3 hrate("MRawRunHeader.GetFileID", "int(MRawEvtHeader.GetTriggerID)&0xff00");
191 hrate.SetWeight("1./TMath::Max(MRawRunHeader.GetRunLength,1.)");
192 hrate.SetName("Rate");
193 hrate.SetTitle("Event rate [Hz];File Id;Trigger Type;");
194 hrate.InitLabels(MH3::kLabelsXY);
195 hrate.DefineLabelY( 0, "Data"); // What if TriggerID==0 already???
196 hrate.DefineLabelY(0x100, "Cal");
197 hrate.DefineLabelY(0x400, "Ped");
198 // hrate.DefaultLabelY("ERROR");
199
200 // Instantiate the task to fill therate histogram
201 MFillH fill5a(&hrate);
202
203 // Instantiate the task to ensure proper sizing of all containers
204 MGeomApply apply5;
205
206 // Instantiate the task to apply the drs calibration constants
207 MDrsCalibApply drsapply5;
208 drsapply5.SetMaxNumPrevEvents(0); // Switched off step-correction -> crashes due to the requirement of N*9 ch
209
210 // Instantiate the filter to select only physics events
211 MFDataPhrase filterdat("(int(MRawEvtHeader.GetTriggerID)&0xff00)==0", "SelectDat");
212
213 // ---
214/*
215 MExtractTimeAndChargeSpline extractor5dat;
216 extractor5dat.SetRange(first_slice, last_slice);
217 extractor5dat.SetRiseTimeHiGain(rise_time_dat);
218 extractor5dat.SetFallTimeHiGain(fall_time_dat);
219 extractor5dat.SetHeightTm(heighttm);
220 extractor5dat.SetChargeType(type);
221 extractor5dat.SetSaturationLimit(600000);
222 extractor5dat.SetNoiseCalculation(kFALSE);
223*/
224
225 // Instantiate the digital filter (weighted sliding average) to clean the data
226 // Note that the weights are not adapted yet to HAWC's Eye
227 // A better filter might be worth to think about
228 MFilterData filterdata1;
229
230 // Instatiate task to extract the maximum of the pulse (correspondins to
231 // a weighted integral) and the position of the leading edge between
232 // first_slice and last_slice as charge and arrival time
233 MExtractFACT extractor5dat("ExtractPulse");
234 extractor5dat.SetRange(first_slice, last_slice);
235 extractor5dat.SetNoiseCalculation(kFALSE);
236
237 // Instantiate task which applies the calibration constants
238 MCalibrateFact conv5;
239 conv5.SetScale(scale);
240 //conv5.SetCalibConst(calib);
241
242 // Instantiate task which applies the DRS time calibration
243 MCalibrateDrsTimes calctm5;
244 calctm5.SetNameUncalibrated("UncalibratedTimes");
245
246 // Instantiate task to interpolate bad pixels
247 MBadPixelsTreat treat5;
248 treat5.SetProcessPedestalRun(kFALSE);
249 treat5.SetProcessPedestalEvt(kFALSE);
250
251 // Instantiate histograms to display charges
252 MHCamEvent evt5b(0, "ExtSig", "Extracted signal;;S [mV·sl]");
253 MHCamEvent evt5c(0, "CalSig", "Calibrated and interpolated signal;;S [~phe]");
254 MHCamEvent evt5d(4, "ExtSigTm", "Extracted time;;T [sl]");
255 MHCamEvent evt5e(6, "CalSigTm", "Calibrated and interpolated time;;T [ns]");
256
257 // Instantiate task to fill those histograms
258 MFillH fill5b(&evt5b, "MExtractedSignalCam", "FillExtSig");
259 MFillH fill5c(&evt5c, "MSignalCam", "FillCalSig");
260 MFillH fill5d(&evt5d, "MArrivalTimeCam", "FillExtTm");
261 MFillH fill5e(&evt5e, "MSignalCam", "FillCalTm");
262
263 // Setup those histograms to fit a gaussian to the distribution
264 fill5c.SetDrawOption("gaus");
265 fill5d.SetDrawOption("gaus");
266 fill5e.SetDrawOption("gaus");
267
268 // Instantiate the task to apply the software trigger
269 MSoftwareTriggerCalc swtrig;
270
271 // This is an alternative if more than one file should be read in a single loop
272 // In this case the output file name of each file is created with this rule
273 // from the input file name. Note that the filename of the output file
274 // with the status display must then be explicitly given.
275 //
276 //const TString fname(Form("s/([0-9]+_[0-9]+)[.]fits([.][fg]z)?$/%s\\/$1_C.root/",
277 // MJob::Esc(outpath).Data()));
278 //MWriteRootFile write5(2, fname, "RECREATE", "Calibrated Data");
279
280 // Convert the name of the input file to the name of the output file
281 TString fname = gSystem->ConcatFileName(outpath, gSystem->BaseName(datafile));
282 fname.ReplaceAll(".fits.gz", ".root");
283 fname.ReplaceAll(".fits.fz", ".root");
284 fname.ReplaceAll(".fits", ".root");
285 fname.ReplaceAll("_D_", "_C_");
286
287 gSystem->ExpandPathName(fname);
288
289 // Instantitate the writing task and setup the writing
290 MWriteRootFile write5(fname, "RECREATE", "Calibrated Data", 2);
291 write5.AddContainer("MSignalCam", "Events");
292 write5.AddContainer("MRawEvtHeader", "Events");
293 write5.AddContainer("MSoftwareTrigger","Events");
294 write5.AddContainer("MRawRunHeader", "RunHeaders");
295 write5.AddContainer("MGeomCam", "RunHeaders");
296
297 write5.AddContainer("MMcCorsikaRunHeader", "RunHeaders", kFALSE);
298 write5.AddContainer("MCorsikaRunHeader", "RunHeaders", kFALSE);
299 write5.AddContainer("MMcRunHeader", "RunHeaders", kFALSE);
300 write5.AddContainer("MCorsikaEvtHeader", "Events", kFALSE);
301 write5.AddContainer("MMcEvt", "Events", kFALSE);
302 write5.AddContainer("IncidentAngle", "Events", kFALSE);
303 write5.AddContainer("MPointingPos", "Events", kFALSE);
304 write5.AddContainer("MSimSourcePos", "Events", kFALSE);
305 write5.AddContainer("MTime", "Events", kFALSE);
306 write5.AddContainer("MSrcPosCam", "Events", kFALSE);
307
308 // ------------------ Setup histograms and fill tasks ----------------
309
310 // Instantitate the task list and setp the list
311 MTaskList tlist5dat;
312 tlist5dat.AddToList(&fill5b);
313 tlist5dat.AddToList(&fill5c);
314 tlist5dat.AddToList(&fill5d);
315 tlist5dat.AddToList(&fill5e);
316 tlist5dat.SetFilter(&filterdat);
317
318 tlist5.AddToList(&read5);
319 tlist5.AddToList(&apply5);
320 tlist5.AddToList(&drsapply5);
321 tlist5.AddToList(&fill5a);
322 tlist5.AddToList(&swtrig);
323 tlist5.AddToList(&filterdata1);
324 tlist5.AddToList(&extractor5dat);
325 tlist5.AddToList(&calctm5);
326 tlist5.AddToList(&conv5);
327 tlist5.AddToList(&treat5);
328 tlist5.AddToList(&filterdat);
329 tlist5.AddToList(&tlist5dat);
330 tlist5.AddToList(&write5);
331
332 // run the eventloop
333 if (!loop5.Eventloop(max))
334 return 18;
335
336 // Check if the display was closed (deleted) by the user
337 if (!loop5.GetDisplay())
338 return 19;
339
340 // ============================================================
341
342 // Check if the output file is still accessible from root
343 TFile *ofile = (TFile*)gROOT->GetListOfFiles()->FindObject(fname);
344 cout << ofile << endl;
345 if (!ofile || !ofile->IsOpen() || ofile->IsZombie())
346 {
347 gLog << err << "File " << fname << " not found" << endl;
348 return 20;
349 }
350
351 // Write the status display to the file
352 TString title = "-- Calibrated signal [";
353 title += datafile;
354 title += "] --";
355 d->SetTitle(title, kFALSE);
356
357 ofile->cd();
358 d->Write();
359
360 return 0;
361}
Note: See TracBrowser for help on using the repository browser.