source: tags/Mars-V0.9.1/sinope.cc@ 10078

Last change on this file since 10078 was 6906, 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 StartUpMessage();
252
253 //
254 // Evaluate arguments
255 //
256 MArgs arg(argc, argv, kTRUE);
257
258 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
259 return 0;
260
261 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
262 {
263 Usage();
264 return 2;
265 }
266
267 gLog.Setup(arg);
268
269 //const TString kConfig = arg.GetStringAndRemove("--config=", "callisto.rc");
270
271 const Bool_t kPrintSeq = arg.HasOnlyAndRemove("--print-seq");
272 const Bool_t kPrintFiles = arg.HasOnlyAndRemove("--print-files");
273 const Bool_t kPrintFound = arg.HasOnlyAndRemove("--print-found");
274 const Bool_t kDebugMem = arg.HasOnlyAndRemove("--debug-mem");
275 //Int_t kDebugEnv = arg.HasOnlyAndRemove("--debug-env") ? 1 : 0;
276 //kDebugEnv = arg.GetIntAndRemove("--debug-env=", kDebugEnv);
277
278 const Bool_t kQuit = arg.HasOnlyAndRemove("-q");
279 const Bool_t kBatch = arg.HasOnlyAndRemove("-b");
280 const Bool_t kOverwrite = arg.HasOnlyAndRemove("-f");
281 //const Bool_t kForceExec = arg.HasOnlyAndRemove("-ff");
282
283 const TString kInpathD = arg.GetStringAndRemove("--ind=", "");
284 /*const TString*/ kOutpath = arg.GetStringAndRemove("--out=", "");
285
286 const Int_t kNumEvents = arg.GetIntAndRemove("--num=", 1000);
287 const Int_t kRunNumber = arg.GetIntAndRemove("--run=", -1);
288 const TString kDate = arg.GetStringAndRemove("--date=", "");
289
290 if (arg.GetNumOptions()>0)
291 {
292 gLog << warn << "WARNING - Unknown commandline options..." << endl;
293 arg.Print("options");
294 gLog << endl;
295 return 2;
296 }
297
298 if (kRunNumber<0)
299 {
300 gLog << warn << "ERROR - No '--run=' option given... required." << endl;
301 gLog << endl;
302 return 2;
303 }
304 if (kDate.IsNull())
305 {
306 gLog << warn << "ERROR - No '--date=' option given... required." << endl;
307 gLog << endl;
308 return 2;
309 }
310
311 //
312 // check for the right usage of the program
313 //
314 if (arg.GetNumArguments()>0)
315 {
316 Usage();
317 return 2;
318 }
319
320 if (kDebugMem)
321 TObject::SetObjectStat(kTRUE);
322
323 //
324 // Setup sequence and check its validity
325 //
326 MSequence seq;
327 seq.SetNight(kDate);
328 seq.AddRuns(kRunNumber);
329 if (kPrintSeq)
330 {
331 gLog << all;
332 gLog.Separator();
333 seq.Print();
334 gLog << endl;
335 }
336 if (!seq.IsValid())
337 {
338 gLog << err << "Sequence invalid!" << endl << endl;
339 return 2;
340 }
341
342 //
343 // Process print options
344 //
345 if (kPrintFiles)
346 PrintFiles(seq, kInpathD, kFALSE);
347 if (kPrintFound)
348 PrintFiles(seq, kInpathD, kTRUE);
349
350 if (kOutpath.IsNull())
351 {
352 kOutpath = seq.GetStandardPath();
353 kOutpath += "rawfiles/";
354 }
355 if (!kOutpath.EndsWith("/"))
356 kOutpath += "/";
357 kOutpath += Form("sinope%08d.", kRunNumber);
358
359 if (!kOverwrite)
360 {
361 TString file = Form("%sroot", kOutpath.Data());
362 if (!gSystem->AccessPathName(file, kFileExists))
363 {
364 gLog << err << "Sorry, file '" << file << "' exists... use -f option.." << endl;
365 return 2;
366 }
367 file = Form("%stxt", 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 }
374
375 //
376 // Initialize root
377 //
378 MArray::Class()->IgnoreTObjectStreamer();
379 MParContainer::Class()->IgnoreTObjectStreamer();
380
381 TApplication app("Sinope", &argc, argv);
382 if (!gROOT->IsBatch() && !gClient || gROOT->IsBatch() && !kBatch)
383 {
384 gLog << err << "Bombing... maybe your DISPLAY variable is not set correctly!" << endl;
385 return 1;
386 }
387
388 // ----------------------------------------------------------
389
390 /*MStatusDisplay **/d = new MStatusDisplay;
391
392 // From now on each 'Exit' means: Terminate the application
393 d->SetBit(MStatusDisplay::kExitLoopOnExit);
394 d->SetTitle(Form("Sinope #%d", kRunNumber));
395
396 MDirIter iter;
397 seq.SetupAllRuns(iter, 0, kTRUE);
398
399 MRawFileRead read;
400 read.AddFiles(iter);
401
402 MTaskInteractive task;
403
404 task.SetPreProcess(PreProcess);
405 task.SetProcess(Process);
406 task.SetPostProcess(PostProcess);
407
408 MTaskList tlist;
409 tlist.AddToList(&read);
410 tlist.AddToList(&task);
411
412 MParList plist;
413 plist.AddToList(&tlist);
414
415 plist.AddToList(&data);
416 plist.AddToList(&header);
417
418 MEvtLoop evtloop;
419 evtloop.SetParList(&plist);
420 evtloop.SetDisplay(d);
421
422 if (!evtloop.Eventloop(kNumEvents))
423 return 1;
424
425 if (!evtloop.GetDisplay())
426 {
427 gLog << warn << "Display closed by user... execution aborted." << endl << endl;
428 return 0;
429 }
430
431 tlist.PrintStatistics();
432
433 if (kBatch || kQuit)
434 delete d;
435 else
436 {
437 // From now on each 'Close' means: Terminate the application
438 d->SetBit(MStatusDisplay::kExitLoopOnClose);
439
440 // Wait until the user decides to exit the application
441 app.Run(kFALSE);
442 }
443
444 if (TObject::GetObjectStat())
445 {
446 TObject::SetObjectStat(kFALSE);
447 gObjectTable->Print();
448 }
449
450 return 0;
451}
Note: See TracBrowser for help on using the repository browser.