AliPhysics  8b695ca (8b695ca)
FastMonitor.C
Go to the documentation of this file.
1 #ifndef FASTMONITOR_C
2 #define FASTMONITOR_C
3 #ifndef __CINT__
4 # include <TQObject.h>
5 # include <TObject.h>
6 # include <TSelector.h>
7 # include <TCanvas.h>
8 # include <TROOT.h>
9 // # include <TProof.h>
10 # include <TGraph.h>
11 # include <TMath.h>
12 # include <TTimer.h>
13 # include <TH1.h>
14 # include <TObjArray.h>
15 # include <TClass.h>
16 #else
17 class TCanvas;
18 class TSelector;
19 class TVirtualPad;
20 class TTimer;
21 class TH1;
22 class TObjArray;
23 class TClass;
24 class TQObject;
25 // class TProof;
26 #endif
27 
28 //====================================================================
29 struct FastMonitor : public TObject, public TQObject
30 {
34  enum {
35  kLogx = 0x01,
36  kLogy = 0x02,
37  kLogz = 0x04,
38  kScale = 0x08,
39  kNoStats = 0x10
40  };
44  enum {
45  kBitScale = (1<<17), // BIT(15),
46  kBitNoStats = (1<<16) // BIT(16)
47  };
56  static Long_t ProofExec(const char* cmd=0)
57  {
58  Bool_t hasCmd = (cmd && cmd[0] != '\0');
59  TString lne;
60  lne.Form("gProof%s%s", (hasCmd ? "->" : ""), (hasCmd ? cmd : ""));
61  // Printf("FastMonitor::ProofExec: %s", lne.Data());
62  return gROOT->ProcessLine(lne);
63  }
65  : TObject(),
66  TQObject(),
67  fName("FastMonitor"),
68  fPaths(),
69  fCanvas(0),
70  fSelector(0),
71  fNPads(0),
72  fTimer(0)
73  {
74  fPaths.SetOwner();
75  }
82  FastMonitor(TSelector* s, const TString& name)
83  : TObject(),
84  TQObject(),
85  fName(name),
86  fPaths(),
87  fCanvas(0),
88  fSelector(s),
89  fNPads(0),
90  fTimer(0)
91  {
92  fPaths.SetOwner();
93  }
98  : TObject(m),
99  TQObject(),
100  fPaths(),
101  fCanvas(0),
102  fSelector(m.fSelector),
103  fTimer(0)
104  {
105  fPaths.SetOwner();
106  TIter next(&m.fPaths);
107  TObject* obj = 0;
108  while ((obj = next())) Register(obj);
109  }
113  virtual ~FastMonitor() { Disconnect(); }
122  {
123  if (&m == this) return *this;
124  fSelector = m.fSelector;
125  if (fCanvas) delete fCanvas;
126  fPaths.Clear();
127  TIter next(&m.fPaths);
128  TObject* obj = 0;
129  while ((obj = next())) Register(obj);
130  return *this;
131  }
136  void Connect(Int_t freq=-1)
137  {
138  if (gROOT->IsBatch()) {
139  Warning("FastMonitor", "Batch processing, no monitoring");
140  return;
141  }
142  if (freq <= 0) return;
143  if (ProofExec()) {
144  // We're on Proof
145  // Info("Construct", "Attaching to PROOF");
146  gROOT->ProcessLine(Form("((FastMonitor*)%p)->SetName("
147  "gProof->GetSessionTag())", this));
148  Long_t ret = ProofExec(Form("Connect(\"Feedback(TList *objs)\","
149  " \"FastMonitor\",(void*)%p,"
150  " \"Feedback(TList *objs)\")", this));
151  if (!ret) {
152  Warning("FastMonitor", "Failed to connect to Proof");
153  return;
154  }
155  ProofExec(Form("SetParameter(\"PROOF_FeedbackPeriod\",%d)",
156  freq*1000));
157  }
158  else {
159  fTimer = new TTimer(freq*1000);
160  fTimer->Connect("Timeout()","FastMonitor",this, "Handle()");
161  fTimer->Start(-1,false);
162  }
163  // Info("Connect", "Monitor %s connected", fName.Data());
164  }
169  void Disconnect()
170  {
171  if (ProofExec())
172  ProofExec(Form("Disconnect(\"Feedback(TList *objs)\","
173  "(void*)%p,\"Feedback(TList* objs)\"", this));
174  else if (fTimer)
175  fTimer->Stop();
176  }
186  void Register(TObject* descr, Bool_t proof=true)
187  {
188  Register(descr->GetName(), descr->GetTitle(), descr->GetUniqueID(), proof);
189  }
198  void Register(const char* name,
199  const char* title="",
200  UInt_t flags=0,
201  Bool_t proof=true)
202  {
203  TNamed* n = new TNamed(name, title);
204  n->SetUniqueID(flags);
205  fPaths.Add(n);
206  if (ProofExec() && proof) {
207  ProofExec(Form("AddFeedback(\"%s\")", name));
208  }
209  }
215  void SetName(const char* name) { fName = name; }
221  const char* GetName() const { return fName.Data(); }
227  void Feedback(TList* objs)
228  {
229  // Info("FeedBack", "List is %p", objs);
230  // if (objs) objs->ls();
231  if (!fCanvas && !SetupCanvas()) return;
232 
233  if (!objs) {
234  Warning("Feedback", "No list");
235  return;
236  }
237  // Info("FeedBack", "Looping over %d pads", fNPads);
238  for (Int_t iPad = 1; iPad <= fNPads; iPad++) {
239  TVirtualPad* p = fCanvas->cd(iPad);
240  // Info("Feedback", "Drawing in sub-pad # %d: %s", iPad, p->GetName());
241  TObject* o = FindPadObject(p->GetName(), objs);
242  if (!o) {
243  Warning("Feedback", "Object correspondig to pad %s (%d) not found",
244  p->GetName(), iPad);
245  iPad++;
246  continue;
247  }
248  DrawObject(o,
249  p->GetTitle(),
250  p->TestBit(kBitScale),
251  p->TestBit(kBitNoStats),
252  false);
253  p->cd();
254  p->Modified();
255  // iPad++;
256  }
257  fCanvas->Modified();
258  fCanvas->Update();
259  fCanvas->cd();
260  }
265  void Handle()
266  {
267  HandleTimer(0);
268  }
273  {
274  // Info("HandleTimer", "Selector=%p", fSelector);
275  if (!fSelector) return false;
276  Feedback(fSelector->GetOutputList());
277  return true;
278  }
279 protected:
284  {
285  if (gROOT->IsBatch()) {
286  Warning("FastMonitor", "Batch processing, no monitoring");
287  return false;
288  }
289  if (fCanvas) return true;
290 
291  // Info("SetupCanvas", "Creating canvas");
292  fCanvas = new TCanvas(fName, Form("Monitor %s", fName.Data()), 1000, 800);
293  fCanvas->SetFillColor(0);
294  fCanvas->SetFillStyle(0);
295  fCanvas->SetTopMargin(0.01);
296  fCanvas->SetRightMargin(0.01);
297 
298  Int_t nTotal = fPaths.GetEntries();
299  Int_t nRow = Int_t(TMath::Sqrt(nTotal)+.5);
300  Int_t nCol = nRow;
301  if (nCol * nRow < nTotal) nCol++;
302  fNPads = nTotal;
303  fCanvas->Divide(nCol,nRow);
304  Info("FastMonitor","Create canvas with (%dx%d) [%d] pads",nCol,nRow,nTotal);
305  TIter next(&fPaths);
306  TObject* o = 0;
307  Int_t i = 1;
308  while ((o = next()))
309  SetupDraw(i++, o->GetName(), o->GetTitle(), o->GetUniqueID());
310 
311  return true;
312  }
326  void SetupDraw(Int_t i,
327  const char* name,
328  const char* option,
329  UInt_t flags=0)
330  {
331  TVirtualPad* p = fCanvas->GetPad(i);
332  if (!p) {
333  Warning("RegisterDraw", "Not enough sub-pads (%d)", i);
334  return;
335  }
336  //Info("RegisterDraw",
337  // "Adding draw # %d %s [%s] (0x%x)",i,name,option,flags);
338  p->SetFillColor(0);
339  p->SetFillStyle(0);
340  p->SetTopMargin(0.01);
341  p->SetRightMargin(0.01);
342  p->SetName(Form("p_%s", name));
343  p->SetTitle(option);
344  if (flags & kLogx) p->SetLogx();
345  if (flags & kLogy) p->SetLogy();
346  if (flags & kLogz) p->SetLogz();
347  if (flags & kScale) p->SetBit(kBitScale);
348  if (flags & kNoStats) p->SetBit(kBitNoStats);
349 
350  fCanvas->Modified();
351  }
362  Option_t* opt,
363  Bool_t scale,
364  Bool_t nostats,
365  Bool_t same=false)
366  {
367  // Info("DrawObject","Drawing %s '%s' with \"%s %s\" (%s with%s stats)",
368  // o->ClassName(), o->GetName(), opt, (same ? "same" : ""),
369  // scale ? "scaled" : "raw", nostats ? "out" : "");
370  // Printf("M: Draw object: %s (%s)", o->GetName(), o->ClassName());
371  if (o->IsA()->InheritsFrom(TH1::Class())) {
372  TH1* h = static_cast<TH1*>(o);
373  TH1* c = static_cast<TH1*>(h->Clone(Form("cpy_%s", h->GetName())));
374  // Printf("M: Is a histogram");
375  c->SetDirectory(0);
376  if (scale) {
377  Int_t nEvents = c->GetBinContent(0);
378  // Info("Feedback", "Scaling %s by 1./%d and width",
379  // c->GetName(), nEvents);
380  if (nEvents <= 0) return;
381  c->Scale(1./nEvents, "width");
382  c->SetMinimum(0);
383  }
384  if (nostats)
385  // suppress stats
386  c->SetStats(0);
387  c->Draw(Form("%s %s",opt, (same ? "same" : "")));
388  c->SetBit(TObject::kCanDelete);
389  }
390  else if (o->IsA()->InheritsFrom(TGraph::Class())) {
391  TGraph* g = static_cast<TGraph*>(o);
392  TGraph* c = static_cast<TGraph*>(g->Clone(Form("cpy_%s",g->GetName())));
393  // Printf("M: Is a graph");
394  c->Draw(Form("%s %s",opt, (same ? "" : "a")));
395  c->SetBit(TObject::kCanDelete);
396  // Info("DrawObject","Drawing Graph '%s' with \"%s %s\"",
397  // c->GetName(), opt, (same ? "" : "a"));
398  }
399  else if (o->IsA()->InheritsFrom(TCollection::Class())) {
400  TCollection* c = static_cast<TCollection*>(o);
401  TIter n(c);
402  TObject* co = 0;
403  Bool_t first = true;
404  // Printf("M: Is a collection");
405  // Info("DrawObject","Drawing collection '%s' with \"%s\"",
406  // c->GetName(), opt);
407  while ((co = n())) {
408  DrawObject(co, opt, scale, nostats, !first);
409  first = false;
410  }
411  }
412  else {
413  Printf("M: Is a generic object");
414  TObject* c = o->DrawClone(opt);
415  c->SetBit(TObject::kCanDelete);
416  }
417  }
425  TVirtualPad* FindPad(const TString& name)
426  {
427  TVirtualPad* p = 0;
428  Int_t i = 1;
429  TString t = Form("p_%s", name.Data());
430  while ((p = fCanvas->GetPad(i))) {
431  if (t.EqualTo(p->GetName())) return p;
432  i++;
433  }
434  return 0;
435  }
446  {
447  TString path(padName);
448  path.Remove(0,2);
449  if (path.Index("/") == kNPOS)
450  return l->FindObject(path);
451  TObjArray* tokens = path.Tokenize("/");
452  Int_t nTokens = tokens->GetEntriesFast();
453  TCollection* current = l;
454  TObject* ret = 0;
455  for (Int_t i = 0; i < nTokens; i++) {
456  TObject* o = current->FindObject(tokens->At(i)->GetName());
457  if (!o) break;
458  if (i == nTokens - 1) {
459  ret = o;
460  break;
461  }
462  if (!o->IsA()->InheritsFrom(TCollection::Class())) {
463  Warning("FindPadObject", "Path object %s of %s is not a collection "
464  "but a %s", o->GetName(), path.Data(), o->ClassName());
465  break;
466  }
467  current = static_cast<TCollection*>(o);
468  }
469  // Info("FindPadObject", "pad=%s -> %p", padName, l);
470  delete tokens;
471  if (!ret) l->ls();
472  return ret;
473  }
479  TCanvas* fCanvas;
481  TSelector* fSelector;
485  TTimer* fTimer;
486  ClassDef(FastMonitor,1);
487 };
488 #endif
489 //
490 // EOF
491 //
TString fName
Definition: FastMonitor.C:475
TTimer * fTimer
Definition: FastMonitor.C:485
static Long_t ProofExec(const char *cmd=0)
Definition: FastMonitor.C:56
const char * title
Definition: MakeQAPdf.C:27
virtual ~FastMonitor()
Definition: FastMonitor.C:113
TObject * FindPadObject(const Char_t *padName, TCollection *l)
Definition: FastMonitor.C:445
void Feedback(TList *objs)
Definition: FastMonitor.C:227
char Char_t
Definition: External.C:18
TCanvas * c
Definition: TestFitELoss.C:172
FastMonitor & operator=(const FastMonitor &m)
Definition: FastMonitor.C:121
TList fPaths
Definition: FastMonitor.C:477
void Connect(Int_t freq=-1)
Definition: FastMonitor.C:136
void Handle()
Definition: FastMonitor.C:265
Bool_t HandleTimer(TTimer *)
Definition: FastMonitor.C:272
void SetupDraw(Int_t i, const char *name, const char *option, UInt_t flags=0)
Definition: FastMonitor.C:326
int Int_t
Definition: External.C:63
unsigned int UInt_t
Definition: External.C:33
Double_t nEvents
plot quality messages
Int_t fNPads
Definition: FastMonitor.C:483
const char * GetName() const
Definition: FastMonitor.C:221
void Disconnect()
Definition: FastMonitor.C:169
void Register(TObject *descr, Bool_t proof=true)
Definition: FastMonitor.C:186
Bool_t SetupCanvas()
Definition: FastMonitor.C:283
TSelector * fSelector
Definition: FastMonitor.C:481
TVirtualPad * FindPad(const TString &name)
Definition: FastMonitor.C:425
const char Option_t
Definition: External.C:48
void Register(const char *name, const char *title="", UInt_t flags=0, Bool_t proof=true)
Definition: FastMonitor.C:198
bool Bool_t
Definition: External.C:53
TCanvas * fCanvas
Definition: FastMonitor.C:479
FastMonitor(const FastMonitor &m)
Definition: FastMonitor.C:97
void SetName(const char *name)
Definition: FastMonitor.C:215
Definition: External.C:196
void DrawObject(TObject *o, Option_t *opt, Bool_t scale, Bool_t nostats, Bool_t same=false)
Definition: FastMonitor.C:361
FastMonitor(TSelector *s, const TString &name)
Definition: FastMonitor.C:82