source: tags/Mars-V0.9.3/sinope.cc@ 14902

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