source: trunk/MagicSoft/Mars/sinope.cc@ 8102

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