Fancy on-line monitor
[u/mrichter/AliRoot.git] / FMD / scripts / SpectraMonitor.C
CommitLineData
2eddac03 1//
2//____________________________________________________________________
3//
4// $Id: DrawHits.C 22496 2007-11-26 13:50:44Z cholm $
5//
6// Script that contains a class to draw hits, using the
7// AliFMDInputHits class in the util library.
8//
9// Use the script `Compile.C' to compile this class using ACLic.
10//
11#include <AliCDBManager.h>
12#include <AliFMDParameters.h>
13#include <AliFMDHit.h>
14#include <AliFMDDigit.h>
15#include <AliFMDSDigit.h>
16#include <AliFMDRecPoint.h>
17#include <AliESDFMD.h>
18#include <AliFMDPattern.h>
19#include <TBrowser.h>
20#include <TDirectory.h>
21#include <TObjArray.h>
22#include <TH1.h>
23#include <TH2.h>
24#include <TH3.h>
25#include <TGraph.h>
26#include <TF1.h>
27#include <iostream>
28#include <TStyle.h>
29#include <TEnv.h>
30#include <TCanvas.h>
31#include <TGFrame.h>
32#include <TGCanvas.h>
33#include <TGListTree.h>
34#include <TGClient.h>
35#include <TSystem.h>
36#include <KeySymbols.h>
37#include <TClass.h>
38#include <RQ_OBJECT.h>
39#include <TSlider.h>
40
41namespace Spectra {
42 struct Top;
43 struct Detector;
44 struct Ring;
45 struct Sector;
46 struct Strip;
47
48 //__________________________________________________________________
49 struct Element : public TNamed
50 {
51 virtual ~Element() {}
52 virtual void Draw(Option_t* option, Double_t l, Double_t h);
53 virtual Top& GetTop() = 0;
54 virtual void MakeHistograms(TAxis* axis);
55 protected:
56 Element(const char* name, const char* title)
57 : TNamed(name, title), fFull(0), fCut(0)
58 {}
59 void DoFill(Double_t v);
60 TH1* fFull;
61 TH1* fCut;
62 };
63
64 //__________________________________________________________________
65 struct Top : public Element
66 {
67 RQ_OBJECT("Spectra::Top")
68 public:
69 Top(TGCompositeFrame& frame, TCanvas* canvas);
70 TGListTree& GetList() { return fList; }
71 Detector& GetOrAdd(UShort_t id);
72 void Fill(UShort_t d, Char_t ring,
73 UShort_t sec, UShort_t str, Double_t v);
74 TAxis* GetAxis() { return fAxis; }
75 void SetAxis(TAxis* a);
76 Top& GetTop() { return *this; }
77
78 /** Handle entries
79 @param e selected entry, if any
80 @param id Id of entry */
81 virtual void HandleEntry(TGListTreeItem* e, Int_t id);
82 /** Handle key strokes
83 @param f Item selected, if any
84 @param keysym Key symbol
85 @param mask Modifier mask */
86 virtual void HandleKey(TGListTreeItem* f, UInt_t keysym, UInt_t mask);
87 /** Handle Return
88 @param f Selected item, if any */
89 virtual void HandleReturn(TGListTreeItem* f);
90 /** Clear the list */
91 virtual void ClearList();
92 /** Clear the canvas */
93 virtual void ClearCanvas();
94 /** Update canvas */
95 virtual void UpdateCanvas();
96 /** Update canvas */
97 virtual void UpdateList();
98 /** Return the currently selected entry */
99 TGListTreeItem* CurrentEntry() const { return fCurrentEntry; }
100 /** @return the currently selected user data (possibly 0) */
101 TObject* Current() const;
102 /** Selection changed signal */
103 void SelectionChanged() { Emit("SelectionChanged()"); }//*SIGNAL*
104 /** Get Picture for 1D histogram */
105 const TGPicture* GetH1Pic() { return fHist1DIcon; }
106 /** 2D Histogram Icon */
107 const TGPicture* GetH2Pic() { return fHist2DIcon; }
108 /** 3D Histogram Icon */
109 const TGPicture* GetH3Pic() { return fHist3DIcon; }
110 /** Graph Icon */
111 const TGPicture* GetGPic() { return fGraphIcon; }
112 TGListTreeItem& GetEntry() { return fEntry; }
113 protected:
114 TGLayoutHints fHints;
115 TGCanvas fContainer;
116 TGListTree fList;
117 TObjArray fChildren;
118 TGListTreeItem* fCurrentEntry;
119 TCanvas* fCanvas;
120 /** 1D Histogram Icon */
121 const TGPicture* fHist1DIcon;
122 /** 2D Histogram Icon */
123 const TGPicture* fHist2DIcon;
124 /** 3D Histogram Icon */
125 const TGPicture* fHist3DIcon;
126 /** Graph Icon */
127 const TGPicture* fGraphIcon;
128 /** The axis to use */
129 TAxis* fAxis;
130 /** Top entry */
131 TGListTreeItem& fEntry;
132 };
133
134 //__________________________________________________________________
135 struct Detector : public Element
136 {
137 Detector(UShort_t det, Top& top);
138 UShort_t Id() const { return fId; }
139 Top& GetTop() { return fParent; }
140 Top& GetParent() { return fParent; }
141 Ring& GetOrAdd(Char_t id);
142 void Fill(Char_t ring, UShort_t sec, UShort_t str, Double_t v);
143 TGListTreeItem& GetEntry() { return fEntry; }
144 protected:
145 UShort_t fId;
146 Top& fParent;
147 TObjArray fChildren;
148 TGListTreeItem& fEntry;
149 };
150
151 //__________________________________________________________________
152 struct Ring : public Element
153 {
154 Ring(Char_t id, Detector& d);
155 Char_t Id() const { return fId; }
156 UShort_t DetectorId() { return fParent.Id(); }
157 Top& GetTop() { return fParent.GetTop(); }
158 Detector& GetDetector() { return GetParent(); }
159 Detector& GetParent() { return fParent; }
160 Sector& GetOrAdd(UShort_t id);
161 void Fill(UShort_t sec, UShort_t str, Double_t v);
162 TGListTreeItem& GetEntry() { return fEntry; }
163 protected:
164 Detector& fParent;
165 Char_t fId;
166 TObjArray fChildren;
167 TGListTreeItem& fEntry;
168 };
169
170 //__________________________________________________________________
171 struct Sector : public Element
172 {
173 Sector(UShort_t id, Ring& r);
174 UShort_t Id() const { return fId; }
175 UShort_t DetectorId() { return fParent.DetectorId(); }
176 Char_t RingId() { return fParent.Id(); }
177 Top& GetTop() { return fParent.GetTop(); }
178 Detector& GetDetector() { return fParent.GetDetector(); }
179 Ring& GetRing() { return fParent; }
180 Ring& GetParent() { return fParent; }
181 Strip& GetOrAdd(UShort_t id);
182 void Fill(UShort_t str, Double_t v);
183 TGListTreeItem& GetEntry() { return fEntry; }
184 protected:
185 Ring& fParent;
186 UShort_t fId;
187 TObjArray fChildren;
188 TGListTreeItem& fEntry;
189 };
190
191 //__________________________________________________________________
192 struct Strip : public Element
193 {
194 Strip(UShort_t id, Sector& s);
195 UShort_t Id() const { return fId; }
196 UShort_t DetectorId() { return fParent.DetectorId(); }
197 Char_t RingId() { return fParent.RingId(); }
198 UShort_t SectorId() { return fParent.Id(); }
199 Top& GetTop() { return fParent.GetTop(); }
200 Detector& GetDetector() { return fParent.GetDetector(); }
201 Ring& GetRing() { return fParent.GetRing(); }
202 Sector& GetSector() { return fParent; }
203 Sector& GetParent() { return fParent; }
204 void Fill(Double_t v);
205 TGListTreeItem& GetEntry() { return fEntry; }
206 protected:
207 Sector& fParent;
208 UShort_t fId;
209 TGListTreeItem& fEntry;
210 };
211
212 //==================================================================
213 void Element::MakeHistograms(TAxis* axis)
214 {
215 if (fFull) return;
216 if (axis->IsVariableBinSize()) {
217 fFull = new TH1F(Form("f_%s", GetName()), Form("%s spectra", GetName()),
218 axis->GetXbins()->fN, axis->GetXbins()->fArray);
219 fCut = new TH1F(Form("c_%s", GetName()),
220 Form("%s restricted spectra", GetName()),
221 axis->GetXbins()->fN, axis->GetXbins()->fArray);
222 }
223 else {
224 fFull = new TH1F(Form("f_%s", GetName()), Form("%s spectra", GetName()),
225 axis->GetNbins(), axis->GetXmin(), axis->GetXmax());
226 fCut = new TH1F(Form("c_%s", GetName()),
227 Form("%s restricted spectra", GetName()),
228 axis->GetNbins(), axis->GetXmin(), axis->GetXmax());
229 }
230 fFull->SetFillColor(kRed);
231 fFull->SetFillStyle(3001);
232 fCut->SetFillColor(kBlue);
233 fCut->SetFillStyle(3001);
234 }
235 //__________________________________________________________________
236 void Element::DoFill(Double_t v)
237 {
238 if (fFull) fFull->Fill(v);
239 }
240 //__________________________________________________________________
241 void Element::Draw(Option_t* option, Double_t l, Double_t h)
242 {
243 if (!fFull) return;
244 if (fFull->GetMinimum() < 0) gPad->SetLogy(kFALSE);
245 fFull->Draw(option);
246 Double_t lx = fFull->GetXaxis()->GetXmin();
247 Double_t hx = fFull->GetXaxis()->GetXmax();
248 Double_t rr = (hx-lx);
249 Double_t ll = rr * l + lx;
250 Double_t hh = rr * h + lx;
251 for (Int_t i = 1; i <= fFull->GetNbinsX(); i++) {
252 if (fFull->GetBinCenter(i) <= ll ||
253 fFull->GetBinCenter(i) > hh) {
254 fCut->SetBinContent(i, 0);
255 continue;
256 }
257 fCut->SetBinContent(i, fFull->GetBinContent(i));
258 }
259 fCut->Draw(Form("%s same", option));
260 }
261 //==================================================================
262 Top::Top(TGCompositeFrame& frame, TCanvas* canvas)
263 : Element("All", "Everything"),
264 fHints(kLHintsExpandX|kLHintsExpandY,3, 3, 3, 3),
265 fContainer(&frame, 200, 350),
266 fList(&fContainer, kHorizontalFrame),
267 fChildren(0),
268 fCurrentEntry(0),
269 fCanvas(canvas),
270 fHist1DIcon(gClient->GetPicture("h1_t.xpm")),
271 fHist2DIcon(gClient->GetPicture("h2_t.xpm")),
272 fHist3DIcon(gClient->GetPicture("h3_t.xpm")),
273 fGraphIcon(gClient->GetPicture("graph.xpm")),
274 fEntry(*(fList.AddItem(0, GetName(), this)))
275 {
276 fContainer.AddFrame(&fList, &fHints);
277 frame.AddFrame(&fContainer, &fHints);
278
279 fList.Connect("Clicked(TGListTreeItem*,Int_t)", "Spectra::Top", this,
280 "HandleEntry(TGListTreeItem*,Int_t)");
281 fList.Connect("KeyPressed(TGListTreeItem*,ULong_t,ULong_t)",
282 "Spectra::Top", this,
283 "HandleKey(TGListTreeItem*,UInt_t,UInt_t)");
284 fList.Connect("ReturnPressed(TGListTreeItem*)", "Spectra::Top", this,
285 "HandleReturn(TGListTreeItem*)");
286 }
287 //____________________________________________________________________
288 void
289 Top::SetAxis(TAxis* axis)
290 {
291 fAxis = axis;
292 MakeHistograms(axis);
293 }
294 //____________________________________________________________________
295 void
296 Top::ClearCanvas()
297 {
298 if (!fCanvas) return;
299 fCanvas->Clear();
300 }
301
302 //____________________________________________________________________
303 void
304 Top::ClearList()
305 {
306 fList.DeleteItem(fList.GetFirstItem());
307 UpdateList();
308 }
309
310
311 //____________________________________________________________________
312 void
313 Top::HandleReturn(TGListTreeItem * f)
314 {
315
316 if (!f) {
317 fList.UnselectAll(kFALSE);
318 fList.SetSelected(0);
319 return;
320 }
321 fList.ToggleItem(f);
322 UpdateList();
323 }
324
325
326 //____________________________________________________________________
327 void
328 Top::HandleKey(TGListTreeItem * f, UInt_t keysym, UInt_t /*mask*/)
329 {
330 if (!f) {
331 fList.UnselectAll(kFALSE);
332 fList.SetSelected(0);
333 return;
334 }
335 TGListTreeItem* next = 0;
336 switch (keysym) {
337 case kKey_Up:
338 next = f->GetPrevSibling();
339 if (!next) {
340 next = f->GetParent();
341 if (next) fList.CloseItem(next);
342 }
343 break;
344 case kKey_Down:
345 next = f->GetNextSibling();
346 if (!next && f->GetParent()) {
347 next = f->GetParent()->GetNextSibling();
348 fList.CloseItem(f->GetParent());
349 }
350 break;
351 case kKey_Left:
352 next = f->GetParent();
353 if (next) fList.CloseItem(next);
354 break;
355 case kKey_Right:
356 next = f->GetFirstChild();
357 if (next) fList.OpenItem(f);
358 break;
359 case kKey_PageUp:
360 fList.PageUp(kTRUE);
361 next = fList.GetSelected();
362 break;
363 case kKey_PageDown:
364 fList.PageDown(kTRUE);
365 next = fList.GetSelected();
366 break;
367 }
368 if (next) gClient->NeedRedraw(&fList);
369 if (next && next != f) {
370 fList.ClearHighlighted();
371 fList.SetSelected(next);
372 HandleEntry(next,0);
373 }
374 }
375
376 //____________________________________________________________________
377 void
378 Top::HandleEntry(TGListTreeItem* entry, Int_t /*id*/)
379 {
380 TGListTreeItem* old = fCurrentEntry;
381 if (entry) {
382 if (!entry->GetUserData()) return;
383 fCurrentEntry = entry;
384 }
385 else {
386 fCurrentEntry = 0;
387 ClearCanvas();
388 }
389 if (old != fCurrentEntry && fCanvas) fCanvas->cd();
390 SelectionChanged();
391 }
392
393 //____________________________________________________________________
394 void
395 Top::UpdateList()
396 {
397 gClient->NeedRedraw(&fList);
398 }
399
400 //____________________________________________________________________
401 void
402 Top::UpdateCanvas()
403 {
404 if (!fCanvas) return;
405 fCanvas->Modified();
406 fCanvas->Update();
407 fCanvas->cd();
408 }
409 //____________________________________________________________________
410 TObject* Top::Current() const
411 {
412 if (!fCurrentEntry) return 0;
413 if (!fCurrentEntry->GetUserData()) return 0;
414 return static_cast<TObject*>(fCurrentEntry->GetUserData());
415 }
416
417 //__________________________________________________________________
418 Detector& Top::GetOrAdd(UShort_t id)
419 {
420 Int_t idx = id - 1;
421 Detector* d = 0;
422 if (fChildren.GetEntriesFast() <= idx ||
423 !(d = static_cast<Detector*>(fChildren.At(idx)))) {
424 d = new Detector(id, *this);
425 fChildren.AddAtAndExpand(d, idx);
426 // fList.SortChildren(&fEntry);
427 }
428 return *d;
429 }
430 //__________________________________________________________________
431 void Top::Fill(UShort_t det, Char_t ring,
432 UShort_t sec, UShort_t str, Double_t v)
433 {
434 Detector& d = GetOrAdd(det);
435 d.Fill(ring, sec, str, v);
436 DoFill(v);
437 }
438 //==================================================================
439 Detector::Detector(UShort_t det, Top& tree)
440 : Element(Form("FMD%d", det), "FMD Sub-detector"),
441 fId(det),
442 fParent(tree),
443 fChildren(0),
444 fEntry(*(tree.GetList().AddItem(&(tree.GetEntry()), GetName())))
445 {
446 fEntry.SetUserData(this);
447 if (GetTop().GetAxis()) MakeHistograms(GetTop().GetAxis());
448 }
449 //__________________________________________________________________
450 Ring& Detector::GetOrAdd(Char_t id)
451 {
452 Int_t idx = (id == 'I' || id == 'i') ? 0 : 1;
453 Ring* r = 0;;
454 if (fChildren.GetEntriesFast() <= idx ||
455 !(r = static_cast<Ring*>(fChildren.At(idx)))) {
456 r = new Ring(id, *this);
457 fChildren.AddAtAndExpand(r, idx);
458 // GetTop().GetList().SortChildren(&fEntry);
459 }
460 return *r;
461 }
462 //__________________________________________________________________
463 void Detector::Fill(Char_t ring, UShort_t sec, UShort_t str, Double_t v)
464 {
465 Ring& r = GetOrAdd(ring);
466 r.Fill(sec, str, v);
467 DoFill(v);
468 }
469 //==================================================================
470 Ring::Ring(Char_t id, Detector& d)
471 : Element(Form("FMD%d%c", d.Id(), id), "FMD Ring"),
472 fParent(d),
473 fId(id),
474 fChildren(0),
475 fEntry(*(GetTop().GetList().AddItem(&(d.GetEntry()), GetName(), this)))
476 {
477 if (GetTop().GetAxis()) MakeHistograms(GetTop().GetAxis());
478 }
479 //__________________________________________________________________
480 Sector& Ring::GetOrAdd(UShort_t id)
481 {
482 Sector* s = 0;
483 if (fChildren.GetEntriesFast() <= id ||
484 !(s = static_cast<Sector*>(fChildren.At(id)))) {
485 s = new Sector(id, *this);
486 fChildren.AddAtAndExpand(s, id);
487 // GetTop().GetList().SortChildren(&fEntry);
488 }
489 return *s;
490 }
491 //__________________________________________________________________
492 void Ring::Fill(UShort_t sec, UShort_t str, Double_t v)
493 {
494 Sector& s = GetOrAdd(sec);
495 s.Fill(str, v);
496 DoFill(v);
497 }
498 //==================================================================
499 Sector::Sector(UShort_t id, Ring& r)
500 : Element(Form("FMD%d%c_%02d", r.DetectorId(), r.Id(), id), "FMD Sector"),
501 fParent(r),
502 fId(id),
503 fChildren(0),
504 fEntry(*(GetTop().GetList().AddItem(&(r.GetEntry()), GetName(), this)))
505 {
506 if (GetTop().GetAxis()) MakeHistograms(GetTop().GetAxis());
507 }
508 //__________________________________________________________________
509 Strip& Sector::GetOrAdd(UShort_t id)
510 {
511 Strip* s = 0;
512 if (fChildren.GetEntriesFast() <= id ||
513 !(s = static_cast<Strip*>(fChildren.At(id)))) {
514 s = new Strip(id, *this);
515 fChildren.AddAtAndExpand(s, id);
516 // GetTop().GetList().SortChildren(&fEntry);
517 }
518 return *s;
519 }
520 //__________________________________________________________________
521 void Sector::Fill(UShort_t str, Double_t v)
522 {
523 Strip& s = GetOrAdd(str);
524 s.Fill(v);
525 DoFill(v);
526 }
527 //==================================================================
528 Strip::Strip(UShort_t id, Sector& s)
529 : Element(Form("FMD%d%c_%02d_%03d", s.DetectorId(), s.RingId(),
530 s.Id(), id), "FMD Strip"),
531 fParent(s),
532 fId(id),
533 fEntry(*(GetTop().GetList().AddItem(&(s.GetEntry()), GetName(), this)))
534 {
535 fEntry.SetPictures(GetTop().GetH1Pic(), GetTop().GetH1Pic());
536 if (GetTop().GetAxis()) MakeHistograms(GetTop().GetAxis());
537 }
538 //__________________________________________________________________
539 void Strip::Fill(Double_t v)
540 {
541 DoFill(v);
542 }
543}
544
545
546//====================================================================
547class SpectraMonitor : public AliFMDPattern
548{
549private:
550 TGMainFrame fSelector;
551 Spectra::Top fTop;
552public:
553 SpectraMonitor()
554 : AliFMDPattern(),
555 fSelector(gClient->GetRoot(), 100, 100),
556 fTop(fSelector, fAux)
557 {
558 AddLoad(AliFMDInput::kRaw);
559 SetName("RAW");
560 SetTitle("RAW");
561
562 fTop.Connect("SelectionChanged()", "SpectraMonitor", this, "HandleDraw()");
563
564 fSelector.MapSubwindows();
565 fSelector.Resize(fSelector.GetDefaultSize());
566 fSelector.MapWindow();
567 }
568 // void SetAxis(TAxis* axis) { fTop.SetAxis(axis); }
569 Bool_t HandleDraw()
570 {
571 TObject* user = fTop.Current();
572 if (!user) return kFALSE;
573 if (!user->InheritsFrom(Spectra::Element::Class())) {
574 Warning("HandleDraw", "%s does not inherit from Spectra::Element",
575 user->GetName());
576 return kFALSE;
577 }
578 fAux->cd();
579 Spectra::Element* e = static_cast<Spectra::Element*>(user);
580 e->Draw("hist", fSlider->GetMinimum(), fSlider->GetMaximum());
581 fAux->Modified();
582 fAux->Update();
583 fAux->cd();
584 return kTRUE;
585 }
586 void MakeAux()
587 {
588 AliFMDPattern::MakeAux();
589 if (!fAux) return;
590 fTop.SetAxis(fSpec->GetXaxis());
591 }
592 void DrawAux()
593 {
594 // Draw in the Aux the canvas
595 // For example draw the spectra
596 // or such stuff
597 if (fTop.Current() != &fTop && HandleDraw()) return;
598 AliFMDPattern::DrawAux();
599 }
600 Bool_t ProcessHit(AliFMDHit* hit, TParticle* p)
601 {
602 fTop.Fill(hit->Detector(),
603 hit->Ring(),
604 hit->Sector(),
605 hit->Strip(),
606 hit->Edep());
607 return AliFMDPattern::ProcessHit(hit, p);
608 }
609 Bool_t ProcessDigit(AliFMDDigit* digit)
610 {
611 fTop.Fill(digit->Detector(),
612 digit->Ring(),
613 digit->Sector(),
614 digit->Strip(),
615 digit->Counts());
616 return AliFMDDisplay::ProcessDigit(digit);
617 }
618 Bool_t ProcessSDigit(AliFMDSDigit* sdigit)
619 {
620 fTop.Fill(sdigit->Detector(),
621 sdigit->Ring(),
622 sdigit->Sector(),
623 sdigit->Strip(),
624 sdigit->Counts());
625 return AliFMDDisplay::ProcessSDigit(sdigit);
626 }
627 Bool_t ProcessRawDigit(AliFMDDigit* digit)
628 {
629 return ProcessDigit(digit);
630 }
631 Bool_t ProcessRecPoint(AliFMDRecPoint* recpoint)
632 {
633 fTop.Fill(recpoint->Detector(),
634 recpoint->Ring(),
635 recpoint->Sector(),
636 recpoint->Strip(),
637 recpoint->Particles());
638 return AliFMDDisplay::ProcessRecPoint(recpoint);
639 }
640 Bool_t ProcessESD(UShort_t det, Char_t rng, UShort_t sec, UShort_t str,
641 Float_t x, Float_t mult)
642 {
643 fTop.Fill(det, rng, sec, str, mult);
644 return AliFMDDisplay::ProcessESD(det, rng, sec, str, x, mult);
645 }
646};
647
648void
649RunSpectraMonitor(const char* file="", UShort_t over=0)
650{
651 // AliLog::SetModuleDebugLevel("FMD", 8);
652 AliCDBManager* cdb = AliCDBManager::Instance();
653 cdb->SetDefaultStorage("local://$ALICE_ROOT");
654 cdb->SetRun(0);
655 UInt_t what = (AliFMDParameters::kPulseGain |
656 AliFMDParameters::kPedestal |
657 AliFMDParameters::kDeadMap |
658 AliFMDParameters::kZeroSuppression|
659 AliFMDParameters::kAltroMap |
660 AliFMDParameters::kStripRange);
661 if (over != 0) what |= AliFMDParameters::kSampleRate;
662 AliFMDParameters::Instance()->Init(kFALSE, what);
663 if (over != 0) AliFMDParameters::Instance()->SetSampleRate(over);
664
665 SpectraMonitor* d = new SpectraMonitor;
666 d->AddLoad(AliFMDInput::kRaw);
667 d->SetRawFile(file);
668 d->Run();
669}
670
671
672
673
674//____________________________________________________________________
675//
676// EOF
677//