AliPhysics  db95e02 (db95e02)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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("((FastMontor*)%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  if (o->IsA()->InheritsFrom(TH1::Class())) {
371  TH1* h = static_cast<TH1*>(o);
372  TH1* c = static_cast<TH1*>(h->Clone(Form("cpy_%s", h->GetName())));
373  c->SetDirectory(0);
374  if (scale) {
375  Int_t nEvents = c->GetBinContent(0);
376  // Info("Feedback", "Scaling %s by 1./%d and width",
377  // c->GetName(), nEvents);
378  if (nEvents <= 0) return;
379  c->Scale(1./nEvents, "width");
380  c->SetMinimum(0);
381  }
382  if (nostats)
383  // suppress stats
384  c->SetStats(0);
385  c->Draw(Form("%s %s",opt, (same ? "same" : "")));
386  c->SetBit(TObject::kCanDelete);
387  }
388  else if (o->IsA()->InheritsFrom(TGraph::Class())) {
389  TGraph* g = static_cast<TGraph*>(o);
390  TGraph* c = static_cast<TGraph*>(g->Clone(Form("cpy_%s",g->GetName())));
391  c->Draw(Form("%s %s",opt, (same ? "" : "a")));
392  c->SetBit(TObject::kCanDelete);
393  // Info("DrawObject","Drawing Graph '%s' with \"%s %s\"",
394  // c->GetName(), opt, (same ? "" : "a"));
395  }
396  else if (o->IsA()->InheritsFrom(TCollection::Class())) {
397  TCollection* c = static_cast<TCollection*>(o);
398  TIter n(c);
399  TObject* co = 0;
400  Bool_t first = true;
401  // Info("DrawObject","Drawing collection '%s' with \"%s\"",
402  // c->GetName(), opt);
403  while ((co = n())) {
404  DrawObject(co, opt, scale, nostats, !first);
405  first = false;
406  }
407  }
408  else {
409  TObject* c = o->DrawClone(opt);
410  c->SetBit(TObject::kCanDelete);
411  }
412  }
420  TVirtualPad* FindPad(const TString& name)
421  {
422  TVirtualPad* p = 0;
423  Int_t i = 1;
424  TString t = Form("p_%s", name.Data());
425  while ((p = fCanvas->GetPad(i))) {
426  if (t.EqualTo(p->GetName())) return p;
427  i++;
428  }
429  return 0;
430  }
441  {
442  TString path(padName);
443  path.Remove(0,2);
444  if (path.Index("/") == kNPOS)
445  return l->FindObject(path);
446  TObjArray* tokens = path.Tokenize("/");
447  Int_t nTokens = tokens->GetEntriesFast();
448  TCollection* current = l;
449  TObject* ret = 0;
450  for (Int_t i = 0; i < nTokens; i++) {
451  TObject* o = current->FindObject(tokens->At(i)->GetName());
452  if (!o) break;
453  if (i == nTokens - 1) {
454  ret = o;
455  break;
456  }
457  if (!o->IsA()->InheritsFrom(TCollection::Class())) {
458  Warning("FindPadObject", "Path object %s of %s is not a collection "
459  "but a %s", o->GetName(), path.Data(), o->ClassName());
460  break;
461  }
462  current = static_cast<TCollection*>(o);
463  }
464  // Info("FindPadObject", "pad=%s -> %p", padName, l);
465  delete tokens;
466  if (!ret) l->ls();
467  return ret;
468  }
474  TCanvas* fCanvas;
476  TSelector* fSelector;
480  TTimer* fTimer;
482 };
483 #endif
484 //
485 // EOF
486 //
TString fName
Definition: FastMonitor.C:470
TTimer * fTimer
Definition: FastMonitor.C:480
static Long_t ProofExec(const char *cmd=0)
Definition: FastMonitor.C:56
const char * title
Definition: MakeQAPdf.C:26
virtual ~FastMonitor()
Definition: FastMonitor.C:113
TObject * FindPadObject(const Char_t *padName, TCollection *l)
Definition: FastMonitor.C:440
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:472
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
ClassDef(FastMonitor, 1)
Int_t fNPads
Definition: FastMonitor.C:478
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
Float_t nEvents[nProd]
TSelector * fSelector
Definition: FastMonitor.C:476
TVirtualPad * FindPad(const TString &name)
Definition: FastMonitor.C:420
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:474
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