source: tags/Mars-V0.9.4.2/sinope.cc@ 16284

Last change on this file since 16284 was 7262, checked in by Daniela Dorner, 19 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 on <" << __DATE__ << ">" << endl;
202 gLog << " Using ROOT v" << ROOTVER << endl;
203 gLog << "========================================================" << endl;
204 gLog << endl;
205}
206
207static void Usage()
208{
209 // 1 2 3 4 5 6 7 8
210 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
211 gLog << all << endl;
212 gLog << "Sorry the usage is:" << endl;
213 gLog << " sinope [options] --run={number} --date={yyyy-mm-dd}" << endl << endl;
214 gLog << " Arguments:" << endl;
215 gLog << " --run={number}: Run number of run to process" << endl;
216 gLog << " --date={yy-mm-dd}: Night the run belongs to" << endl << endl;
217 gLog << " Root Options:" << endl;
218 gLog << " -b Batch mode (no graphical output to screen)" << endl<<endl;
219 gLog << " Options:" << endl;
220 gLog.Usage();
221 // gLog << " --debug-env=0 Disable debugging setting resources <default>" << endl;
222 // gLog << " --debug-env[=1] Display untouched resources after program execution" << endl;
223 // gLog << " --debug-env=2 Display untouched resources after eventloop setup" << endl;
224 // gLog << " --debug-env=3 Debug setting resources from resource file" << endl;
225 gLog << " --debug-mem Debug memory usage" << endl << endl;
226 gLog << endl;
227 gLog << " -q Quit when job is finished" << endl;
228 gLog << " -f Force overwrite of existing files" << endl;
229 gLog << " -dat Run sinope only for data events in the run" << endl;
230 gLog << " -cal Run sinope only for calibration events in the run" << endl;
231 gLog << " --ind=path Path where to search for the data file" << endl;
232 gLog << " [default=standard path in datacenter]" << endl;
233 gLog << " --out=path Path to write the all results to [def=local path]" << endl;
234 gLog << " --outf=filename Filename of the outputfiles [def=sinope{runnumber}.*]" << endl;
235 gLog << " --num={number} Number of events to process (default=1000)" << endl;
236 gLog << " --print-seq Print Sequence information" << endl;
237 gLog << " --print-files Print Files taken from Sequence" << endl;
238 gLog << " --print-found Print Files found from Sequence" << endl;
239 // gLog << " --config=callisto.rc Resource file [default=callisto.rc]" << endl;
240 gLog << endl;
241 gLog << " --version, -V Show startup message with version number" << endl;
242 gLog << " -?, -h, --help This help" << endl << endl;
243 gLog << "Background:" << endl;
244 gLog << " Sinope is Jupiter's sixteenth moon. Sinope is 28km in diameter and" << endl;
245 gLog << " and orbits 23,700,000km from Jupiter. Sinope has a mass of 8e16kg." << endl;
246 gLog << " It orbits Jupiter in 758days and is in a retrograde orbit (orbiting" << endl;
247 gLog << " opposite to the direction of Jupiter). Very little is known about" << endl;
248 gLog << " Sinope. Sinope was discovered by S.Nicholson in 1914." << endl << endl;
249 gLog << "Example:" << endl;
250 gLog << " sinope -f --date=2004-05-06 --run=32456" << endl;
251 gLog << endl;
252}
253
254static void PrintFiles(const MSequence &seq, const TString &kInpathD, Bool_t all)
255{
256 const char *prep = all ? "Found" : "Scheduled";
257
258 MDirIter Next;
259 seq.SetupAllRuns(Next, kInpathD, kTRUE);
260
261 gLog << all;
262 gLog.Separator(Form("%s Files", prep));
263 Next.Print(all?"all":"");
264 gLog << endl;
265}
266
267int main(int argc, char **argv)
268{
269 //
270 // Evaluate arguments
271 //
272 MArgs arg(argc, argv, kTRUE);
273 gLog.Setup(arg);
274
275 StartUpMessage();
276
277 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
278 return 0;
279
280 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
281 {
282 Usage();
283 return 2;
284 }
285
286 //const TString kConfig = arg.GetStringAndRemove("--config=", "callisto.rc");
287
288 const Bool_t kPrintSeq = arg.HasOnlyAndRemove("--print-seq");
289 const Bool_t kPrintFiles = arg.HasOnlyAndRemove("--print-files");
290 const Bool_t kPrintFound = arg.HasOnlyAndRemove("--print-found");
291 const Bool_t kDebugMem = arg.HasOnlyAndRemove("--debug-mem");
292 //Int_t kDebugEnv = arg.HasOnlyAndRemove("--debug-env") ? 1 : 0;
293 //kDebugEnv = arg.GetIntAndRemove("--debug-env=", kDebugEnv);
294
295 const Bool_t kQuit = arg.HasOnlyAndRemove("-q");
296 const Bool_t kBatch = arg.HasOnlyAndRemove("-b");
297 const Bool_t kOverwrite = arg.HasOnlyAndRemove("-f");
298 const Bool_t kData = arg.HasOnlyAndRemove("-dat");
299 const Bool_t kCal = arg.HasOnlyAndRemove("-cal");
300 //const Bool_t kForceExec = arg.HasOnlyAndRemove("-ff");
301
302 const TString kInpathD = arg.GetStringAndRemove("--ind=", "");
303 /*const TString*/ kOutpath = arg.GetStringAndRemove("--out=", "");
304 /*const TString*/ kOutfile = arg.GetStringAndRemove("--outf=", "");
305
306 const Int_t kNumEvents = arg.GetIntAndRemove("--num=", header.GetNumEvents());
307 const Int_t kRunNumber = arg.GetIntAndRemove("--run=", -1);
308 const TString kDate = arg.GetStringAndRemove("--date=", "");
309
310 if (arg.GetNumOptions()>0)
311 {
312 gLog << warn << "WARNING - Unknown commandline options..." << endl;
313 arg.Print("options");
314 gLog << endl;
315 Usage();
316 return 2;
317 }
318
319 if (kRunNumber<0)
320 {
321 gLog << warn << "ERROR - No '--run=' option given... required." << endl;
322 gLog << endl;
323 Usage();
324 return 2;
325 }
326 if (kDate.IsNull())
327 {
328 gLog << warn << "ERROR - No '--date=' option given... required." << endl;
329 gLog << endl;
330 Usage();
331 return 2;
332 }
333
334 //
335 // check for the right usage of the program
336 //
337 if (arg.GetNumArguments()>0)
338 {
339 Usage();
340 return 2;
341 }
342
343 if (kDebugMem)
344 TObject::SetObjectStat(kTRUE);
345
346 //
347 // Setup sequence and check its validity
348 //
349 MSequence seq;
350 seq.SetNight(kDate);
351 seq.AddRuns(kRunNumber);
352 if (kPrintSeq)
353 {
354 gLog << all;
355 gLog.Separator();
356 seq.Print();
357 gLog << endl;
358 }
359 if (!seq.IsValid())
360 {
361 gLog << err << "Sequence invalid!" << endl << endl;
362 return 2;
363 }
364
365 //
366 // Process print options
367 //
368 if (kPrintFiles)
369 PrintFiles(seq, kInpathD, kFALSE);
370 if (kPrintFound)
371 PrintFiles(seq, kInpathD, kTRUE);
372
373 if (kOutpath.IsNull())
374 {
375 kOutpath = seq.GetStandardPath();
376 kOutpath += "rawfiles/";
377 }
378 if (!kOutpath.EndsWith("/"))
379 kOutpath += "/";
380 if (kOutfile.IsNull())
381 kOutpath += Form("sinope%08d.", kRunNumber);
382 else
383 kOutpath += kOutfile+".";
384
385 if (!kOverwrite)
386 {
387 TString file = Form("%sroot", kOutpath.Data());
388 if (!gSystem->AccessPathName(file, kFileExists))
389 {
390 gLog << err << "Sorry, file '" << file << "' exists... use -f option.." << endl;
391 return 2;
392 }
393 file = Form("%stxt", kOutpath.Data());
394 if (!gSystem->AccessPathName(file, kFileExists))
395 {
396 gLog << err << "Sorry, file '" << file << "' exists... use -f option.." << endl;
397 return 2;
398 }
399 }
400
401 //
402 // Initialize root
403 //
404 MArray::Class()->IgnoreTObjectStreamer();
405 MParContainer::Class()->IgnoreTObjectStreamer();
406
407 TApplication app("sinope", &argc, argv);
408 if (!gROOT->IsBatch() && !gClient || gROOT->IsBatch() && !kBatch)
409 {
410 gLog << err << "Bombing... maybe your DISPLAY variable is not set correctly!" << endl;
411 return 1;
412 }
413
414 // ----------------------------------------------------------
415
416 /*MStatusDisplay **/d = new MStatusDisplay;
417
418 // From now on each 'Exit' means: Terminate the application
419 d->SetBit(MStatusDisplay::kExitLoopOnExit);
420 d->SetTitle(Form("Sinope #%d", kRunNumber));
421
422 MDirIter iter;
423 seq.SetupAllRuns(iter, 0, kTRUE);
424
425 MRawFileRead read;
426 read.AddFiles(iter);
427
428 MTaskInteractive task;
429
430 MTriggerPatternDecode decode;
431 MFTriggerPattern ftp;
432 if (kCal || kData)
433 {
434 ftp.SetDefault(kTRUE);
435 ftp.DenyCalibration();
436 if (!kCal)
437 {
438 ftp.DenyPedestal();
439 ftp.SetInverted();
440 }
441 }
442 MContinue conttp(&ftp, "ContTrigPattern");
443
444 task.SetPreProcess(PreProcess);
445 task.SetProcess(Process);
446 task.SetPostProcess(PostProcess);
447
448 MTaskList tlist;
449 tlist.AddToList(&read);
450 if (kCal || kData)
451 {
452 tlist.AddToList(&decode);
453 tlist.AddToList(&conttp);
454 }
455 tlist.AddToList(&task);
456
457 MParList plist;
458 plist.AddToList(&tlist);
459
460 plist.AddToList(&data);
461 plist.AddToList(&header);
462
463 MEvtLoop evtloop;
464 evtloop.SetParList(&plist);
465 evtloop.SetDisplay(d);
466
467 if (!evtloop.Eventloop(kNumEvents))
468 return 1;
469
470 if (!evtloop.GetDisplay())
471 {
472 gLog << warn << "Display closed by user... execution aborted." << endl << endl;
473 return 0;
474 }
475
476 if (kBatch || kQuit)
477 delete d;
478 else
479 {
480 // From now on each 'Close' means: Terminate the application
481 d->SetBit(MStatusDisplay::kExitLoopOnClose);
482
483 // Wait until the user decides to exit the application
484 app.Run(kFALSE);
485 }
486
487 if (TObject::GetObjectStat())
488 {
489 TObject::SetObjectStat(kFALSE);
490 gObjectTable->Print();
491 }
492
493 return 0;
494}
Note: See TracBrowser for help on using the repository browser.