GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WatchersUtilities.C
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) IPNL, IN2P3, CNRS *
3  * Contibutor(s) : *
4  * Jeremie Dudouet dudouet(AT)ipnl.in2p3.fr [2014] *
5  * Olivier Stezowski stezow(AT)ipnl.in2p3.fr [2014] *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #include <fstream>
24 
25 #include "TFolder.h"
26 #include "TList.h"
27 #include "TClassMenuItem.h"
28 #include "TClass.h"
29 #include "TVirtualPad.h"
30 #include "TSocket.h"
31 
32 #include "PadManager.h"
33 
35 namespace {
36  // wheel color use to display spectra on the same pad
37  const Int_t nb_color_wheel = 20;
38  Color_t color_wheel[nb_color_wheel] = {
39  kBlue+0, kMagenta+0, kRed+0, kGreen+0, kCyan+0,
40  kBlue+1, kMagenta+1, kRed+1, kGreen+1, kCyan+1,
41  kBlue+2, kMagenta+2, kRed+2, kGreen+2, kCyan+2,
42  kBlue+3, kMagenta+3, kRed+3, kGreen+3, kCyan+3
43  };
44 }
45 
46 
47 //void Count(const char *include="* {TH*}", const char *exclude="- {TH2*}", const char *drawopt="clone !wheel", int searchdepth = kMaxInt, TObject *topfolder = 0x0);
48 
50 //void ZeroOnFolder(const char *include="* {TH*}", const char *exclude="* {TH2*}", int searchdepth = kMaxInt, TObject *topfolder = 0x0);
51 void ZeroOnFolder(const char *include="* {TH*}", const char *exclude="- {TH2*}", const char *drawopt="ICE", int searchdepth = kMaxInt, TObject *topfolder = 0x0);
52 
54 void DisplayFolder(const char *include="* {TH*}", const char *exclude="- {TH2*}", const char *drawopt="clone !wheel", int searchdepth = kMaxInt, TObject *topfolder = 0x0);
55 
57 void DisplayFolderSUM(const char *include="* {TH1*}", const char *exclude="- {TH2*}", const char *drawopt="", int searchdepth = kMaxInt, TObject *topfolder = 0x0);
58 
60 void WatchersUtilities(const Char_t * /*WhatMethod*/ = "first")
61 {
62  // Add some method to the folder
63  TClass *cl = TFolder::Class(); TList *l = cl->GetMenuList(); TClassMenuItem *n = 0x0;
64 
65 
66  n = new TClassMenuItem(TClassMenuItem::kPopupUserFunction,cl,
67  "Zero","ZeroOnFolder",0,"char*,char*,char *,int,int,int,TObject*",4);
68  l->AddFirst(n);
69  n = new TClassMenuItem(TClassMenuItem::kPopupUserFunction,cl,
70  "DisplaySUM","DisplayFolderSUM",0,"char*,char*,char *,int,int,int,TObject*",4);
71  l->AddFirst(n);
72  n = new TClassMenuItem(TClassMenuItem::kPopupUserFunction,cl,
73  "Display","DisplayFolder",0,"char*,char*,char *,int,int,int,TObject*",4);
74  l->AddFirst(n);
75  // n = new TClassMenuItem(TClassMenuItem::kPopupUserFunction,cl,
76  // "Count","Count",0,"char*,char*,char *,int,int,int,TObject*",4);
77  // l->AddFirst(n);
78 }
79 /*
80 void Myls(const char *include, const char *exclude, TString drawopt, int searchdepth, TObject *topfolder)
81 {
82  // First collect all the spectra
83  TString include = inc, exclude = exc, include_name = "*", include_class = "*", exlude_name = "", exclude_class = "", tmp_obj = "", tmp_token = "",
84  draw_option = draw, h_draw_opt = "";
85 
86  TList collected;
87  TObject *obj = 0x0, *obj_to_draw = 0x0 ;
88  TFolder *topfolder =
89  (TFolder *)folder;
90  TVirtualPad *top_pad = TVirtualPad::Pad();
91 
92  if ( ! folder->InheritsFrom("TFolder") || TVirtualPad::Pad() == 0x0 ) {
93  return;
94  }
95 
96  // separation filter on name and classname
97  Ssiz_t begin_ = include.Index("{"), end_ = include.Index("}");
98  if ( begin_ != (const Ssiz_t)(-1) ) {
99  if ( end_ == (const Ssiz_t)(-1) ) {
100  end_ = include.Length();
101  }
102  if ( begin_ < end_ ) {
103  include_class = include(begin_+1,end_-begin_-1);
104  include.Remove(begin_,end_-begin_+1);
105  }
106  }
107  begin_ = exclude.Index("{"), end_ = exclude.Index("}");
108  if ( begin_ != (const Ssiz_t)(-1) ) {
109  if ( end_ == (const Ssiz_t)(-1) ) {
110  end_ = exclude.Length();
111  }
112  if ( begin_ < end_ ) {
113  exclude_class = exclude(begin_+1,end_-begin_-1);
114  exclude.Remove(begin_,end_-begin_+1);
115  }
116  }
117 
118  // collect according to includes/excludes
119  Gw::PadManager::Collect<TObject>(&collected,topfolder,include,exclude,include_class,exclude_class,depth);
120 
121  std::cout <<
122 }
123 */
124 
125 void ZeroOnFolder(const char *inc, const char *exc, const char *ropt, int depth, TObject *folder)
126 {
127  // First collect all the spectra
128  TString include = inc, exclude = exc, include_name = "*", include_class = "*", exlude_name = "", exclude_class = "", tmp_obj = "", tmp_token = "";
129  TList collected;
130  TObject *obj = 0x0, *obj_to_draw = 0x0 ;
131  TFolder *topfolder =
132  (TFolder *)folder;
133  TVirtualPad *top_pad = TVirtualPad::Pad();
134 
135  if ( ! folder->InheritsFrom("TFolder") || TVirtualPad::Pad() == 0x0 ) {
136  return;
137  }
138 
139  // separation filter on name and classname
140  Ssiz_t begin_ = include.Index("{"), end_ = include.Index("}");
141  if ( begin_ != (const Ssiz_t)(-1) ) {
142  if ( end_ == (const Ssiz_t)(-1) ) {
143  end_ = include.Length();
144  }
145  if ( begin_ < end_ ) {
146  include_class = include(begin_+1,end_-begin_-1);
147  include.Remove(begin_,end_-begin_+1);
148  }
149  }
150  begin_ = exclude.Index("{"), end_ = exclude.Index("}");
151  if ( begin_ != (const Ssiz_t)(-1) ) {
152  if ( end_ == (const Ssiz_t)(-1) ) {
153  end_ = exclude.Length();
154  }
155  if ( begin_ < end_ ) {
156  exclude_class = exclude(begin_+1,end_-begin_-1);
157  exclude.Remove(begin_,end_-begin_+1);
158  }
159  }
160 
161  // collect according to includes/excludes
162  Gw::PadManager::Collect<TObject>(&collected,topfolder,include,exclude,include_class,exclude_class,depth);
163 
164  printf("From TFolder::Drawall, %d object to be drawn \n",collected.GetSize());
165  collected.ls();
166 
167  //
168  if ( collected.GetSize() ) {
169 
170  TIter next(&collected);
171  while ( (obj = next()) ) {
172 
173  if ( obj->InheritsFrom("TH1") ) {
174  ((TH1 *)obj)->Reset(ropt);
175  }
176 
177  }
178  }
179 }
180 
181 
182 
184 void DisplayFolder(const char *inc, const char *exc, const char *draw, int depth, TObject *folder)
185 {
186  // First collect all the spectra
187  TString include = inc, exclude = exc, include_name = "*", include_class = "*", exlude_name = "", exclude_class = "", tmp_obj = "", tmp_token = "",
188  draw_option = draw, h_draw_opt = "";
189  TList collected;
190  TObject *obj = 0x0, *obj_to_draw = 0x0 ;
191  TFolder *topfolder =
192  (TFolder *)folder;
193  TVirtualPad *top_pad = TVirtualPad::Pad();
194 
195  if ( ! folder->InheritsFrom("TFolder") || TVirtualPad::Pad() == 0x0 ) {
196  return;
197  }
198 
199  // separation filter on name and classname
200  Ssiz_t begin_ = include.Index("{"), end_ = include.Index("}");
201  if ( begin_ != (const Ssiz_t)(-1) ) {
202  if ( end_ == (const Ssiz_t)(-1) ) {
203  end_ = include.Length();
204  }
205  if ( begin_ < end_ ) {
206  include_class = include(begin_+1,end_-begin_-1);
207  include.Remove(begin_,end_-begin_+1);
208  }
209  }
210  begin_ = exclude.Index("{"), end_ = exclude.Index("}");
211  if ( begin_ != (const Ssiz_t)(-1) ) {
212  if ( end_ == (const Ssiz_t)(-1) ) {
213  end_ = exclude.Length();
214  }
215  if ( begin_ < end_ ) {
216  exclude_class = exclude(begin_+1,end_-begin_-1);
217  exclude.Remove(begin_,end_-begin_+1);
218  }
219  }
220 
221  // collect according to includes/excludes
222  Gw::PadManager::Collect<TObject>(&collected,topfolder,include,exclude,include_class,exclude_class,depth);
223 
224 // printf("From TFolder::Drawall, %d object to be drawn \n");
225 // collected.ls();
226 
227  //
228  if ( collected.GetSize() ) {
229 
230  // treat drawing options
231  Bool_t do_clone = false, do_wheel = false;
232  //
233  h_draw_opt = draw_option;
234  if ( h_draw_opt.Contains("clone") ) {
235  if ( h_draw_opt.Contains("!clone") ) {
236  h_draw_opt.ReplaceAll("!clone","");
237  }
238  else { h_draw_opt.ReplaceAll("clone",""); do_clone = true; }
239  }
240  if ( h_draw_opt.Contains("wheel") ) {
241  if ( h_draw_opt.Contains("!wheel") ) {
242  h_draw_opt.ReplaceAll("!wheel","");
243  }
244  else {
245  h_draw_opt.ReplaceAll("!wheel","");
246  if ( do_clone )
247  do_wheel = true;
248  }
249  }
250 
251  TIter next(&collected); Int_t pad_number = top_pad->GetPad(1) ? 1 : 0; Int_t color = 0;
252  while ( (obj = next()) ) {
253 
254  if ( top_pad->GetPad(pad_number) ) {
255  top_pad->cd(pad_number++);
256  }
257  else { // same pad
258  if ( !h_draw_opt.Contains("same") ) {
259  h_draw_opt += " same ";
260  }
261  }
262 
263  obj_to_draw = obj;
264  if ( draw_option.Contains("clone") && !draw_option.Contains("!clone") ) {
265 
266  }
267 // printf("PAD # %d %s\n",pad_number,obj_to_draw->GetName());
268 
269  if ( h_draw_opt.IsWhitespace() ) {
270  obj_to_draw->Draw();
271  }
272  else obj_to_draw->Draw(h_draw_opt);
273 
274  //
275  if ( obj_to_draw->InheritsFrom("TAttLine") && do_wheel ) {
276  TAttLine *line = dynamic_cast <TAttLine*> (obj_to_draw);
277  line->SetLineColor( color_wheel[color % nb_color_wheel] ); color++;
278  }
279 
280  }
281 
282  top_pad->Update(); top_pad->cd();
283  }
284 }
285 
286 void DisplayFolderSUM(const char *inc, const char *exc, const char *draw, int depth, TObject *folder)
287 {
288  // First collect all the spectra
289  TString include = inc, exclude = exc, include_name = "*", include_class = "*", exlude_name = "", exclude_class = "", tmp_obj = "", tmp_token = "",
290  draw_option = draw, h_draw_opt = "";
291  TList collected;
292  TFolder *topfolder =
293  (TFolder *)folder;
294  TVirtualPad *top_pad = TVirtualPad::Pad();
295 
296  if ( ! folder->InheritsFrom("TFolder") || TVirtualPad::Pad() == 0x0 ) {
297  return;
298  }
299 
300  // separation filter on name and classname
301  Ssiz_t begin_ = include.Index("{"), end_ = include.Index("}");
302  if ( begin_ != (const Ssiz_t)(-1) ) {
303  if ( end_ == (const Ssiz_t)(-1) ) {
304  end_ = include.Length();
305  }
306  if ( begin_ < end_ ) {
307  include_class = include(begin_+1,end_-begin_-1);
308  include.Remove(begin_,end_-begin_+1);
309  }
310  }
311  begin_ = exclude.Index("{"), end_ = exclude.Index("}");
312  if ( begin_ != (const Ssiz_t)(-1) ) {
313  if ( end_ == (const Ssiz_t)(-1) ) {
314  end_ = exclude.Length();
315  }
316  if ( begin_ < end_ ) {
317  exclude_class = exclude(begin_+1,end_-begin_-1);
318  exclude.Remove(begin_,end_-begin_+1);
319  }
320  }
321 
322  // collect according to includes/excludes
323  Gw::PadManager::Collect<TH1>(&collected,topfolder,include,exclude,include_class,exclude_class,depth);
324 
325  // printf("From TFolder::Drawall, %d object to be drawn \n");
326  // collected.ls();
327 
328  //
329  if ( collected.GetSize() ) {
330 
331  TH1 *hsum, *h;
332 
333  hsum = (TH1 *)collected.First()->Clone(Form("SUM_OF_%s",topfolder->GetName()));
334  hsum->SetTitle( Form("SUM of histograms from %s with pattern: %s and %s",topfolder->GetName(),inc,exc) );
335  //hsum->SetDirectory(0x0);
336 
337  TIter next(&collected);
338  while ( (h = (TH1 *)next()) ) {
339  hsum->Add(h);
340  }
341  hsum->Draw(draw);
342  top_pad->Update(); top_pad->cd();
343  }
344 }
345 
347 
348 namespace { TList PingNames; char buffer[4*1024*1024]; }
349 
351 /*
352 */
353 void LoadPing( const char * filename, Option_t * opt = "+" )
354 {
355  TString option = opt;
356  std::ifstream inputfile(filename);
357  TList listName_tmp;
358  std::string machineName;
359  std::string portName;
360  bool test_is;
361  TNamed * tmp_name = NULL;
362  while(1){
363  machineName.clear();
364  portName.clear();
365  inputfile >> machineName >> portName;
366  if( inputfile.eof() ) break;
367  std::cerr << machineName << " " << portName << std::endl;
368  listName_tmp.Add(new TNamed( machineName.c_str(),
369  portName.c_str() ) );
370 
371  };
372  std::cerr << listName_tmp.GetSize() << std::endl;
373  if( PingNames.GetSize() == 0 && option.Contains("+") ){
374  for( Int_t i = 0; i < listName_tmp.GetSize(); i++)
375  PingNames.Add( listName_tmp.At(i) );
376  listName_tmp.Clear("nodelete");
377  }
378  else if( option.Contains("+") ){
379  for( Int_t i = 0; i<listName_tmp.GetSize(); i++){
380  test_is = false;
381  for( Int_t j = 0; j<PingNames.GetSize(); j++){
382  if(std::string(PingNames.At(j)->GetName()) ==
383  std::string(listName_tmp.At(i)->GetName()) &&
384  std::string(PingNames.At(j)->GetTitle()) ==
385  std::string(listName_tmp.At(i)->GetTitle()) ) test_is = true;
386  }
387  if( test_is == false ) PingNames.Add(listName_tmp.At(i));
388  }
389  listName_tmp.Clear("nodelete");
390  }
391 
392  if( option.Contains("-") ){
393  for( Int_t i = 0; i<listName_tmp.GetSize(); i++ ){
394  test_is = false;
395  for( Int_t j = 0; j<PingNames.GetSize(); j++){
396  if(std::string(PingNames.At(j)->GetName()) ==
397  std::string(listName_tmp.At(i)->GetName()) &&
398  std::string(PingNames.At(j)->GetTitle()) ==
399  std::string(listName_tmp.At(i)->GetTitle()) ){
400  test_is = true;
401  tmp_name = (TNamed*)PingNames.At(j);
402  }
403  }
404  if( test_is == true ){
405  PingNames.Remove(tmp_name);
406  delete tmp_name;
407  }
408  }
409  listName_tmp.Clear("delete");
410  }
411 
412  PingNames.Print();
413  for( Int_t ii = 0; ii < PingNames.GetSize(); ii++ ){
414  std::cerr << PingNames.At(ii)->GetName() << " " << PingNames.At(ii)->GetTitle() << std::endl;}
415 }
416 
418 /*
419  */
420 void Ping( UInt_t nbtimes = 5, bool dodump = false)
421 {
422  TIter next(&PingNames);
423  TObject * obj;
424  while ( (obj = next()) ) {
425 
426  TString option = obj->GetTitle(), ifdump;
427  cout << "Ping on : " << obj->GetName() << " with Port # " << obj->GetTitle() << endl;
428 
429  TSocket *fSocket = new TSocket(obj->GetName(),option.Atoi());
430  if ( fSocket ) {
431  if ( fSocket->IsValid() ) {
432  Int_t bsize, ret;
433  cout << "Select with 1 millisecond " << endl;
434  for (UInt_t i = 0u; i < nbtimes; i++) {
435  cout << " - try # " << i << endl;
436  ifdump = Form("%s_%s_%d",obj->GetName(),obj->GetTitle(),i);
437 
438  switch ( fSocket->Select(TSocket::kRead,1000) )
439  {
440  case 0:
441  cout << "--> timeout " << endl;
442  break;
443  case -1:
444  cout << "--> Error " << endl;
445  break;
446  default:
447  fSocket->RecvRaw(&bsize, sizeof(Int_t));
448  cout << " Size expected " << bsize << endl;
449  ret = fSocket->RecvRaw(buffer, bsize);
450  cout << " Size received " << ret << endl;
451  if ( dodump ) {
452  ofstream file(ifdump.Data(),ios_base::app);
453  file << bsize;
454  file.write(buffer,bsize);
455  }
456  break;
457  }
458  }
459  }
460  else cout << " *** Invalid connection *** " << endl;
461  }
462  else cout << " *** Invalid connection *** " << endl;
463  delete fSocket;
464  }
465 }
466 
467 void Ping(const char *machine, UInt_t port, UInt_t nbtimes = 5, Option_t *opt = "")
468 {
469  TString option = opt, sport = Form("%d",port);
470  bool test_is;
471  TNamed * tmp_name = NULL;
472  // add or removed from the list
473  if ( option.Contains("+") ) {
474  test_is = false;
475  for( Int_t i = 0; i < PingNames.GetSize(); i++ )
476  if( std::string(PingNames.At(i)->GetName()) == std::string(machine) &&
477  std::string(PingNames.At(i)->GetTitle()) == std::string(sport) ) test_is = true;
478  if( test_is == false )
479  PingNames.Add ( new TNamed(machine,sport.Data()) );
480  }
481  if ( option.Contains("-") ){
482  test_is = false;
483  for( Int_t i = 0; i < PingNames.GetSize(); i++ )
484  if( std::string(PingNames.At(i)->GetName()) == std::string(machine) &&
485  std::string(PingNames.At(i)->GetTitle()) == std::string(sport) ) {
486  test_is = true;
487  tmp_name = (TNamed*)PingNames.At(i);
488  }
489  if ( test_is == true ) {
490  PingNames.Remove(tmp_name);
491  delete tmp_name;
492  }
493  }
494 
495  Ping(nbtimes);
496 }
497 
499 /*
500  */
501 void SSHPing( UInt_t nbtimes = 1, bool dodump = false)
502 {
503 // TIter next(&PingNames);
504 // TObject * obj;
505 // while ( (obj = next()) ) {
506 
507  TNamed *obj = new TNamed("localhost","10001");
508 
509  TString option = obj->GetTitle(), ifdump;
510  cout << "Ping on : " << obj->GetName() << " with Port # " << obj->GetTitle() << endl;
511 
512 
513  TSocket *fSocket = new TSocket(obj->GetName(),option.Atoi());
514  if ( fSocket ) {
515  if ( fSocket->IsValid() ) {
516  Int_t bsize, ret;
517  cout << "Select with 1 millisecond " << endl;
518  for (UInt_t i = 0u; i < nbtimes; i++) {
519  cout << " - try # " << i << endl;
520  ifdump = Form("%s_%s_%d",obj->GetName(),obj->GetTitle(),i);
521 
522  switch ( fSocket->Select(TSocket::kRead,1000) )
523  {
524  case 0:
525  cout << "--> timeout " << endl;
526  break;
527  case -1:
528  cout << "--> Error " << endl;
529  break;
530  default:
531  fSocket->RecvRaw(&bsize, sizeof(Int_t));
532  cout << " Size expected " << bsize << endl;
533  ret = fSocket->RecvRaw(buffer, bsize);
534  cout << " Size received " << ret << endl;
535  if ( dodump ) {
536  ofstream file(ifdump.Data(),ios_base::app);
537  file << bsize;
538  file.write(buffer,bsize);
539  }
540  break;
541  }
542  }
543  }
544  else cout << " *** Invalid connection *** " << endl;
545  }
546  else cout << " *** Invalid connection *** " << endl;
547  delete fSocket;
548 
549  delete obj;
550 // }
551 }
552 
553 
printf("******************************************************************** \n")
header file for PadManager.cpp
void DisplayFolderSUM(const char *include="* {TH1*}", const char *exclude="- {TH2*}", const char *drawopt="", int searchdepth=kMaxInt, TObject *topfolder=0x0)
added to the folder menu, it helps to add many spectra and display it in a pad.
void WatchersUtilities(const Char_t *="first")
Default function.
void DisplayFolder(const char *include="* {TH*}", const char *exclude="- {TH2*}", const char *drawopt="clone !wheel", int searchdepth=kMaxInt, TObject *topfolder=0x0)
added to the folder menu, it helps to draw many spectra in a pad.
void SSHPing(UInt_t nbtimes=1, bool dodump=false)
function to check if a watcher connexion is possible with an actor
void Ping(UInt_t nbtimes=5, bool dodump=false)
function to check if a watcher connexion is possible with an actor
void LoadPing(const char *filename, Option_t *opt="+")
Load list of server to ping from a file.
ADF::LogMessage & endl(ADF::LogMessage &log)
void ZeroOnFolder(const char *include="* {TH*}", const char *exclude="- {TH2*}", const char *drawopt="ICE", int searchdepth=kMaxInt, TObject *topfolder=0x0)
Call Reset of Histograms.