source: tags/Mars-V0.10/sinope.cc@ 16065

Last change on this file since 16065 was 8011, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 14.9 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 //
272 // Evaluate arguments
273 //
274 MArgs arg(argc, argv, kTRUE);
275 gLog.Setup(arg);
276
277 StartUpMessage();
278
279 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
280 return 0;
281
282 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
283 {
284 Usage();
285 return 2;
286 }
287
288 //const TString kConfig = arg.GetStringAndRemove("--config=", "callisto.rc");
289
290 const Bool_t kPrintSeq = arg.HasOnlyAndRemove("--print-seq");
291 const Bool_t kPrintFiles = arg.HasOnlyAndRemove("--print-files");
292 const Bool_t kPrintFound = arg.HasOnlyAndRemove("--print-found");
293 const Bool_t kDebugMem = arg.HasOnlyAndRemove("--debug-mem");
294 //Int_t kDebugEnv = arg.HasOnlyAndRemove("--debug-env") ? 1 : 0;
295 //kDebugEnv = arg.GetIntAndRemove("--debug-env=", kDebugEnv);
296
297 const Bool_t kQuit = arg.HasOnlyAndRemove("-q");
298 const Bool_t kBatch = arg.HasOnlyAndRemove("-b");
299 const Bool_t kOverwrite = arg.HasOnlyAndRemove("-f");
300 const Bool_t kData = arg.HasOnlyAndRemove("-dat");
301 const Bool_t kCal = arg.HasOnlyAndRemove("-cal");
302 //const Bool_t kForceExec = arg.HasOnlyAndRemove("-ff");
303
304 const TString kInpathD = arg.GetStringAndRemove("--ind=", "");
305 /*const TString*/ kOutpath = arg.GetStringAndRemove("--out=", "");
306 /*const TString*/ kOutfile = arg.GetStringAndRemove("--outf=", "");
307
308 const Int_t kNumEvents = arg.GetIntAndRemove("--num=", header.GetNumEvents());
309 const Int_t kRunNumber = arg.GetIntAndRemove("--run=", -1);
310 const TString kDate = arg.GetStringAndRemove("--date=", "");
311
312 if (arg.GetNumOptions()>0)
313 {
314 gLog << warn << "WARNING - Unknown commandline options..." << endl;
315 arg.Print("options");
316 gLog << endl;
317 Usage();
318 return 2;
319 }
320
321 if (kRunNumber<0)
322 {
323 gLog << warn << "ERROR - No '--run=' option given... required." << endl;
324 gLog << endl;
325 Usage();
326 return 2;
327 }
328 if (kDate.IsNull())
329 {
330 gLog << warn << "ERROR - No '--date=' option given... required." << endl;
331 gLog << endl;
332 Usage();
333 return 2;
334 }
335
336 //
337 // check for the right usage of the program
338 //
339 if (arg.GetNumArguments()>0)
340 {
341 Usage();
342 return 2;
343 }
344
345 if (kDebugMem)
346 TObject::SetObjectStat(kTRUE);
347
348 //
349 // Setup sequence and check its validity
350 //
351 MSequence seq;
352 seq.SetNight(kDate);
353 seq.AddRuns(kRunNumber);
354 if (kPrintSeq)
355 {
356 gLog << all;
357 gLog.Separator();
358 seq.Print();
359 gLog << endl;
360 }
361 if (!seq.IsValid())
362 {
363 gLog << err << "Sequence invalid!" << endl << endl;
364 return 2;
365 }
366
367 //
368 // Process print options
369 //
370 if (kPrintFiles)
371 PrintFiles(seq, kInpathD, kFALSE);
372 if (kPrintFound)
373 PrintFiles(seq, kInpathD, kTRUE);
374
375 if (kOutpath.IsNull())
376 {
377 kOutpath = seq.GetStandardPath();
378 kOutpath += "rawfiles/";
379 }
380 if (!kOutpath.EndsWith("/"))
381 kOutpath += "/";
382 if (kOutfile.IsNull())
383 kOutpath += Form("sinope%08d.", kRunNumber);
384 else
385 kOutpath += kOutfile+".";
386
387 if (!kOverwrite)
388 {
389 TString file = Form("%sroot", kOutpath.Data());
390 if (!gSystem->AccessPathName(file, kFileExists))
391 {
392 gLog << err << "Sorry, file '" << file << "' exists... use -f option.." << endl;
393 return 2;
394 }
395 file = Form("%stxt", kOutpath.Data());
396 if (!gSystem->AccessPathName(file, kFileExists))
397 {
398 gLog << err << "Sorry, file '" << file << "' exists... use -f option.." << endl;
399 return 2;
400 }
401 }
402
403 //
404 // Initialize root
405 //
406 MArray::Class()->IgnoreTObjectStreamer();
407 MParContainer::Class()->IgnoreTObjectStreamer();
408
409 TApplication app("sinope", &argc, argv);
410 if (!gROOT->IsBatch() && !gClient || gROOT->IsBatch() && !kBatch)
411 {
412 gLog << err << "Bombing... maybe your DISPLAY variable is not set correctly!" << endl;
413 return 1;
414 }
415
416 // ----------------------------------------------------------
417
418 /*MStatusDisplay **/d = new MStatusDisplay;
419
420 // From now on each 'Exit' means: Terminate the application
421 d->SetBit(MStatusDisplay::kExitLoopOnExit);
422 d->SetTitle(Form("Sinope #%d", kRunNumber));
423
424 MDirIter iter;
425 seq.SetupAllRuns(iter, 0, kTRUE);
426
427 MRawFileRead read;
428 read.AddFiles(iter);
429
430 MTaskInteractive task;
431
432 MTriggerPatternDecode decode;
433 MFTriggerPattern ftp;
434 if (kCal || kData)
435 {
436 ftp.SetDefault(kTRUE);
437 ftp.DenyCalibration();
438 if (!kCal)
439 {
440 ftp.DenyPedestal();
441 ftp.SetInverted();
442 }
443 }
444 MContinue conttp(&ftp, "ContTrigPattern");
445
446 task.SetPreProcess(PreProcess);
447 task.SetProcess(Process);
448 task.SetPostProcess(PostProcess);
449
450 MTaskList tlist;
451 tlist.AddToList(&read);
452 if (kCal || kData)
453 {
454 tlist.AddToList(&decode);
455 tlist.AddToList(&conttp);
456 }
457 tlist.AddToList(&task);
458
459 MParList plist;
460 plist.AddToList(&tlist);
461
462 plist.AddToList(&data);
463 plist.AddToList(&header);
464
465 MEvtLoop evtloop;
466 evtloop.SetParList(&plist);
467 evtloop.SetDisplay(d);
468
469 if (!evtloop.Eventloop(kNumEvents))
470 return 1;
471
472 if (!evtloop.GetDisplay())
473 {
474 gLog << warn << "Display closed by user... execution aborted." << endl << endl;
475 return 0;
476 }
477
478 if (kBatch || kQuit)
479 delete d;
480 else
481 {
482 // From now on each 'Close' means: Terminate the application
483 d->SetBit(MStatusDisplay::kExitLoopOnClose);
484
485 // Wait until the user decides to exit the application
486 app.Run(kFALSE);
487 }
488
489 if (TObject::GetObjectStat())
490 {
491 TObject::SetObjectStat(kFALSE);
492 gObjectTable->Print();
493 }
494
495 return 0;
496}
Note: See TracBrowser for help on using the repository browser.