EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pmonitor.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file pmonitor.cc
1 #include <Event/testEventiterator.h>
2 #include <Event/fileEventiterator.h>
3 #include <Event/listEventiterator.h>
4 #include <Event/oncsEventiterator.h>
5 #include <Event/rcdaqEventiterator.h>
6 
7 #include <string>
8 #include <iostream>
9 #include <sstream>
10 #include <unistd.h>
11 
12 #include "pmonitor.h"
13 #include "pmonstate.h"
14 //#include "pmondisplay.h"
15 //#include "pmongui.h"
16 
17 #include <TThread.h>
18 #include <TGClient.h>
19 #include <pMutex.h>
20 #include <pthread.h>
21 
22 #ifdef HAVE_FROG
23 #include "FROG.h"
24 #endif
25 
27 //static pmongui *theGui = 0;
28 
31 
33 
34 static TThread *main_thread = 0;
35 //static TThread *display_thread = 0;
36 
37 static pMutex TM;
39 
40 using namespace std;
41 
42 pthread_mutex_t pmonmutex;
43 
44 int pstatus()
45 {
46  cout << theState;
47  if ( theState.streamOpened() )
48  {
49  theIterator->identify();
50  }
51  else
52  cout << endl;
53 
54  return 0;
55 }
56 
58 {
59  return theState.getloopStatus();
60 }
61 
62 
63 //-----------------------------------------
64 
65 int ptestopen()
66 {
67 
68  if ( theState.streamOpened() )
69  {
70  cout << "Stream in already open" << endl;
71  return 1;
72  }
73 
74  theIterator = new testEventiterator();
75  theState.setTestOpened();
76  // if (theGui)
77  // theGui->setStreamLabel(theIterator->getIdTag() );
78  return 0;
79 }
80 
81 //-----------------------------------------
82 
83 int pfileopen(const char * filename)
84 {
85  int status = 0;
86  if ( theState.streamOpened() )
87  {
88  cout << "Stream in already open" << endl;
89  return 1;
90  }
91 
92 #ifdef HAVE_FROG
93  FROG f;
94  theIterator = new fileEventiterator(f.location(filename), status);
95 #else
96  theIterator = new fileEventiterator( filename, status);
97 #endif
98 
99  if (status)
100  {
101  delete theIterator;
102  theIterator = 0;
103  cout << "Could not open file " << filename << endl;
104  return 2;
105  }
106  theState.setFileOpened(filename);
107  // if (theGui)
108  // theGui->setStreamLabel(theIterator->getIdTag() );
109  return 0;
110 }
111 //-----------------------------------------
112 
113 int poncsopen(const char * filename)
114 {
115  int status = 0;
116  if ( theState.streamOpened() )
117  {
118  cout << "Stream in already open" << endl;
119  return 1;
120  }
121  theIterator = new oncsEventiterator(filename, status);
122  if (status)
123  {
124  delete theIterator;
125  theIterator = 0;
126  cout << "Could not open file " << filename << endl;
127  return 2;
128  }
129  theState.setFileOpened(filename);
130  // if (theGui)
131  // theGui->setStreamLabel(theIterator->getIdTag() );
132  return 0;
133 }
134 //-----------------------------------------
135 
136 
137 int rcdaqopen(const char * ip)
138 {
139  int status = 0;
140  if ( theState.streamOpened() )
141  {
142  cout << "Stream in already open" << endl;
143  return 1;
144  }
145 
146  std::string host;
147 
148  if ( ip)
149  {
150  host = ip;
151  }
152  else
153  {
154  if ( getenv("RCDAQHOST") )
155  {
156  host = getenv("RCDAQHOST");
157  }
158  else
159  {
160  host = "localhost";
161  }
162  }
163 
164  theIterator = new rcdaqEventiterator(host.c_str(), status);
165  if (status)
166  {
167  delete theIterator;
168  theIterator = 0;
169  cout << "Could not connect to server " << host.c_str() << endl;
170  return 2;
171  }
172  theState.setRCDAQOpened(host.c_str());
173  //if (theGui)
174  // theGui->setStreamLabel(theIterator->getIdTag() );
175  return 0;
176 }
177 //-----------------------------------------
178 
179 int plistopen(const char * filename)
180 {
181  int status = 0;
182  if ( theState.streamOpened() )
183  {
184  cout << "Stream in already open" << endl;
185  return 1;
186  }
187  theIterator = new listEventiterator(filename, status);
188  if (status)
189  {
190  delete theIterator;
191  theIterator = 0;
192  cout << "Could not open list " << filename << endl;
193  return 2;
194  }
195  theState.setFileOpened(filename);
196  //if (theGui)
197  // theGui->setStreamLabel(theIterator->getIdTag() );
198  return 0;
199 }
200 
201 int pidentify(const int n)
202 {
203 
204  if (n > 0)
205  {
206  theState.setIdentifyFlag(n);
207  return 0;
208  }
209  else if (n == 0)
210  {
211  theState.setIdentifyFlag();
212  return 0;
213  }
214  else
215  {
216  cout << "must be a positive number" << endl;
217  return 1;
218  }
219 }
220 
222 {
223  theState.setIdentifyFlag(1);
224  return 0;
225 }
226 
228 {
229  theState.setIdentifyFlag(0);
230  return 0;
231 }
232 
233 
234 //-----------------------------------------
235 
236 
238 void pprocess (void * ptr)
239 {
240  theState.setloopStatus(0); //clear loop status field
241  int nevents = totalevents;
242  stopcondition = 0;
243 
244  runningTM.Lock();
245 
246  theState.setRunning();
247  theState.clearNoevt();
248  Event *evt;
249  evt = theIterator->getNextEvent();
250  int ncount = 0;
251 
252  // if (theGui)
253  // theGui->setStatusLabel("Running");
254  while (evt )
255  {
256  // if (theGui)
257  // theGui->setEvtnrLabel(evt->getEvtSequence() );
258  if (theState.isIdentifyFlag())
259  evt->identify();
260  theState.incrementNoevt();
261  TM.Lock();
262  int istat = process_event(evt);
263  theState.setloopStatus(istat);
264  delete evt;
265  TM.Release();
266  if (istat || stopcondition)
267  {
268  runningTM.Release();
269  theState.clearRunning();
270  //if (theGui)
271  // theGui->setStatusLabel("Stopped");
272  return ;
273  }
274  if ( ( nevents > 0 && ++ncount >= nevents) )
275  break;
276  evt = theIterator->getNextEvent();
277  }
278  theState.clearRunning();
279  if (!evt)
280  pclose();
281  //if (theGui)
282  // theGui->setEvtnrLabel(0);
283  //if (theGui)
284  // theGui->setStatusLabel("Stopped");
285  runningTM.Release();
286  return ;
287 }
288 
289 
290 //-----------------------------------------
291 
292 int pstart (const int nevents)
293 {
294  if (! theState.streamOpened() )
295  {
296  cout << "No input Stream open" << endl;
297  return 1;
298  }
299 
300  if (theState.isRunning() )
301  {
302  cout << "Already running" << endl;
303  return 1;
304  }
305 
307 
308  if (!main_thread)
309  main_thread = new TThread ( pprocess);
310  main_thread->Run();
311  return 0;
312 }
313 
314 int pstart ()
315 {
316  return pstart(0);
317 }
318 
319 void prun ()
320 {
321  prun(0);
322 }
323 
324 void prun (const int nevents)
325 {
326  if (! theState.streamOpened() )
327  {
328  cout << "No input Stream open" << endl;
329  return ;
330  }
331 
332  stopcondition = 0;
333 
334  theState.setRunning();
335  theState.clearNoevt();
336  Event *evt;
337  evt = theIterator->getNextEvent();
338  int ncount = 0;
339 
340  while (evt )
341  {
342  if (theState.isIdentifyFlag())
343  evt->identify();
344  theState.incrementNoevt();
345  int istat = process_event(evt);
346  theState.setloopStatus(istat);
347  delete evt;
348  if (istat || stopcondition)
349  {
350  theState.clearRunning();
351  return ;
352  }
353  if ( ( nevents > 0 && ++ncount >= nevents) )
354  break;
355  evt = theIterator->getNextEvent();
356  }
357  theState.clearRunning();
358  if (! evt)
359  pclose();
360  return ;
361 }
362 
363 //-----------------------------------------
364 
365 
366 int pstop()
367 {
368  if (! theState.isRunning() )
369  {
370  cout << "Not running" << endl;
371  return 1;
372  }
373 
374  stopcondition = 1;
375  //if (theGui)
376  // theGui->setStatusLabel("Stopped");
377  return 0;
378 }
379 
380 //-----------------------------------------
381 
382 int pclose()
383 {
384 
385  if (! theState.streamOpened() )
386  {
387  cout << "No input Stream open" << endl;
388  return 1;
389  }
390 
391  if ( theState.isRunning() )
392  {
393  cout << "Still running, stop first" << endl;
394  return 1;
395  }
396  delete theIterator;
397  theIterator = 0;
398  theState.clearOpened();
399  //if (theGui)
400  // theGui->setStreamLabel("No stream open");
401 
402  return 0;
403 }
404 
405 int mylock = 0;
406 int plock()
407 {
408  if (!mylock)
409  {
410  pthread_mutex_init(&pmonmutex, NULL);
411  }
412  if (mylock)
413  {
414  cout << "Locked already" << endl;
415  return 0;
416  }
417  pthread_mutex_lock(&pmonmutex);
418  mylock = 1;
419  return 0;
420 }
421 
422 int prelease()
423 {
424  if (!mylock)
425  {
426  cout << "not locked" << endl;
427  return 0;
428  }
429  else
430  {
431  pthread_mutex_unlock(&pmonmutex);
432  }
433  mylock = 0;
434  return 0;
435 }
436 
437 int pwait()
438 {
439  runningTM.Lock();
440  return runningTM.Release();
441 }
442 
443 
444 
445 // int timeinterval;
446 // void pdisplay (void * ptr)
447 // {
448 
449 
450 // char string[40];
451 // TThread::Lock();
452 // pmondisplay *pm = new pmondisplay();
453 // TThread::UnLock();
454 
455 // int seconds = timeinterval;
456 // displaystopcondition = 0;
457 
458 // while ( displaystopcondition == 0 )
459 // {
460 // if (theState.streamOpened() )
461 // {
462 // char *c = string;
463 // for (int i = 0; i++ < 40; *c++ = 0)
464 // ;
465 // ostream *os = new ostringstream(string);
466 // theIterator->identify(*os);
467 // cout << "---" << string << endl;
468 // delete os;
469 // }
470 // else
471 // {
472 // strcpy(string, "none");
473 // }
474 // TThread::Lock();
475 // pm->setStream(string);
476 
477 // pm->setEvtnr(theState.getNoevt());
478 // pm->setStatus(theState.isRunning());
479 // pm->Update();
480 // TThread::UnLock();
481 // sleep(seconds);
482 // }
483 
484 // TThread::Lock();
485 // delete pm;
486 // TThread::UnLock();
487 
488 
489 // return ;
490 // }
491 
492 
493 // //-----------------------------------------
494 
495 // int pcontrol ()
496 // {
497 // pcontrol(10);
498 // return 0;
499 // }
500 
501 // int pcontrol (const int seconds)
502 // {
503 // if (!display_thread)
504 // {
505 // display_thread = new TThread("Display", pdisplay);
506 // }
507 // timeinterval = seconds;
508 // display_thread->Run();
509 // return 0;
510 // }
511 
512 // const char *pname()
513 // {
514 
515 // if (! theState.isRunning() )
516 // {
517 // return " ";
518 // }
519 // return theIterator->getCurrentFileName();
520 
521 // }
522 
523 // int pgui ()
524 // {
525 // if (theGui)
526 // {
527 // return -1;
528 // }
529 // theGui = new pmongui(gClient->GetRoot(), 400, 120 );
530 // return 0;
531 // }
532 
533 // int prmgui ()
534 // {
535 // if (!theGui)
536 // {
537 // return -1;
538 // }
539 // delete theGui;
540 // theGui = 0;
541 // return 0;
542 // }
543 
544 // int phsave (const char *filename)
545 // {
546 
547 // return -1;
548 // }
549 
550 void phelp()
551 {
552 
553  cout << " -- pmonitor commands" << endl;
554  cout << endl;
555  cout << " pstatus() gives a brief status of pmonitor" << endl;
556  cout << " pfileopen(\"filename\") opens a PRDF file" << endl;
557  cout << " plistopen(\"filename\") opens a file with a list of prdfs" << endl;
558  cout << " ptestopen() opens a test input stream" << endl;
559  cout << " pclose() closes the open stream" << endl;
560  cout << " pstart(nevt) starts event loop for nevt events in background (nevt=0 -> all events)" << endl;
561  cout << " prun(nevt) run nevt events (nevt=0 -> all events)" << endl;
562  cout << endl;
563  cout << " pidentify() identify the next event read from the data stream" << endl;
564  cout << " pidentify(5) identify the next 5 events read from the data stream" << endl;
565  cout << " pidentify(0) identify all events read from the data stream (lots of output!)" << endl;
566  cout << " pclearidentify() resets pidentify(0)" << endl;
567  cout << endl;
568 #ifdef HAVE_ROOT6
569  cout << " pupdate(TCanvas *c1, int seconds) starts periodic updates of Canvas c1" << endl;
570  cout << " pendupdate(TCanvas *c1) stop updating Canvas c1" << endl;
571  cout << " pendupdate() stop updating all Canvases that are updating" << endl;
572  cout << endl;
573 #endif
574  // cout << " phsave(\"filename\") saves all histograms made the histogram factory to the file" << endl;
575  cout << "--" << endl;
576 
577 }
578 
579 
580 unsigned int stop_display = 0;
581 
583 {
584  TVirtualPad *thePad;
585  unsigned int refreshinterval;
586 };
587 
589 
590 static TThread *mon_thread = 0;
591 
592 unsigned int refreshinterval = 5;
593 
594 // if stop update is true, we look for the thread that updates pad_to_stop
595 // if that is 0, we stop all threads
596 unsigned int stop_update = 0;
597 TVirtualPad * pad_to_stop = 0;
598 
599 void update_process (void * ptr)
600 {
601 
602  // our argument
603  threadargument *ta = ( threadargument *) ptr;
604 
605  TVirtualPad *myPad = ta->thePad;
606  unsigned int my_refreshinterval = ta->refreshinterval;
607 
608  // and done with it.
609  delete ta;
610 
611  time_t last_time = time(0) + my_refreshinterval + 1; //force an update on start
612 
613 
614  while ( 1)
615  {
616  if ( stop_update)
617  {
618  if ( pad_to_stop == 0)
619  {
620  break;
621  }
622  else
623  {
624  if (pad_to_stop == myPad) break;
625  }
626  }
627 
628  time_t x = time(0);
629  if ( x - last_time > my_refreshinterval)
630  {
631  last_time = x;
632 
633  updatePad(myPad);
634  sleep(1);
635  }
636  }
637 
638 }
639 
640 
641 void pupdate(TVirtualPad * pad, const unsigned int refresh)
642 {
643 
644 #ifdef HAVE_ROOT6
645  // this structure is getting deleted in the thread after
646  // we got everytthing off it.
647  threadargument *ta = new threadargument;
648 
649  // parameters for the thread - the pad, and the waitinterval
650  ta->thePad = pad;
651  ta->refreshinterval = refresh;
652 
653  stop_update = 0;
654  pad_to_stop = 0;
655  mon_thread = new TThread ( update_process, ta);
656  mon_thread->Run();
657 #else
658  cout << "this is implemented in root v6 only" << endl;
659 #endif
660 
661 }
662 
663 void pendupdate(TVirtualPad * pad)
664 {
665 #ifdef HAVE_ROOT6
666  stop_update = 1;
667  pad_to_stop = pad;
668 #else
669  cout << "this is implemented in root v6 only" << endl;
670 #endif
671 
672 }
673 
674 void updatePad( TVirtualPad *myPad)
675 {
676 
677  int i = 0;
678 
679  TVirtualPad *s = 0;
680  while ( ! ( s == myPad->cd(i)) )
681  {
682  // cout << " updating " << i << endl;
683  s= myPad->cd(i);
684  i++;
685  s->Modified();
686  s->Update();
687  }
688 }