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

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