File indexing completed on 2025-08-03 08:20:53
0001 #include <Event/testEventiterator.h>
0002 #include <Event/fileEventiterator.h>
0003 #include <Event/listEventiterator.h>
0004 #include <Event/oncsEventiterator.h>
0005 #include <Event/rcdaqEventiterator.h>
0006
0007 #include <string>
0008 #include <iostream>
0009 #include <sstream>
0010 #include <unistd.h>
0011
0012 #include "pmonitor.h"
0013 #include "pmonstate.h"
0014
0015
0016
0017 #include <TThread.h>
0018 #include <TGClient.h>
0019 #include <pMutex.h>
0020 #include <pthread.h>
0021
0022 #ifdef HAVE_FROG
0023 #include "FROG.h"
0024 #endif
0025
0026 static Eventiterator *theIterator = 0;
0027
0028
0029 int stopcondition;
0030 int displaystopcondition;
0031
0032 static pmonstate theState;
0033
0034 static TThread *main_thread = 0;
0035
0036
0037 static pMutex TM;
0038 static pMutex runningTM;
0039
0040 using namespace std;
0041
0042 pthread_mutex_t pmonmutex;
0043
0044 int pstatus()
0045 {
0046 cout << theState;
0047 if ( theState.streamOpened() )
0048 {
0049 theIterator->identify();
0050 }
0051 else
0052 cout << endl;
0053
0054 return 0;
0055 }
0056
0057 int pexitstatus()
0058 {
0059 return theState.getloopStatus();
0060 }
0061
0062
0063
0064
0065 int ptestopen()
0066 {
0067
0068 if ( theState.streamOpened() )
0069 {
0070 cout << "Stream in already open" << endl;
0071 return 1;
0072 }
0073
0074 theIterator = new testEventiterator();
0075 theState.setTestOpened();
0076
0077
0078 return 0;
0079 }
0080
0081
0082
0083 int pfileopen(const char * filename)
0084 {
0085 int status = 0;
0086 if ( theState.streamOpened() )
0087 {
0088 cout << "Stream in already open" << endl;
0089 return 1;
0090 }
0091
0092 #ifdef HAVE_FROG
0093 FROG f;
0094 theIterator = new fileEventiterator(f.location(filename), status);
0095 #else
0096 theIterator = new fileEventiterator( filename, status);
0097 #endif
0098
0099 if (status)
0100 {
0101 delete theIterator;
0102 theIterator = 0;
0103 cout << "Could not open file " << filename << endl;
0104 return 2;
0105 }
0106 theState.setFileOpened(filename);
0107
0108
0109 return 0;
0110 }
0111
0112
0113 int poncsopen(const char * filename)
0114 {
0115 int status = 0;
0116 if ( theState.streamOpened() )
0117 {
0118 cout << "Stream in already open" << endl;
0119 return 1;
0120 }
0121 theIterator = new oncsEventiterator(filename, status);
0122 if (status)
0123 {
0124 delete theIterator;
0125 theIterator = 0;
0126 cout << "Could not open file " << filename << endl;
0127 return 2;
0128 }
0129 theState.setFileOpened(filename);
0130
0131
0132 return 0;
0133 }
0134
0135
0136
0137 int rcdaqopen(const char * ip)
0138 {
0139 int status = 0;
0140 if ( theState.streamOpened() )
0141 {
0142 cout << "Stream in already open" << endl;
0143 return 1;
0144 }
0145
0146 std::string host;
0147
0148 if ( ip)
0149 {
0150 host = ip;
0151 }
0152 else
0153 {
0154 if ( getenv("RCDAQHOST") )
0155 {
0156 host = getenv("RCDAQHOST");
0157 }
0158 else
0159 {
0160 host = "localhost";
0161 }
0162 }
0163
0164 theIterator = new rcdaqEventiterator(host.c_str(), status);
0165 if (status)
0166 {
0167 delete theIterator;
0168 theIterator = 0;
0169 cout << "Could not connect to server " << host.c_str() << endl;
0170 return 2;
0171 }
0172 theState.setRCDAQOpened(host.c_str());
0173
0174
0175 return 0;
0176 }
0177
0178
0179 int plistopen(const char * filename)
0180 {
0181 int status = 0;
0182 if ( theState.streamOpened() )
0183 {
0184 cout << "Stream in already open" << endl;
0185 return 1;
0186 }
0187 theIterator = new listEventiterator(filename, status);
0188 if (status)
0189 {
0190 delete theIterator;
0191 theIterator = 0;
0192 cout << "Could not open list " << filename << endl;
0193 return 2;
0194 }
0195 theState.setFileOpened(filename);
0196
0197
0198 return 0;
0199 }
0200
0201 int pidentify(const int n)
0202 {
0203
0204 if (n > 0)
0205 {
0206 theState.setIdentifyFlag(n);
0207 return 0;
0208 }
0209 else if (n == 0)
0210 {
0211 theState.setIdentifyFlag();
0212 return 0;
0213 }
0214 else
0215 {
0216 cout << "must be a positive number" << endl;
0217 return 1;
0218 }
0219 }
0220
0221 int pidentify()
0222 {
0223 theState.setIdentifyFlag(1);
0224 return 0;
0225 }
0226
0227 int pclearidentify()
0228 {
0229 theState.setIdentifyFlag(0);
0230 return 0;
0231 }
0232
0233
0234
0235
0236
0237 int totalevents;
0238 void pprocess (void * ptr)
0239 {
0240 theState.setloopStatus(0);
0241 int nevents = totalevents;
0242 stopcondition = 0;
0243
0244 runningTM.Lock();
0245
0246 theState.setRunning();
0247 theState.clearNoevt();
0248 Event *evt;
0249 evt = theIterator->getNextEvent();
0250 int ncount = 0;
0251
0252
0253
0254 while (evt )
0255 {
0256
0257
0258 if (theState.isIdentifyFlag())
0259 evt->identify();
0260 theState.incrementNoevt();
0261 TM.Lock();
0262 int istat = process_event(evt);
0263 theState.setloopStatus(istat);
0264 delete evt;
0265 TM.Release();
0266 if (istat || stopcondition)
0267 {
0268 runningTM.Release();
0269 theState.clearRunning();
0270
0271
0272 return ;
0273 }
0274 if ( ( nevents > 0 && ++ncount >= nevents) )
0275 break;
0276 evt = theIterator->getNextEvent();
0277 }
0278 theState.clearRunning();
0279 if (!evt)
0280 pclose();
0281
0282
0283
0284
0285 runningTM.Release();
0286 return ;
0287 }
0288
0289
0290
0291
0292 int pstart (const int nevents)
0293 {
0294 if (! theState.streamOpened() )
0295 {
0296 cout << "No input Stream open" << endl;
0297 return 1;
0298 }
0299
0300 if (theState.isRunning() )
0301 {
0302 cout << "Already running" << endl;
0303 return 1;
0304 }
0305
0306 totalevents = nevents;
0307
0308 if (!main_thread)
0309 main_thread = new TThread ( pprocess);
0310 main_thread->Run();
0311 return 0;
0312 }
0313
0314 int pstart ()
0315 {
0316 return pstart(0);
0317 }
0318
0319 void prun ()
0320 {
0321 prun(0);
0322 }
0323
0324 void prun (const int nevents)
0325 {
0326 if (! theState.streamOpened() )
0327 {
0328 cout << "No input Stream open" << endl;
0329 return ;
0330 }
0331
0332 stopcondition = 0;
0333
0334 theState.setRunning();
0335 theState.clearNoevt();
0336 Event *evt;
0337 evt = theIterator->getNextEvent();
0338 int ncount = 0;
0339
0340 while (evt )
0341 {
0342 if (theState.isIdentifyFlag())
0343 evt->identify();
0344 theState.incrementNoevt();
0345 int istat = process_event(evt);
0346 theState.setloopStatus(istat);
0347 delete evt;
0348 if (istat || stopcondition)
0349 {
0350 theState.clearRunning();
0351 return ;
0352 }
0353 if ( ( nevents > 0 && ++ncount >= nevents) )
0354 break;
0355 evt = theIterator->getNextEvent();
0356 }
0357 theState.clearRunning();
0358 if (! evt)
0359 pclose();
0360 return ;
0361 }
0362
0363
0364
0365
0366 int pstop()
0367 {
0368 if (! theState.isRunning() )
0369 {
0370 cout << "Not running" << endl;
0371 return 1;
0372 }
0373
0374 stopcondition = 1;
0375
0376
0377 return 0;
0378 }
0379
0380
0381
0382 int pclose()
0383 {
0384
0385 if (! theState.streamOpened() )
0386 {
0387 cout << "No input Stream open" << endl;
0388 return 1;
0389 }
0390
0391 if ( theState.isRunning() )
0392 {
0393 cout << "Still running, stop first" << endl;
0394 return 1;
0395 }
0396 delete theIterator;
0397 theIterator = 0;
0398 theState.clearOpened();
0399
0400
0401
0402 return 0;
0403 }
0404
0405 int mylock = 0;
0406 int plock()
0407 {
0408 if (!mylock)
0409 {
0410 pthread_mutex_init(&pmonmutex, NULL);
0411 }
0412 if (mylock)
0413 {
0414 cout << "Locked already" << endl;
0415 return 0;
0416 }
0417 pthread_mutex_lock(&pmonmutex);
0418 mylock = 1;
0419 return 0;
0420 }
0421
0422 int prelease()
0423 {
0424 if (!mylock)
0425 {
0426 cout << "not locked" << endl;
0427 return 0;
0428 }
0429 else
0430 {
0431 pthread_mutex_unlock(&pmonmutex);
0432 }
0433 mylock = 0;
0434 return 0;
0435 }
0436
0437 int pwait()
0438 {
0439 runningTM.Lock();
0440 return runningTM.Release();
0441 }
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550 void phelp()
0551 {
0552
0553 cout << " -- pmonitor commands" << endl;
0554 cout << endl;
0555 cout << " pstatus() gives a brief status of pmonitor" << endl;
0556 cout << " pfileopen(\"filename\") opens a PRDF file" << endl;
0557 cout << " plistopen(\"filename\") opens a file with a list of prdfs" << endl;
0558 cout << " ptestopen() opens a test input stream" << endl;
0559 cout << " pclose() closes the open stream" << endl;
0560 cout << " pstart(nevt) starts event loop for nevt events in background (nevt=0 -> all events)" << endl;
0561 cout << " prun(nevt) run nevt events (nevt=0 -> all events)" << endl;
0562 cout << endl;
0563 cout << " pidentify() identify the next event read from the data stream" << endl;
0564 cout << " pidentify(5) identify the next 5 events read from the data stream" << endl;
0565 cout << " pidentify(0) identify all events read from the data stream (lots of output!)" << endl;
0566 cout << " pclearidentify() resets pidentify(0)" << endl;
0567 cout << endl;
0568 #ifdef HAVE_ROOT6
0569 cout << " pupdate(TCanvas *c1, int seconds) starts periodic updates of Canvas c1" << endl;
0570 cout << " pendupdate(TCanvas *c1) stop updating Canvas c1" << endl;
0571 cout << " pendupdate() stop updating all Canvases that are updating" << endl;
0572 cout << endl;
0573 #endif
0574
0575 cout << "--" << endl;
0576
0577 }
0578
0579
0580 unsigned int stop_display = 0;
0581
0582 struct threadargument
0583 {
0584 TVirtualPad *thePad;
0585 unsigned int refreshinterval;
0586 };
0587
0588 pMutex mondisplay_started;
0589
0590 static TThread *mon_thread = 0;
0591
0592 unsigned int refreshinterval = 5;
0593
0594
0595
0596 unsigned int stop_update = 0;
0597 TVirtualPad * pad_to_stop = 0;
0598
0599 void update_process (void * ptr)
0600 {
0601
0602
0603 threadargument *ta = ( threadargument *) ptr;
0604
0605 TVirtualPad *myPad = ta->thePad;
0606 unsigned int my_refreshinterval = ta->refreshinterval;
0607
0608
0609 delete ta;
0610
0611 time_t last_time = time(0) + my_refreshinterval + 1;
0612
0613
0614 while ( 1)
0615 {
0616 if ( stop_update)
0617 {
0618 if ( pad_to_stop == 0)
0619 {
0620 break;
0621 }
0622 else
0623 {
0624 if (pad_to_stop == myPad) break;
0625 }
0626 }
0627
0628 time_t x = time(0);
0629 if ( x - last_time > my_refreshinterval)
0630 {
0631 last_time = x;
0632
0633 updatePad(myPad);
0634 sleep(1);
0635 }
0636 }
0637
0638 }
0639
0640
0641 void pupdate(TVirtualPad * pad, const unsigned int refresh)
0642 {
0643
0644 #ifdef HAVE_ROOT6
0645
0646
0647 threadargument *ta = new threadargument;
0648
0649
0650 ta->thePad = pad;
0651 ta->refreshinterval = refresh;
0652
0653 stop_update = 0;
0654 pad_to_stop = 0;
0655 mon_thread = new TThread ( update_process, ta);
0656 mon_thread->Run();
0657 #else
0658 cout << "this is implemented in root v6 only" << endl;
0659 #endif
0660
0661 }
0662
0663 void pendupdate(TVirtualPad * pad)
0664 {
0665 #ifdef HAVE_ROOT6
0666 stop_update = 1;
0667 pad_to_stop = pad;
0668 #else
0669 cout << "this is implemented in root v6 only" << endl;
0670 #endif
0671
0672 }
0673
0674 void updatePad( TVirtualPad *myPad)
0675 {
0676
0677 int i = 0;
0678
0679 TVirtualPad *s = 0;
0680 while ( ! ( s == myPad->cd(i)) )
0681 {
0682
0683 s= myPad->cd(i);
0684 i++;
0685 s->Modified();
0686 s->Update();
0687 }
0688 }