source: tags/Mars-V2.1.1/sinope.cc@ 19251

Last change on this file since 19251 was 8907, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 16.0 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: sinope.cc,v 1.13 2008-06-02 08:46:36 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19!
20! Author(s): Thomas Bretz
21! Author(s): Daniela Dorner
22!
23! Copyright: MAGIC Software Development, 2000-2006
24!
25!
26\* ======================================================================== */
27#include <errno.h>
28#include <fstream>
29
30#include <TROOT.h>
31#include <TClass.h>
32#include <TApplication.h>
33#include <TObjectTable.h>
34
35#include <TH1.h>
36#include <TLine.h>
37#include <TString.h>
38#include <TVirtualPad.h>
39
40#include "MRawRunHeader.h"
41#include "MRawEvtData.h"
42#include "MRawEvtPixelIter.h"
43#include "MRawFileRead.h"
44#include "MArrayD.h"
45#include "MFTriggerPattern.h"
46#include "MTriggerPatternDecode.h"
47#include "MContinue.h"
48#include "MTaskInteractive.h"
49#include "MLog.h"
50#include "MLogManip.h"
51#include "MArgs.h"
52#include "MSequence.h"
53#include "MStatusDisplay.h"
54#include "MParList.h"
55#include "MTaskList.h"
56#include "MEvtLoop.h"
57#include "MRawFileRead.h"
58#include "MDirIter.h"
59
60using namespace std;
61
62MRawEvtData data;
63MRawRunHeader header;
64
65MArrayD artime;
66MArrayD height(256);
67Int_t entries=0;
68Int_t events =0;
69
70Int_t PreProcess(MParList *plist)
71{
72 artime.Set(header.GetNumSamplesHiGain() + header.GetNumSamplesLoGain());
73 return kTRUE;
74}
75
76Int_t Process()
77{
78 events++;
79
80 // This is the workaround to put hi- and lo-gains together
81 const Int_t nhigain = header.GetNumSamplesHiGain();
82 const Int_t nlogain = header.GetNumSamplesLoGain();
83
84 const Int_t n = nhigain+nlogain;
85
86 // Real Process
87 MRawEvtPixelIter pixel(&data);
88
89 Byte_t slices[n];
90
91 while (pixel.Next())
92 {
93 // This is the fast workaround to put hi- and lo-gains together
94 memcpy(slices, pixel.GetHiGainSamples(), nhigain);
95 memcpy(slices+nhigain, pixel.GetLoGainSamples(), nlogain);
96
97 Byte_t *max = slices;
98 Byte_t *min = slices;
99
100 for (Byte_t *b=slices+1; b<slices+n; b++)
101 {
102 if (*b>=*max)
103 max = b;
104 if (*b<=*min)
105 min = b;
106 }
107
108 const Int_t smax = max-slices;
109 const Int_t diff = *max-*min;
110
111 if (diff<50 || diff>235) // no-signal
112 continue;
113
114 height[diff]++;
115 artime[smax]++;
116 entries++;
117 }
118 return kTRUE;
119}
120
121Int_t GetFWHM(const TH1D &h, Int_t &min, Int_t &max)
122{
123 const Double_t hmax = h.GetMaximum()/2;
124 const Int_t bin = h.GetMaximumBin();
125
126 for (min=bin; min>1; min--)
127 if (h.GetBinContent(min)<hmax)
128 break;
129
130 for (max=bin; max<h.GetNbinsX(); max++)
131 if (h.GetBinContent(max)<hmax)
132 break;
133
134 return max-min;
135}
136
137TString kOutpath="";
138TString kOutfile="";
139MStatusDisplay *d=0;
140
141Int_t PostProcess()
142{
143 if (entries==0)
144 {
145 gLog << warn << "No entries processed..." << endl;
146
147 ofstream fout(Form("%stxt", kOutpath.Data()));
148 if (!fout)
149 {
150 gLog << err << "Cannot open file: " << strerror(errno) << endl;
151 return kERROR;
152 }
153
154 fout << "Events: " << events << endl;
155 fout << endl;
156
157 return kTRUE;
158 }
159
160 TH1D h1("Arrival", "Arrival Time distribution for signals", artime.GetSize(), -0.5, artime.GetSize()-0.5);
161 TH1D h2("Height", "Pulse height distribution", height.GetSize(), -0.5, height.GetSize()-0.5);
162 h1.SetXTitle("Arrival Time [slice]");
163 h2.SetXTitle("Pulse Height [cts]");
164 h1.SetDirectory(0);
165 h2.SetDirectory(0);
166 h1.SetEntries(entries);
167 h2.SetEntries(entries);
168 memcpy(h1.GetArray()+1, artime.GetArray(), artime.GetSize()*sizeof(Double_t));
169 memcpy(h2.GetArray()+1, height.GetArray(), height.GetSize()*sizeof(Double_t));
170
171 TLine l;
172 l.SetLineColor(kGreen);
173 l.SetLineWidth(2);
174
175 Int_t min, max;
176
177 //MStatusDisplay *d = new MStatusDisplay;
178 d->AddTab("Time");
179 h1.DrawCopy();
180 l.DrawLine(h1.GetMaximumBin()-1, 0, h1.GetMaximumBin()-1, h1.GetMaximum());
181 const Int_t fwhm1 = GetFWHM(h1, min, max);
182 const Bool_t asym1 = TMath::Abs((h1.GetMaximumBin()-min)-(max-h1.GetMaximumBin()))>fwhm1/2;;
183 l.DrawLine(min-1, h1.GetMaximum()/2, max-1, h1.GetMaximum()/2);
184 gPad->Update();
185
186 d->AddTab("Pulse");
187 h2.DrawCopy();
188 l.DrawLine(h2.GetMaximumBin()-1, 0, h2.GetMaximumBin()-1, h2.GetMaximum());
189 const Int_t fwhm2 = GetFWHM(h2, min, max);
190 const Bool_t asym2 = TMath::Abs((h2.GetMaximumBin()-min)-(max-h2.GetMaximumBin()))>fwhm2/2;;
191 l.DrawLine(min-1, h2.GetMaximum()/2, max-1, h2.GetMaximum()/2);
192 gPad->Update();
193
194 d->SaveAsRoot(Form("%sroot", kOutpath.Data()));
195
196 ofstream fout(Form("%stxt", kOutpath.Data()));
197 if (!fout)
198 {
199 gLog << err << "Cannot open file: " << strerror(errno) << endl;
200 return kERROR;
201 }
202
203 fout << "Events: " << events << endl;
204 fout << "HasSignal: " << (fwhm1>10?"no":"yes") << endl;
205 fout << "HasPedestal: " << (fwhm1<20?"no":"yes") << endl;
206 fout << endl;
207 fout << "PositionSignal: " << h1.GetMaximumBin()-1 << endl;
208 fout << "PositionFWHM: " << fwhm1 << endl;
209 fout << "PositionAsym: " << (asym1?"yes":"no") << endl;
210 fout << endl;
211 fout << "HeightSignal: " << h2.GetMaximumBin()-1 << endl;
212 fout << "HeightFWHM: " << fwhm2 << endl;
213 fout << "HeightAsym: " << (asym2?"yes":"no") << endl;
214 fout << endl;
215
216 return kTRUE;
217}
218
219static void StartUpMessage()
220{
221 gLog << all << endl;
222
223 // 1 2 3 4 5
224 // 12345678901234567890123456789012345678901234567890
225 gLog << "==================================================" << endl;
226 gLog << " Sinope - MARS V" << MARSVER << endl;
227 gLog << " MARS -- SImple Non Online Pulse Evaluation" << endl;
228 gLog << " Compiled with ROOT v" << ROOT_RELEASE << " on <" << __DATE__ << ">" << endl;
229 gLog << "==================================================" << endl;
230 gLog << endl;
231}
232
233static void Usage()
234{
235 // 1 2 3 4 5 6 7 8
236 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
237 gLog << all << endl;
238 gLog << "Sorry the usage is:" << endl;
239 gLog << " sinope [options] --run={number} --date={yyyy-mm-dd}" << endl << endl;
240 gLog << " Arguments:" << endl;
241 gLog << " --run={number}: Run number of run to process" << endl;
242 gLog << " --date={yy-mm-dd}: Night the run belongs to" << endl << endl;
243 gLog << " Root Options:" << endl;
244 gLog << " -b Batch mode (no graphical output to screen)" << endl<<endl;
245 gLog << " Options:" << endl;
246 gLog.Usage();
247 // gLog << " --debug-env=0 Disable debugging setting resources <default>" << endl;
248 // gLog << " --debug-env[=1] Display untouched resources after program execution" << endl;
249 // gLog << " --debug-env=2 Display untouched resources after eventloop setup" << endl;
250 // gLog << " --debug-env=3 Debug setting resources from resource file" << endl;
251 gLog << " --debug-mem Debug memory usage" << endl << endl;
252 gLog << endl;
253 gLog << " -q Quit when job is finished" << endl;
254 gLog << " -f Force overwrite of existing files" << endl;
255 gLog << " -dat Run sinope only for data events in the run" << endl;
256 gLog << " -cal Run sinope only for calibration events in the run" << endl;
257 gLog << " --ind=path Path where to search for the data file" << endl;
258 gLog << " [default=standard path in datacenter]" << endl;
259 gLog << " --out=path Path to write the all results to [def=local path]" << endl;
260 gLog << " --outf=filename Filename of the outputfiles [def=sinope{runnumber}.*]" << endl;
261 gLog << " --num={number} Number of events to process (default=1000)" << endl;
262 gLog << " --print-seq Print Sequence information" << endl;
263 gLog << " --print-files Print Files taken from Sequence" << endl;
264 gLog << " --print-found Print Files found from Sequence" << endl;
265 // gLog << " --config=callisto.rc Resource file [default=callisto.rc]" << endl;
266 gLog << endl;
267 gLog << " --version, -V Show startup message with version number" << endl;
268 gLog << " -?, -h, --help This help" << endl << endl;
269 gLog << "Background:" << endl;
270 gLog << " Sinope is Jupiter's sixteenth moon. Sinope is 28km in diameter and" << endl;
271 gLog << " and orbits 23,700,000km from Jupiter. Sinope has a mass of 8e16kg." << endl;
272 gLog << " It orbits Jupiter in 758days and is in a retrograde orbit (orbiting" << endl;
273 gLog << " opposite to the direction of Jupiter). Very little is known about" << endl;
274 gLog << " Sinope. Sinope was discovered by S.Nicholson in 1914." << endl << endl;
275 gLog << "Example:" << endl;
276 gLog << " sinope -f --date=2004-05-06 --run=32456" << endl;
277 gLog << endl;
278}
279
280static void PrintFiles(const MSequence &seq, const TString &kInpathD, Bool_t ball)
281{
282 const char *prep = ball ? "Found" : "Scheduled";
283
284 MDirIter Next;
285 seq.SetupAllRuns(Next, kInpathD, kTRUE);
286
287 gLog << all;
288 gLog.Separator(Form("%s Files", prep));
289 Next.Print(ball?"all":"");
290 gLog << endl;
291}
292
293int main(int argc, char **argv)
294{
295 if (!MARS::CheckRootVer())
296 return 0xff;
297
298 MLog::RedirectErrorHandler(MLog::kColor);
299
300 //
301 // Evaluate arguments
302 //
303 MArgs arg(argc, argv, kTRUE);
304 gLog.Setup(arg);
305
306 StartUpMessage();
307
308 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
309 return 0;
310
311 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
312 {
313 Usage();
314 return 2;
315 }
316
317 //const TString kConfig = arg.GetStringAndRemove("--config=", "callisto.rc");
318
319 const Bool_t kPrintSeq = arg.HasOnlyAndRemove("--print-seq");
320 const Bool_t kPrintFiles = arg.HasOnlyAndRemove("--print-files");
321 const Bool_t kPrintFound = arg.HasOnlyAndRemove("--print-found");
322 const Bool_t kDebugMem = arg.HasOnlyAndRemove("--debug-mem");
323 //Int_t kDebugEnv = arg.HasOnlyAndRemove("--debug-env") ? 1 : 0;
324 //kDebugEnv = arg.GetIntAndRemove("--debug-env=", kDebugEnv);
325
326 const Bool_t kQuit = arg.HasOnlyAndRemove("-q");
327 const Bool_t kBatch = arg.HasOnlyAndRemove("-b");
328 const Bool_t kOverwrite = arg.HasOnlyAndRemove("-f");
329 const Bool_t kData = arg.HasOnlyAndRemove("-dat");
330 const Bool_t kCal = arg.HasOnlyAndRemove("-cal");
331 //const Bool_t kForceExec = arg.HasOnlyAndRemove("-ff");
332
333 const TString kInpathD = arg.GetStringAndRemove("--ind=", "");
334 /*const TString*/ kOutpath = arg.GetStringAndRemove("--out=", "");
335 /*const TString*/ kOutfile = arg.GetStringAndRemove("--outf=", "");
336
337 const Int_t kNumEvents = arg.GetIntAndRemove("--num=", header.GetNumEvents());
338 const Int_t kRunNumber = arg.GetIntAndRemove("--run=", -1);
339 const TString kDate = arg.GetStringAndRemove("--date=", "");
340
341 if (arg.GetNumOptions()>0)
342 {
343 gLog << warn << "WARNING - Unknown commandline options..." << endl;
344 arg.Print("options");
345 gLog << endl;
346 Usage();
347 return 2;
348 }
349
350 if (kRunNumber<0)
351 {
352 gLog << warn << "ERROR - No '--run=' option given... required." << endl;
353 gLog << endl;
354 Usage();
355 return 2;
356 }
357 if (kDate.IsNull())
358 {
359 gLog << warn << "ERROR - No '--date=' option given... required." << endl;
360 gLog << endl;
361 Usage();
362 return 2;
363 }
364
365 //
366 // check for the right usage of the program
367 //
368 if (arg.GetNumArguments()>0)
369 {
370 Usage();
371 return 2;
372 }
373
374 if (kDebugMem)
375 TObject::SetObjectStat(kTRUE);
376
377 //
378 // Setup sequence and check its validity
379 //
380 MSequence seq;
381 seq.SetNight(kDate);
382 seq.AddRuns(kRunNumber);
383 if (kPrintSeq)
384 {
385 gLog << all;
386 gLog.Separator();
387 seq.Print();
388 gLog << endl;
389 }
390 if (!seq.IsValid())
391 {
392 gLog << err << "Sequence invalid!" << endl << endl;
393 return 2;
394 }
395
396 //
397 // Process print options
398 //
399 if (kPrintFiles)
400 PrintFiles(seq, kInpathD, kFALSE);
401 if (kPrintFound)
402 PrintFiles(seq, kInpathD, kTRUE);
403
404 if (kOutpath.IsNull())
405 {
406 kOutpath = seq.GetStandardPath();
407 kOutpath += "rawfiles/";
408 }
409 if (!kOutpath.EndsWith("/"))
410 kOutpath += "/";
411 if (kOutfile.IsNull())
412 kOutpath += Form("sinope%08d.", kRunNumber);
413 else
414 kOutpath += kOutfile+".";
415
416 if (!kOverwrite)
417 {
418 TString file = Form("%sroot", kOutpath.Data());
419 if (!gSystem->AccessPathName(file, kFileExists))
420 {
421 gLog << err << "Sorry, file '" << file << "' exists... use -f option.." << endl;
422 return 2;
423 }
424 file = Form("%stxt", kOutpath.Data());
425 if (!gSystem->AccessPathName(file, kFileExists))
426 {
427 gLog << err << "Sorry, file '" << file << "' exists... use -f option.." << endl;
428 return 2;
429 }
430 }
431
432 //
433 // Initialize root
434 //
435 MArray::Class()->IgnoreTObjectStreamer();
436 MParContainer::Class()->IgnoreTObjectStreamer();
437
438 TApplication app("sinope", &argc, argv);
439 if (!gROOT->IsBatch() && !gClient || gROOT->IsBatch() && !kBatch)
440 {
441 gLog << err << "Bombing... maybe your DISPLAY variable is not set correctly!" << endl;
442 return 1;
443 }
444
445 // ----------------------------------------------------------
446
447 /*MStatusDisplay **/d = new MStatusDisplay;
448
449 // From now on each 'Exit' means: Terminate the application
450 d->SetBit(MStatusDisplay::kExitLoopOnExit);
451 d->SetTitle(Form("Sinope #%d", kRunNumber));
452
453 MDirIter iter;
454 seq.SetupAllRuns(iter, 0, kTRUE);
455
456 MRawFileRead read;
457 read.AddFiles(iter);
458
459 MTaskInteractive task;
460
461 MTriggerPatternDecode decode;
462 MFTriggerPattern ftp;
463 if (kCal || kData)
464 {
465 ftp.SetDefault(kTRUE);
466 ftp.DenyCalibration();
467 if (!kCal)
468 {
469 ftp.DenyPedestal();
470 ftp.SetInverted();
471 }
472 }
473 MContinue conttp(&ftp, "ContTrigPattern");
474
475 task.SetPreProcess(PreProcess);
476 task.SetProcess(Process);
477 task.SetPostProcess(PostProcess);
478
479 MTaskList tlist;
480 tlist.AddToList(&read);
481 if (kCal || kData)
482 {
483 tlist.AddToList(&decode);
484 tlist.AddToList(&conttp);
485 }
486 tlist.AddToList(&task);
487
488 MParList plist;
489 plist.AddToList(&tlist);
490
491 plist.AddToList(&data);
492 plist.AddToList(&header);
493
494 MEvtLoop evtloop;
495 evtloop.SetParList(&plist);
496 evtloop.SetDisplay(d);
497
498 if (!evtloop.Eventloop(kNumEvents))
499 return 1;
500
501 if (!evtloop.GetDisplay())
502 {
503 gLog << warn << "Display closed by user... execution aborted." << endl << endl;
504 return 0;
505 }
506
507 if (kBatch || kQuit)
508 delete d;
509 else
510 {
511 // From now on each 'Close' means: Terminate the application
512 d->SetBit(MStatusDisplay::kExitLoopOnClose);
513
514 // Wait until the user decides to exit the application
515 app.Run(kFALSE);
516 }
517
518 if (TObject::GetObjectStat())
519 {
520 TObject::SetObjectStat(kFALSE);
521 gObjectTable->Print();
522 }
523
524 return 0;
525}
Note: See TracBrowser for help on using the repository browser.