Back to home page

sPhenix code displayed by LXR

 
 

    


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 //#include "pmondisplay.h"
0015 //#include "pmongui.h"
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 //static pmongui *theGui = 0;
0028 
0029 int stopcondition;
0030 int displaystopcondition;
0031 
0032 static pmonstate theState;
0033 
0034 static TThread *main_thread = 0;
0035 //static TThread *display_thread = 0;
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   //  if (theGui)
0077   //   theGui->setStreamLabel(theIterator->getIdTag() );
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   //  if (theGui)
0108   //  theGui->setStreamLabel(theIterator->getIdTag() );
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   //  if (theGui)
0131   //  theGui->setStreamLabel(theIterator->getIdTag() );
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   //if (theGui)
0174   //  theGui->setStreamLabel(theIterator->getIdTag() );
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   //if (theGui)
0197   //  theGui->setStreamLabel(theIterator->getIdTag() );
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); //clear loop status field
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   // if (theGui)
0253   //  theGui->setStatusLabel("Running");
0254   while (evt )
0255     {
0256       // if (theGui)
0257       //  theGui->setEvtnrLabel(evt->getEvtSequence() );
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           //if (theGui)
0271           //  theGui->setStatusLabel("Stopped");
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   //if (theGui)
0282   //  theGui->setEvtnrLabel(0);
0283   //if (theGui)
0284   //  theGui->setStatusLabel("Stopped");
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   //if (theGui)
0376   //  theGui->setStatusLabel("Stopped");
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   //if (theGui)
0400   //  theGui->setStreamLabel("No stream open");
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 // int timeinterval;
0446 // void pdisplay (void * ptr)
0447 // {
0448 
0449 
0450 //   char string[40];
0451 //   TThread::Lock();
0452 //   pmondisplay *pm = new pmondisplay();
0453 //   TThread::UnLock();
0454 
0455 //   int seconds = timeinterval;
0456 //   displaystopcondition = 0;
0457 
0458 //   while ( displaystopcondition == 0 )
0459 //     {
0460 //       if (theState.streamOpened() )
0461 //         {
0462 //           char *c = string;
0463 //           for (int i = 0; i++ < 40; *c++ = 0)
0464 //             ;
0465 //           ostream *os = new ostringstream(string);
0466 //           theIterator->identify(*os);
0467 //           cout << "---" << string << endl;
0468 //           delete os;
0469 //         }
0470 //       else
0471 //         {
0472 //           strcpy(string, "none");
0473 //         }
0474 //       TThread::Lock();
0475 //       pm->setStream(string);
0476 
0477 //       pm->setEvtnr(theState.getNoevt());
0478 //       pm->setStatus(theState.isRunning());
0479 //       pm->Update();
0480 //       TThread::UnLock();
0481 //       sleep(seconds);
0482 //     }
0483 
0484 //   TThread::Lock();
0485 //   delete pm;
0486 //   TThread::UnLock();
0487 
0488 
0489 //   return ;
0490 // }
0491 
0492 
0493 // //-----------------------------------------
0494 
0495 // int pcontrol ()
0496 // {
0497 //   pcontrol(10);
0498 //   return 0;
0499 // }
0500 
0501 // int pcontrol (const int seconds)
0502 // {
0503 //   if (!display_thread)
0504 //     {
0505 //       display_thread = new TThread("Display", pdisplay);
0506 //     }
0507 //   timeinterval = seconds;
0508 //   display_thread->Run();
0509 //   return 0;
0510 // }
0511 
0512 // const char *pname()
0513 // {
0514 
0515 //   if (! theState.isRunning() )
0516 //     {
0517 //       return " ";
0518 //     }
0519 //   return theIterator->getCurrentFileName();
0520 
0521 // }
0522 
0523 // int pgui ()
0524 // {
0525 //   if (theGui)
0526 //     {
0527 //       return -1;
0528 //     }
0529 //   theGui = new pmongui(gClient->GetRoot(), 400, 120 );
0530 //   return 0;
0531 // }
0532 
0533 // int prmgui ()
0534 // {
0535 //   if (!theGui)
0536 //     {
0537 //       return -1;
0538 //     }
0539 //   delete theGui;
0540 //   theGui = 0;
0541 //   return 0;
0542 // }
0543 
0544 // int phsave (const char *filename)
0545 // {
0546 
0547 //       return -1;
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   //  cout << " phsave(\"filename\")        saves all histograms made the histogram factory to the file" << endl;
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 // if stop update is true, we look for the thread that updates pad_to_stop
0595 // if that is 0, we stop all threads
0596 unsigned int stop_update = 0;
0597 TVirtualPad * pad_to_stop = 0;
0598 
0599 void update_process (void * ptr)
0600 {
0601 
0602   // our argument
0603   threadargument *ta = ( threadargument *) ptr; 
0604 
0605   TVirtualPad *myPad = ta->thePad;
0606   unsigned int my_refreshinterval = ta->refreshinterval;
0607 
0608   // and done with it.
0609   delete ta;
0610 
0611   time_t last_time = time(0) + my_refreshinterval + 1;  //force an update on start
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   // this structure is getting deleted in the thread after
0646   // we got everytthing off it. 
0647   threadargument *ta = new threadargument;
0648   
0649   // parameters for the thread - the pad, and the waitinterval
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       //      cout << " updating " << i << endl;
0683       s= myPad->cd(i);
0684       i++;
0685       s->Modified();
0686       s->Update();
0687     }
0688 }