1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz 4/2007 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2007
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MMovieWrite
|
---|
28 | //
|
---|
29 | // The intention of this class is to encode movies prepard by the
|
---|
30 | // MMoviePrepare task.
|
---|
31 | //
|
---|
32 | // For writing the movies the images are converted to ppm and piped through
|
---|
33 | // ppm2y4m to mpeg2enc. The output format is a mpeg2 movie and should
|
---|
34 | // be within the specifications of mpeg2 for DVD. Its size is 720x480,
|
---|
35 | // which is a good compromise between resolution and file size. The frame
|
---|
36 | // rate is fixed to 24fps.
|
---|
37 | //
|
---|
38 | // By changing the setup you can control the output:
|
---|
39 | //
|
---|
40 | // NumEvents: To make sure the file size doesn't get too large
|
---|
41 | // (300 evts are roughly 80MB with the default seetings) you can set
|
---|
42 | // a maximum number of events. If this number of events has been encoded
|
---|
43 | // the eventloop is stopped.
|
---|
44 | //
|
---|
45 | // TargetLength: The length (in seconds) each even will be encoded to.
|
---|
46 | // For example with the default Target Length of 5s and the fixed frame
|
---|
47 | // rate of 24fps each event will be encoded into 24f/s*5s +1f = 121frames
|
---|
48 | // equally distributed between the beginning of the first and the end of
|
---|
49 | // the last frame.
|
---|
50 | //
|
---|
51 | // Threshold: The default threshold is 2. At 2 times median pedestal rms
|
---|
52 | // of the pixles with area index 0 (for MAGIC: inner pixels) the color
|
---|
53 | // palette will change from yellow to red and isolated pixels between
|
---|
54 | // the median rms and 2 times the median of the rms will be removed from
|
---|
55 | // the image. To switch off this behaviour you can set a threshold
|
---|
56 | // below one.
|
---|
57 | //
|
---|
58 | // Filename: The output filename of the movie. If it doesn't end with ".mpg"
|
---|
59 | // the suffix is added.
|
---|
60 | //
|
---|
61 | // The interpolation of the frames is done using a TSpline3. If the spline
|
---|
62 | // would extrapolate the contents is set to zero. Unsuitable pixels are
|
---|
63 | // interpolated frame by frame using the surrounding suitable pixels.
|
---|
64 | //
|
---|
65 | //
|
---|
66 | // Input:
|
---|
67 | // - MGeomCam
|
---|
68 | // - MRawRunHeader
|
---|
69 | // - MRawEvtHeader
|
---|
70 | // - MSignalCam
|
---|
71 | // - MBadPixelsCam
|
---|
72 | // - MPedestalFundamental [MPedestalCam]
|
---|
73 | // - MMovieData
|
---|
74 | //
|
---|
75 | /////////////////////////////////////////////////////////////////////////////
|
---|
76 | #include "MMovieWrite.h"
|
---|
77 |
|
---|
78 | #include <errno.h>
|
---|
79 |
|
---|
80 | #include <TF1.h>
|
---|
81 | #include <TStyle.h>
|
---|
82 | #include <TColor.h>
|
---|
83 | #include <TCanvas.h>
|
---|
84 | #include <TSystem.h>
|
---|
85 | #include <TASImage.h>
|
---|
86 | #include <TStopwatch.h>
|
---|
87 |
|
---|
88 | #include "MParList.h"
|
---|
89 | #include "MTaskList.h"
|
---|
90 |
|
---|
91 | #include "MGeomCam.h"
|
---|
92 | #include "MGeomPix.h"
|
---|
93 |
|
---|
94 | #include "MPedestalCam.h"
|
---|
95 | #include "MPedestalPix.h"
|
---|
96 |
|
---|
97 | #include "MH.h"
|
---|
98 | #include "MHCamera.h"
|
---|
99 | #include "MMovieData.h"
|
---|
100 | #include "MSignalCam.h"
|
---|
101 | #include "MRawEvtHeader.h"
|
---|
102 | #include "MRawRunHeader.h"
|
---|
103 | #include "MBadPixelsCam.h"
|
---|
104 | #include "MBadPixelsPix.h"
|
---|
105 | #include "MCalibrateData.h"
|
---|
106 |
|
---|
107 | #include "MLog.h"
|
---|
108 | #include "MLogManip.h"
|
---|
109 |
|
---|
110 | ClassImp(MMovieWrite);
|
---|
111 |
|
---|
112 | using namespace std;
|
---|
113 |
|
---|
114 | // --------------------------------------------------------------------------
|
---|
115 | //
|
---|
116 | // Default constructor.
|
---|
117 | //
|
---|
118 | MMovieWrite::MMovieWrite(const char *name, const char *title)
|
---|
119 | : fPipe(0), fTargetLength(5), fThreshold(2), fNumEvents(1000), fFilename("movie.mpg")
|
---|
120 | {
|
---|
121 | fName = name ? name : "MMovieWrite";
|
---|
122 | fTitle = title ? title : "Task to encode a movie";
|
---|
123 | }
|
---|
124 |
|
---|
125 | // --------------------------------------------------------------------------
|
---|
126 | //
|
---|
127 | // Close pipe if still open
|
---|
128 | //
|
---|
129 | MMovieWrite::~MMovieWrite()
|
---|
130 | {
|
---|
131 | if (fPipe)
|
---|
132 | gSystem->ClosePipe(fPipe);
|
---|
133 | }
|
---|
134 |
|
---|
135 | // --------------------------------------------------------------------------
|
---|
136 | //
|
---|
137 | // Check the pipe for errors. In case of error print an error message.
|
---|
138 | // return kFALSE in case of error, kTRUE in case of success.
|
---|
139 | //
|
---|
140 | Bool_t MMovieWrite::CheckPipe()
|
---|
141 | {
|
---|
142 | if (!ferror(fPipe))
|
---|
143 | return kTRUE;
|
---|
144 |
|
---|
145 | *fLog << err << "Error in pipe: " << strerror(errno) << endl;
|
---|
146 | return kFALSE;
|
---|
147 | }
|
---|
148 |
|
---|
149 | // --------------------------------------------------------------------------
|
---|
150 | //
|
---|
151 | // Open pipe for encoding the movie
|
---|
152 | //
|
---|
153 | Bool_t MMovieWrite::OpenPipe()
|
---|
154 | {
|
---|
155 | // name = "ppmtoy4m -B -S 420mpeg2 -v 0 | yuvplay";
|
---|
156 | // name = Form("ppmtoy4m -B -S 420jpeg -v 0 -F %d:%d | yuv2lav -v 0 -o output%03d.avi", TMath::Nint() TMath::Nint(fTargetLength*1000))
|
---|
157 | // name = "ppmtoy4m -B -F 3:1 -S 420jpeg -v 0 | yuv2lav -v 0 -o output.avi";
|
---|
158 |
|
---|
159 | TString name;
|
---|
160 | name = "ppmtoy4m -B -F 24:1 -S 420jpeg -v 0 | ";
|
---|
161 | name += "mpeg2enc -v 0 -F 2 -I 0 -M 2 -o ";
|
---|
162 | name += fFilename;
|
---|
163 | if (!fFilename.EndsWith(".mpg"))
|
---|
164 | name += ".mpg";
|
---|
165 |
|
---|
166 | const Int_t n = TMath::Nint(fTargetLength*24)+1;
|
---|
167 |
|
---|
168 | name += " -f 9 -E 40 -r 0 -K kvcd ";
|
---|
169 | name += Form("-g %d -G %d", n, n);
|
---|
170 |
|
---|
171 | // For higher resolution add "--no-constraints"
|
---|
172 |
|
---|
173 | fPipe = gSystem->OpenPipe(name, "w");
|
---|
174 | if (fPipe)
|
---|
175 | return kTRUE;
|
---|
176 |
|
---|
177 | *fLog << err;
|
---|
178 | *fLog << "Pipe: " << name << endl;
|
---|
179 | *fLog << "Couldn't open pipe... aborting." << endl;
|
---|
180 | CheckPipe();
|
---|
181 | return kFALSE;
|
---|
182 |
|
---|
183 | // 1: 37M name += "-f 9 -E 40 -H -4 1 -2 1 --dualprime-mpeg2";
|
---|
184 | // 2: 42M name += "-f 9";
|
---|
185 | // 3: 37M name += "-f 9 -E 40 -4 1 -2 1 --dualprime-mpeg2";
|
---|
186 | // 4: 37M name += "-f 9 -E 40 -4 1 -2 1";
|
---|
187 | // 5: 37M name += "-f 9 -E 40 -4 4 -2 4"; // 640x400 3 frames/slice
|
---|
188 | // 6: 11M name += "-f 3 -E 40 -b 750"; // 640x400 3 frames/slice
|
---|
189 |
|
---|
190 | // 7: 28M name += "-f 9 -E 40 -G 50"; // 640x400 3 frames/slice
|
---|
191 | // 8: 24M name += "-f 9 -E 40 -G 500"; // 640x400 3 frames/slice
|
---|
192 |
|
---|
193 | // 9: 17M name += "-f 9 -E 40 -G 2400"; // 640x400 24 frames/slice
|
---|
194 | // 10: 19M name += "-f 9 -E 40 -G 1120"; // 720x480 3 frames/slice
|
---|
195 | // 20: 33M name += "-f 9 -E 40 -g 28 -G 28"; // 720x480 3 frames/slice
|
---|
196 | // 57M name += "-f 9 -E 40 -g 28 -G 28 -q 4"; // 720x480 3 frames/slice
|
---|
197 |
|
---|
198 | // 30M name += "-f 9 -E 40 -g 84 -G 84 -r 0"; // 720x480 3 frames/slice
|
---|
199 | // 31M name += "-f 9 -E 40 -g 56 -G 56 -r 0"; // 720x480 3 frames/slice
|
---|
200 | // 34M name += "-f 9 -E 40 -g 28 -G 28 -r 0"; // 720x480 3 frames/slice
|
---|
201 | // 24: 24M name += "-f 9 -E 40 -g 28 -G 28 -r 0 -K kvcd"; // 720x480 3 frames/slice
|
---|
202 | // 25: 24M name += "-f 9 -E -40 -g 28 -G 28 -r 0 -K kvcd"; // 720x480 3 frames/slice
|
---|
203 | // 26: 26M name += "-f 9 -E 0 -g 28 -G 28 -r 0 -K kvcd"; // 720x480 3 frames/slice
|
---|
204 | // 34M name += "-f 9 -E 40 -g 28 -G 28 -r 2"; // 720x480 3 frames/slice
|
---|
205 | // 33M name += "-f 9 -E 40 -g 28 -G 28 -r 32"; // 720x480 3 frames/slice
|
---|
206 |
|
---|
207 | // name += "-f 9 -E 40 -g 121 -G 121 -r 0 -K kvcd"; // 720x480 5s 24 frames/slice
|
---|
208 |
|
---|
209 | // 11: 56M name += "-f 9 -E 40 -g 217 -G 217"; // 720x480 24 frames/slice
|
---|
210 | // 18: 59M name += "-f 9 -E 40 -G 250"; // 720x480 24 frames/slice
|
---|
211 | // 62M name += "-f 9 -E 40 -G 184"; // 720x480 24 frames/slice
|
---|
212 |
|
---|
213 | // 12: --- name += "-f 9 -E 40 -G 500 -q 31"; // 720x480 3frames/slice
|
---|
214 | // 13: 49M name += "-f 9 -E 40 -G 500 -q 4"; // 720x480 3frames/slice
|
---|
215 | // 14: 21M name += "-f 9 -E 40 -G 500 -q 4 -b 1500"; // 720x480 3frames/slice
|
---|
216 |
|
---|
217 | // 15: 57M name += "-f 9 -E 40 -G 500 --no-constraints"; // 1280 864 3frames/slice
|
---|
218 |
|
---|
219 | // 16: >80 name += "-f 9 -E 40 -G 217 --no-constraints -b 3000"; // 1280 864 24frames/slice
|
---|
220 | // 17: >50 name += "-f 9 -E 40 -G 682 -b 3000 --no-constraints"; // 1280 864 24frames/slice
|
---|
221 | }
|
---|
222 |
|
---|
223 | // --------------------------------------------------------------------------
|
---|
224 | //
|
---|
225 | // Search for:
|
---|
226 | // - MGeomCam
|
---|
227 | // - MRawRunHeader
|
---|
228 | // - MRawEvtHeader
|
---|
229 | // - MSignalCam
|
---|
230 | // - MBadPixelsCam
|
---|
231 | // - MPedestalFundamental [MPedestalCam]
|
---|
232 | // - MMovieData
|
---|
233 | //
|
---|
234 | // Open a pipe to write the images to. Can be either a player or
|
---|
235 | // an encoder.
|
---|
236 | //
|
---|
237 | Int_t MMovieWrite::PreProcess(MParList *plist)
|
---|
238 | {
|
---|
239 | fCam = (MGeomCam*)plist->FindObject("MGeomCam");
|
---|
240 | if (!fCam)
|
---|
241 | {
|
---|
242 | *fLog << err << "MGeomCam not found ... aborting." << endl;
|
---|
243 | return kFALSE;
|
---|
244 | }
|
---|
245 | fRun = (MRawRunHeader*)plist->FindObject("MRawRunHeader");
|
---|
246 | if (!fRun)
|
---|
247 | {
|
---|
248 | *fLog << err << "MRawRunHeader not found ... aborting." << endl;
|
---|
249 | return kFALSE;
|
---|
250 | }
|
---|
251 | fHead = (MRawEvtHeader*)plist->FindObject("MRawEvtHeader");
|
---|
252 | if (!fHead)
|
---|
253 | {
|
---|
254 | *fLog << err << "MRawEvtHeader not found ... aborting." << endl;
|
---|
255 | return kFALSE;
|
---|
256 | }
|
---|
257 | fSig = (MSignalCam*)plist->FindObject("MSignalCam");
|
---|
258 | if (!fSig)
|
---|
259 | {
|
---|
260 | *fLog << err << "MSignalCam not found ... aborting." << endl;
|
---|
261 | return kFALSE;
|
---|
262 | }
|
---|
263 | fBad = (MBadPixelsCam*)plist->FindObject("MBadPixelsCam");
|
---|
264 | if (!fBad)
|
---|
265 | {
|
---|
266 | *fLog << err << "MBadPixelsCam not found ... aborting." << endl;
|
---|
267 | return kFALSE;
|
---|
268 | }
|
---|
269 | fPed = (MPedestalCam*)plist->FindObject("MPedestalFundamental", "MPedestalCam");
|
---|
270 | if (!fPed)
|
---|
271 | {
|
---|
272 | *fLog << err << "MPedestalFundamental [MPedestalCam] not found ... aborting." << endl;
|
---|
273 | return kFALSE;
|
---|
274 | }
|
---|
275 | fIn = (MMovieData*)plist->FindObject("MMovieData");
|
---|
276 | if (!fIn)
|
---|
277 | {
|
---|
278 | *fLog << err << "MMovieData not found... aborting." << endl;
|
---|
279 | return kFALSE;
|
---|
280 | }
|
---|
281 |
|
---|
282 | return OpenPipe();
|
---|
283 | }
|
---|
284 |
|
---|
285 | TStopwatch clockT, clock1, clock2, clock3;
|
---|
286 |
|
---|
287 | // --------------------------------------------------------------------------
|
---|
288 | //
|
---|
289 | // Close pipe if still open
|
---|
290 | //
|
---|
291 | Int_t MMovieWrite::PostProcess()
|
---|
292 | {
|
---|
293 | if (fPipe)
|
---|
294 | {
|
---|
295 | gSystem->ClosePipe(fPipe);
|
---|
296 | fPipe=0;
|
---|
297 | }
|
---|
298 |
|
---|
299 | *fLog << all << endl;
|
---|
300 | *fLog << "Snap: " << flush;
|
---|
301 | clock1.Print();
|
---|
302 | *fLog << "Writ: " << flush;
|
---|
303 | clock2.Print();
|
---|
304 | *fLog << "Prep: " << flush;
|
---|
305 | clock3.Print();
|
---|
306 | *fLog << "Totl: " << flush;
|
---|
307 | clockT.Print();
|
---|
308 | *fLog << endl;
|
---|
309 |
|
---|
310 | return kTRUE;
|
---|
311 | }
|
---|
312 |
|
---|
313 | // --------------------------------------------------------------------------
|
---|
314 | //
|
---|
315 | // Produce a 99 color palette made such, that everything below one
|
---|
316 | // pedestal rms is white, everything up to two pedestal rms is yellow
|
---|
317 | // and everything above gets colors.
|
---|
318 | //
|
---|
319 | void MMovieWrite::SetPalette(Double_t rms, const TH1 &h) const
|
---|
320 | {
|
---|
321 | const Double_t min = h.GetMinimum();
|
---|
322 | const Double_t max = h.GetMaximum();
|
---|
323 |
|
---|
324 | const Double_t f = (fThreshold*rms-min)/(max-min);
|
---|
325 | const Double_t w = 1-f;
|
---|
326 |
|
---|
327 | // --- Produce the nice colored palette ---
|
---|
328 |
|
---|
329 | // min th*rms max
|
---|
330 | double s[6] = {0.0, f/2, f, f+w/4, f+3*w/5, 1.0 };
|
---|
331 |
|
---|
332 | double r[6] = {1.0, 1.0, 1.0, 0.85, 0.1, 0.0 };
|
---|
333 | double g[6] = {1.0, 1.0, 1.0, 0.0, 0.1, 0.0 };
|
---|
334 | double b[6] = {0.9, 0.55, 0.4, 0.0, 0.7, 0.1 };
|
---|
335 |
|
---|
336 | TArrayI col(99);
|
---|
337 | gStyle->CreateGradientColorTable(6, s, r, g, b, col.GetSize());
|
---|
338 |
|
---|
339 | // --- Overwrite the 'underflow' bin with white ---
|
---|
340 |
|
---|
341 | for (int i=0; i<col.GetSize(); i++)
|
---|
342 | col[i] = gStyle->GetColorPalette(i);
|
---|
343 |
|
---|
344 | col[0] = TColor::GetColor(0xff, 0xff, 0xff);
|
---|
345 |
|
---|
346 | // --- Set Plette ---
|
---|
347 |
|
---|
348 | gStyle->SetPalette(col.GetSize(), col.GetArray());
|
---|
349 | }
|
---|
350 |
|
---|
351 | /*
|
---|
352 | // --------------------------------------------------------------------------
|
---|
353 | //
|
---|
354 | // Do a snapshot from the pad via TASImage::FromPad and write the
|
---|
355 | // image to the pipe.
|
---|
356 | // return kFALSE in case of error, kTRUE in case of success.
|
---|
357 | //
|
---|
358 | Bool_t MMovieWrite::WriteImage(TVirtualPad &pad)
|
---|
359 | {
|
---|
360 | clock1.Start(kFALSE);
|
---|
361 | TASImage img;
|
---|
362 | img.FromPad(&pad);
|
---|
363 | clock1.Stop();
|
---|
364 |
|
---|
365 | clock2.Start(kFALSE);
|
---|
366 | const Bool_t rc = WriteImage(img);
|
---|
367 | clock2.Stop();
|
---|
368 |
|
---|
369 | return rc;
|
---|
370 | }
|
---|
371 |
|
---|
372 | #include <TVirtualPS.h>
|
---|
373 | Bool_t MMovieWrite::WriteImage(TVirtualPad &pad)
|
---|
374 | {
|
---|
375 | TVirtualPS *psave = gVirtualPS;
|
---|
376 |
|
---|
377 | clock1.Start(kFALSE);
|
---|
378 | TImage dump("", 114);
|
---|
379 | dump.SetBit(BIT(11));
|
---|
380 | pad.Paint();
|
---|
381 | TASImage *itmp = (TASImage*)dump.GetStream();
|
---|
382 | clock1.Stop();
|
---|
383 |
|
---|
384 | clock2.Start(kFALSE);
|
---|
385 | const Bool_t rc = WriteImage(*itmp);
|
---|
386 | clock2.Stop();
|
---|
387 |
|
---|
388 | gVirtualPS = psave;
|
---|
389 |
|
---|
390 | return rc;
|
---|
391 | }
|
---|
392 | */
|
---|
393 |
|
---|
394 | // --------------------------------------------------------------------------
|
---|
395 | //
|
---|
396 | // Update the part of the idst image with the contents of the pad.
|
---|
397 | //
|
---|
398 | // It is a lot faster not to rerender the parts of the image which don't
|
---|
399 | // change anyhow, because rerendering the camera is by far the slowest.
|
---|
400 | //
|
---|
401 | void MMovieWrite::UpdateImage(TASImage &idst, TVirtualPad &pad)
|
---|
402 | {
|
---|
403 | // Get image from pad
|
---|
404 | TASImage isrc;
|
---|
405 | isrc.FromPad(&pad);
|
---|
406 |
|
---|
407 | // Get position and width of destination- and source-image
|
---|
408 | const UInt_t wsrc = isrc.GetWidth(); // width of image
|
---|
409 | const UInt_t hsrc = isrc.GetHeight(); // height of image
|
---|
410 |
|
---|
411 | const UInt_t usrc = pad.UtoPixel(1)*4; // width of pad (argb)
|
---|
412 | //const UInt_t vsrc = pad.VtoPixel(0); // height of pad
|
---|
413 |
|
---|
414 | const UInt_t xsrc = pad.UtoAbsPixel(0); // offset of pad in canvas
|
---|
415 | const UInt_t ysrc = pad.VtoAbsPixel(1); // offset of pad in canvas
|
---|
416 |
|
---|
417 | const UInt_t wdst = idst.GetWidth();
|
---|
418 | //const UInt_t hdst = idst.GetHeight();
|
---|
419 |
|
---|
420 | // Update destination image with source image
|
---|
421 | const UInt_t size = wsrc*hsrc;
|
---|
422 |
|
---|
423 | UInt_t *psrc = isrc.GetArgbArray();
|
---|
424 | UInt_t *pdst = idst.GetArgbArray();
|
---|
425 |
|
---|
426 | UInt_t *src = psrc + ysrc*wsrc+xsrc;
|
---|
427 | UInt_t *dst = pdst + ysrc*wdst+xsrc;
|
---|
428 |
|
---|
429 | while (src<psrc+size)
|
---|
430 | {
|
---|
431 | memcpy(dst, src, usrc);
|
---|
432 |
|
---|
433 | src += wsrc;
|
---|
434 | dst += wdst;
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | // --------------------------------------------------------------------------
|
---|
439 | //
|
---|
440 | // Write the image as ppm (raw/P6) to the pipe.
|
---|
441 | // return kFALSE in case of error, kTRUE in case of success.
|
---|
442 | //
|
---|
443 | Bool_t MMovieWrite::WriteImage(TASImage &img)
|
---|
444 | {
|
---|
445 | // Write image header
|
---|
446 | fprintf(fPipe, "P6 %d %d 255\n", img.GetWidth(), img.GetHeight());
|
---|
447 | if (!CheckPipe())
|
---|
448 | return kFALSE;
|
---|
449 |
|
---|
450 | // Write image data (remove alpha channel from argb data)
|
---|
451 | UInt_t *argb = img.GetArgbArray();
|
---|
452 | for (UInt_t *ptr=argb; ptr<argb+img.GetWidth()*img.GetHeight(); ptr++)
|
---|
453 | fwrite(ptr, 1, 3, fPipe);
|
---|
454 |
|
---|
455 | return CheckPipe();
|
---|
456 | }
|
---|
457 |
|
---|
458 | // --------------------------------------------------------------------------
|
---|
459 | //
|
---|
460 | // Update TASImage with changing parts of the image and write image to pipe.
|
---|
461 | // return kFALSE in case of error, kTRUE in case of success.
|
---|
462 | //
|
---|
463 | Bool_t MMovieWrite::WriteImage(TASImage &img, TVirtualPad &pad)
|
---|
464 | {
|
---|
465 | clock1.Start(kFALSE);
|
---|
466 | UpdateImage(img, pad);
|
---|
467 | clock1.Stop();
|
---|
468 |
|
---|
469 | clock2.Start(kFALSE);
|
---|
470 | const Bool_t rc = WriteImage(img);
|
---|
471 | clock2.Stop();
|
---|
472 |
|
---|
473 | return rc;
|
---|
474 | }
|
---|
475 |
|
---|
476 | // --------------------------------------------------------------------------
|
---|
477 | //
|
---|
478 | // Do a simple interpolation of the surrounding suitable pixels for all
|
---|
479 | // unsuitable pixels.
|
---|
480 | //
|
---|
481 | void MMovieWrite::TreatBadPixels(TH1 &h) const
|
---|
482 | {
|
---|
483 | const UShort_t entries = fCam->GetNumPixels();
|
---|
484 |
|
---|
485 | //
|
---|
486 | // Loop over all pixels
|
---|
487 | //
|
---|
488 | for (UShort_t i=0; i<entries; i++)
|
---|
489 | {
|
---|
490 | //
|
---|
491 | // Check whether pixel with idx i is blind
|
---|
492 | //
|
---|
493 | if (!(*fBad)[i].IsUnsuitable())
|
---|
494 | continue;
|
---|
495 |
|
---|
496 | const MGeomPix &gpix = (*fCam)[i];
|
---|
497 |
|
---|
498 | Int_t num = 0;
|
---|
499 | Double_t sum = 0;
|
---|
500 |
|
---|
501 | //
|
---|
502 | // Loop over all its neighbors
|
---|
503 | //
|
---|
504 | Int_t n = gpix.GetNumNeighbors();
|
---|
505 | while (n--)
|
---|
506 | {
|
---|
507 | const UShort_t nidx = gpix.GetNeighbor(n);
|
---|
508 |
|
---|
509 | //
|
---|
510 | // Do not use blind neighbors
|
---|
511 | //
|
---|
512 | if ((*fBad)[nidx].IsUnsuitable())
|
---|
513 | continue;
|
---|
514 |
|
---|
515 | sum += h.GetBinContent(nidx+1);
|
---|
516 | num++;
|
---|
517 | }
|
---|
518 |
|
---|
519 | h.SetBinContent(i+1, sum/num);
|
---|
520 | }
|
---|
521 | }
|
---|
522 |
|
---|
523 | // --------------------------------------------------------------------------
|
---|
524 | //
|
---|
525 | // Do a simple interpolation of the surrounding suitable pixels for all
|
---|
526 | // unsuitable pixels.
|
---|
527 | //
|
---|
528 | void MMovieWrite::Clean(TH1 &h, Double_t rms) const
|
---|
529 | {
|
---|
530 | if (fThreshold<1)
|
---|
531 | return;
|
---|
532 |
|
---|
533 | const UShort_t entries = fCam->GetNumPixels();
|
---|
534 |
|
---|
535 | //
|
---|
536 | // Loop over all pixels
|
---|
537 | //
|
---|
538 | for (UShort_t i=0; i<entries; i++)
|
---|
539 | {
|
---|
540 | Double_t val = h.GetBinContent(i+1);
|
---|
541 | if (val<rms || val>fThreshold*rms)
|
---|
542 | continue;
|
---|
543 |
|
---|
544 | const MGeomPix &gpix = (*fCam)[i];
|
---|
545 |
|
---|
546 | //
|
---|
547 | // Loop over all its neighbors
|
---|
548 | //
|
---|
549 | Int_t n = gpix.GetNumNeighbors();
|
---|
550 | while (n)
|
---|
551 | {
|
---|
552 | const UShort_t nidx = gpix.GetNeighbor(n-1);
|
---|
553 | if (h.GetBinContent(nidx+1)>=rms)
|
---|
554 | break;
|
---|
555 | n--;
|
---|
556 | }
|
---|
557 |
|
---|
558 | if (n==0)
|
---|
559 | h.SetBinContent(i+1, 0);
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|
563 | // --------------------------------------------------------------------------
|
---|
564 | //
|
---|
565 | // Return the median of the pedestal rms of all pixels with area index 0
|
---|
566 | //
|
---|
567 | Double_t MMovieWrite::GetMedianPedestalRms() const
|
---|
568 | {
|
---|
569 | Int_t n = fCam->GetNumPixWithAidx(0);
|
---|
570 |
|
---|
571 | MArrayF rms(n);
|
---|
572 |
|
---|
573 | for (UInt_t i=0; i<fCam->GetNumPixels(); i++)
|
---|
574 | if ((*fCam)[i].GetAidx()==0)
|
---|
575 | rms[--n] = (*fPed)[i].GetPedestalRms();
|
---|
576 |
|
---|
577 | return TMath::Median(rms.GetSize(), rms.GetArray());
|
---|
578 | }
|
---|
579 |
|
---|
580 | // --------------------------------------------------------------------------
|
---|
581 | //
|
---|
582 | Bool_t MMovieWrite::Process(TH1 &h, TVirtualPad &c)
|
---|
583 | {
|
---|
584 | // ---------------- Setup ------------------
|
---|
585 |
|
---|
586 | const Float_t freq = fRun->GetFreqSampling()/1000.; // [GHz] Sampling frequency
|
---|
587 | const UInt_t slices = fIn->GetNumSlices();
|
---|
588 | const Float_t len = slices/freq; // [ns] length of data stream in data-time
|
---|
589 | //const Float_t len = (slices-2)/freq; // [ns] length of data stream in data-time
|
---|
590 |
|
---|
591 | const Double_t rms = GetMedianPedestalRms();
|
---|
592 |
|
---|
593 | h.SetMinimum(fIn->GetMax()-(fIn->GetMax()-rms)*99/98); // rms0
|
---|
594 | h.SetMaximum(fIn->GetMax());
|
---|
595 |
|
---|
596 | // -----------------------------------------
|
---|
597 |
|
---|
598 | // Produce starting image from canvas
|
---|
599 | TASImage img;
|
---|
600 | img.FromPad(&c);
|
---|
601 |
|
---|
602 | // Set new adapted palette for further rendering
|
---|
603 | SetPalette(rms, h);
|
---|
604 |
|
---|
605 | // Get the pad containing the camera with the movie
|
---|
606 | TVirtualPad &pad = *c.GetPad(1)->GetPad(1);
|
---|
607 |
|
---|
608 | // Calculate number of frames
|
---|
609 | const Int_t numframes = TMath::Nint(fTargetLength*24);
|
---|
610 |
|
---|
611 | // Get number of pixels in camera
|
---|
612 | const Int_t npix = fCam->GetNumPixels();
|
---|
613 |
|
---|
614 | // Loop over all frames+1 (upper edge)
|
---|
615 | for (Int_t i=0; i<=numframes; i++)
|
---|
616 | {
|
---|
617 | // Calculate corresponding time
|
---|
618 | const Float_t t = len*i/numframes;// + 0.5/freq; // Process from slice beg+0.5 to end-1.5
|
---|
619 |
|
---|
620 | // Calculate histogram contents by spline interpolation
|
---|
621 | for (UShort_t p=0; p<npix; p++)
|
---|
622 | {
|
---|
623 | const Double_t y = (*fBad)[p].IsUnsuitable() ? 0 : fIn->CheckedEval(p, t);
|
---|
624 | h.SetBinContent(p+1, y);
|
---|
625 | }
|
---|
626 |
|
---|
627 | // Interpolate unsuitable pixels
|
---|
628 | TreatBadPixels(h);
|
---|
629 |
|
---|
630 | // Clean single pixels
|
---|
631 | Clean(h, rms);
|
---|
632 |
|
---|
633 | // Set new name to be displayed
|
---|
634 | h.SetName(Form("%d: %.2f/%.1fns", i+1, t*freq, t));
|
---|
635 |
|
---|
636 | // Update existing image with new data and encode into pipe
|
---|
637 | if (!WriteImage(img, pad))
|
---|
638 | return kFALSE;
|
---|
639 | }
|
---|
640 |
|
---|
641 | cout << setw(3) << GetNumExecutions() << ": " << Form("%6.2f", (float)numframes/(slices-2)) << " f/sl " << slices << " " << numframes+1 << endl;
|
---|
642 |
|
---|
643 | return kTRUE;
|
---|
644 | }
|
---|
645 |
|
---|
646 | // --------------------------------------------------------------------------
|
---|
647 | //
|
---|
648 | Int_t MMovieWrite::Process()
|
---|
649 | {
|
---|
650 | clockT.Start(kFALSE);
|
---|
651 |
|
---|
652 | clock3.Start(kFALSE);
|
---|
653 |
|
---|
654 | // ---------------- Prepare display ------------------
|
---|
655 |
|
---|
656 | Bool_t batch = gROOT->IsBatch();
|
---|
657 | gROOT->SetBatch();
|
---|
658 |
|
---|
659 | TCanvas c;
|
---|
660 | //c.Iconify();
|
---|
661 | c.SetBorderMode(0);
|
---|
662 | c.SetFrameBorderMode(0);
|
---|
663 | c.SetFillColor(kWhite);
|
---|
664 | //c.SetCanvasSize(640, 400);
|
---|
665 | c.SetCanvasSize(720, 480);
|
---|
666 | //c.SetCanvasSize(960, 640);
|
---|
667 | //c.SetCanvasSize(1024, 688);
|
---|
668 | //c.SetCanvasSize(1152, 768);
|
---|
669 | //c.SetCanvasSize(1280, 864);
|
---|
670 |
|
---|
671 | MH::SetPalette("pretty");
|
---|
672 |
|
---|
673 | c.cd();
|
---|
674 | TPad p1("Pad2", "", 0.7, 0.66, 0.99, 0.99);
|
---|
675 | p1.SetNumber(2);
|
---|
676 | p1.SetBorderMode(0);
|
---|
677 | p1.SetFrameBorderMode(0);
|
---|
678 | p1.SetFillColor(kWhite);
|
---|
679 | p1.Draw();
|
---|
680 | p1.cd();
|
---|
681 |
|
---|
682 | MHCamera hsig(*fCam, "Signal", "Calibrated Signal");
|
---|
683 | hsig.SetYTitle("S [phe]");
|
---|
684 | hsig.SetCamContent(*fSig, 99);
|
---|
685 | hsig.SetMinimum(0);
|
---|
686 | //hsig.SetContour(99);
|
---|
687 | hsig.Draw("nopal");
|
---|
688 |
|
---|
689 | c.cd();
|
---|
690 | TPad p2("Pad3", "", 0.7, 0.33, 0.99, 0.65);
|
---|
691 | p2.SetNumber(3);
|
---|
692 | p2.SetBorderMode(0);
|
---|
693 | p2.SetFrameBorderMode(0);
|
---|
694 | p2.SetFillColor(kWhite);
|
---|
695 | p2.Draw();
|
---|
696 | p2.cd();
|
---|
697 |
|
---|
698 | MHCamera htime(*fCam, "ArrivalTime", "Calibrated Arrival Time");
|
---|
699 | htime.SetYTitle("T [au]");
|
---|
700 | htime.SetCamContent(*fSig, 8);
|
---|
701 | htime.Draw("nopal");
|
---|
702 |
|
---|
703 | c.cd();
|
---|
704 | TPad p3("Pad4", "", 0.7, 0.00, 0.99, 0.32);
|
---|
705 | p3.SetNumber(4);
|
---|
706 | p3.SetBorderMode(0);
|
---|
707 | p3.SetFrameBorderMode(0);
|
---|
708 | p3.SetFillColor(kWhite);
|
---|
709 | p3.Draw();
|
---|
710 | p3.cd();
|
---|
711 | /*
|
---|
712 | TH1F htpro("TimeProj", "", slices, 0, len);
|
---|
713 | for (UInt_t i=0; i<htime.GetNumPixels(); i++)
|
---|
714 | if(htime.IsUsed(i))
|
---|
715 | htpro.Fill((htime.GetBinContent(i+1)-first)/freq, hsig.GetBinContent(i+1));
|
---|
716 | htpro.SetMinimum(0);
|
---|
717 | htpro.SetMaximum(100);
|
---|
718 | htpro.SetLineColor(kBlue);
|
---|
719 | htpro.Draw();
|
---|
720 |
|
---|
721 | TF1 fgaus("f1", "gaus");
|
---|
722 | const Double_t m = (htpro.GetMaximumBin()-0.5)*len/slices;
|
---|
723 | fgaus.SetParameter(0, htpro.GetMaximum());
|
---|
724 | fgaus.SetParameter(1, m);
|
---|
725 | fgaus.SetParameter(2, 1.0);
|
---|
726 | fgaus.SetParLimits(1, m-3, m+3);
|
---|
727 | fgaus.SetParLimits(2, 0, 3);
|
---|
728 | fgaus.SetLineWidth(1);
|
---|
729 | fgaus.SetLineColor(kMagenta);
|
---|
730 | htpro.Fit(&fgaus, "NI", "", m-3, m+3);
|
---|
731 | fgaus.Draw("same");
|
---|
732 |
|
---|
733 | g.SetMarkerStyle(kFullDotMedium);
|
---|
734 | g.Draw("PL");
|
---|
735 | //g.SetMinimum(0);
|
---|
736 | //g.SetMaximum(100);
|
---|
737 | //g.Draw("APL");
|
---|
738 |
|
---|
739 | p3.Update();
|
---|
740 | p3.cd(1);
|
---|
741 | gPad->Update();
|
---|
742 | */
|
---|
743 | c.cd();
|
---|
744 | TPad p0("MainPad", "", 0.01, 0.01, 0.69, 0.99);
|
---|
745 | p0.SetNumber(1);
|
---|
746 | p0.SetBorderMode(0);
|
---|
747 | p0.SetFrameBorderMode(0);
|
---|
748 | p0.SetFillColor(kWhite);
|
---|
749 | p0.Draw();
|
---|
750 | p0.cd();
|
---|
751 | /*
|
---|
752 | cout << "Max=" << hsig.GetMaximum() << "/" << fIn->GetMax() << " ";
|
---|
753 | cout << hsig.GetMaximum()/fIn->GetMax() << endl;
|
---|
754 | Float_t rms0 = fPed->GetAveragedRmsPerArea(*fCam, 0, fBad)[0];
|
---|
755 | Float_t rms1 = fPed->GetAveragedRmsPerArea(*fCam, 1, fBad)[0];
|
---|
756 | cout << "RMS="<<rms0<<"/"<<rms1<<endl;
|
---|
757 |
|
---|
758 | rms0 = GetMedianPedestalRms();
|
---|
759 |
|
---|
760 | cout << "MED=" << rms0 << endl;
|
---|
761 | */
|
---|
762 | MHCamera h(*fCam);
|
---|
763 | h.SetTitle(Form("%d: Run #%d, Evt %d", GetNumExecutions()+1,
|
---|
764 | fRun->GetRunNumber(), fHead->GetDAQEvtNumber()));
|
---|
765 | h.SetAllUsed();
|
---|
766 | h.SetYTitle("V [au]");
|
---|
767 | h.SetContour(99);
|
---|
768 |
|
---|
769 | h.Draw("nopal");
|
---|
770 |
|
---|
771 | // ---------------- Show data ------------------
|
---|
772 | gStyle->SetOptStat(1000000001);
|
---|
773 | /*
|
---|
774 | p0.Modified();
|
---|
775 | p1.Modified();
|
---|
776 | p2.Modified();
|
---|
777 |
|
---|
778 | p0.GetPad(1)->Modified();
|
---|
779 | p1.GetPad(1)->Modified();
|
---|
780 | p2.GetPad(1)->Modified();
|
---|
781 |
|
---|
782 | c.Update();
|
---|
783 | */
|
---|
784 |
|
---|
785 | // ---------------- Show data ------------------
|
---|
786 |
|
---|
787 | clock3.Stop();
|
---|
788 |
|
---|
789 | // Switch off automatical adding to directory (SetName would do)
|
---|
790 | const Bool_t add = TH1::AddDirectoryStatus();
|
---|
791 | TH1::AddDirectory(kFALSE);
|
---|
792 |
|
---|
793 | const Bool_t rc = Process(h, c);
|
---|
794 |
|
---|
795 | // restore previous state
|
---|
796 | TH1::AddDirectory(add);
|
---|
797 |
|
---|
798 | clockT.Stop();
|
---|
799 |
|
---|
800 | gROOT->SetBatch(batch);
|
---|
801 |
|
---|
802 | if (!rc)
|
---|
803 | return kERROR;
|
---|
804 |
|
---|
805 | return fNumEvents<=0 || GetNumExecutions()<fNumEvents;
|
---|
806 | }
|
---|
807 |
|
---|
808 | // --------------------------------------------------------------------------
|
---|
809 | //
|
---|
810 | // Check for corresponding entries in resource file and setup
|
---|
811 | //
|
---|
812 | // Example:
|
---|
813 | // MMoviewPrepare.TargetLength: 5 <seconds>
|
---|
814 | // MMoviewPrepare.NumEvents: 500
|
---|
815 | // MMoviewPrepare.Threshold: 2 <rms>
|
---|
816 | // MMoviewPrepare.Filename: movie.mpg
|
---|
817 | //
|
---|
818 | Int_t MMovieWrite::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
819 | {
|
---|
820 | Bool_t rc = kFALSE;
|
---|
821 | if (IsEnvDefined(env, prefix, "NumEvents", print))
|
---|
822 | {
|
---|
823 | fNumEvents = GetEnvValue(env, prefix, "NumEvents", (Int_t)fNumEvents);
|
---|
824 | rc = kTRUE;
|
---|
825 | }
|
---|
826 | if (IsEnvDefined(env, prefix, "TargetLength", print))
|
---|
827 | {
|
---|
828 | fTargetLength = GetEnvValue(env, prefix, "TargetLength", fTargetLength);
|
---|
829 | rc = kTRUE;
|
---|
830 | }
|
---|
831 | if (IsEnvDefined(env, prefix, "Threshold", print))
|
---|
832 | {
|
---|
833 | fThreshold = GetEnvValue(env, prefix, "Threshold", fThreshold);
|
---|
834 | rc = kTRUE;
|
---|
835 | }
|
---|
836 | if (IsEnvDefined(env, prefix, "Filename", print))
|
---|
837 | {
|
---|
838 | fFilename = GetEnvValue(env, prefix, "Filename", fFilename);
|
---|
839 | rc = kTRUE;
|
---|
840 | }
|
---|
841 | return rc;
|
---|
842 | }
|
---|