Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:33

0001 #include <stdlib.h>
0002 #include <signal.h>
0003 #include <string>
0004 
0005 #include "fileEventiterator.h"
0006 #include "testEventiterator.h"
0007 #include "rcdaqEventiterator.h"
0008 #include "oncsEventiterator.h"
0009 #include <stdio.h>
0010 
0011 #ifdef HAVE_GETOPT_H
0012 #include "getopt.h"
0013 #endif
0014 
0015 #include<vector>
0016 #include <boost/algorithm/string.hpp>
0017 #include <boost/lexical_cast.hpp>
0018 
0019 
0020 
0021 #define RCDAQEVENTITERATOR 1
0022 #define FILEEVENTITERATOR 2
0023 #define TESTEVENTITERATOR 3
0024 #define ONCSEVENTITERATOR 4
0025 
0026 #if defined(SunOS) || defined(Linux) || defined(OSF1)
0027 void sig_handler(int);
0028 #else
0029 void sig_handler(...);
0030 #endif
0031 
0032 // we make it a global variable so the signal; handler can get at it.
0033 
0034 Eventiterator *it;
0035 
0036 void exitmsg()
0037 {
0038   COUT << "** usage: ddump -ecnstdfghiIFTOv datastream" << std::endl;
0039   COUT << "    type  ddump -h   for more help" << std::endl;
0040   exit(0);
0041 }
0042 
0043 void evtcountexitmsg()
0044 {
0045   COUT << "** cannot specify both -e and -c!" << std::endl;
0046   COUT << "    type  dlist -h  for more help" << std::endl;
0047   exit(0);
0048 }
0049 
0050 
0051 void exithelp()
0052 {
0053 
0054   COUT << std::endl;
0055   COUT << "   ddump dumps selected packets from a given event. The event can come" << std::endl;
0056   COUT << "   from any of the standard sources, ET pool, file, or test stream. " << std::endl;
0057   COUT << "   The default is to get the next available event from a ET pool." << std::endl;
0058   COUT << std::endl;
0059   COUT << "   ddump can optionally identify the event with the  -i option." << std::endl;
0060   COUT << "   You can request a certain event number with the -e option. " << std::endl;
0061   COUT << "   You can ask for a certain event type (data, beg-run, etc) with -t." << std::endl;
0062   COUT << "     -t takes a number (e.g. 9 for begin-run) for an exact match," << std::endl;
0063   COUT << "     or DATA or SPECIAL to selct data or special events (DATA is default)" << std::endl;
0064   COUT << "     you can abbreviate DATA to D or d and SPECIAL to S or s." << std::endl;
0065 
0066   COUT << std::endl;
0067   COUT << "   Note: In order to find out which packets are contained in an event, you can" << std::endl;
0068   COUT << "   use the dlist utility." << std::endl;
0069   COUT << std::endl;
0070   COUT << "   Example:" << std::endl;
0071   COUT << std::endl;
0072   COUT << "   -T says that the input stream is a test stream (where events have 3 " << std::endl;
0073   COUT << "   predictable packets 1001, 1002, and 1003); we dump packet 1002:" << std::endl;
0074   COUT << std::endl;
0075   COUT << "   > ddump -p 1002 -T" << std::endl;
0076   COUT << "   Packet  1002    16  0 (Unformatted)  30005 (ID2EVT)" << std::endl;
0077   COUT << "       0 |     1    2    3    4    5    6    7    8 " << std::endl;
0078   COUT << "       8 |     9    a    b    c    d    e    f   10 " << std::endl;
0079   COUT << "      10 |    11   12   13   14 " << std::endl;
0080   COUT << std::endl;
0081   COUT << "   The -i option also lets the event identify itself." << std::endl;
0082   COUT << "   The -I option gets you an in-depth identification of the packets." << std::endl;
0083 
0084   COUT << std::endl;
0085   COUT << "   > ddump -p 1002 -T -i" << std::endl;
0086   COUT << "    -- Event     1 Run:  1331 length:    68 frames:   1 type:  1 (Data Event)" << std::endl;
0087   COUT << "   Packet  1002    16  0 (Unformatted)  30005 (ID2EVT)" << std::endl;
0088   COUT << "       0 |     1    2    3    4    5    6    7    8 " << std::endl;
0089   COUT << "       8 |     9    a    b    c    d    e    f   10 " << std::endl;
0090   COUT << "      10 |    11   12   13   14 " << std::endl;
0091 
0092   COUT << " The -F option also causes the frames in the event to be listed." << std::endl;
0093   COUT << " If you list a particular packet, you will get the frame where" << std::endl; 
0094   COUT << " this packet is in." << std::endl; 
0095 
0096   COUT << std::endl;
0097 
0098   COUT << "  The -p option takes a number, a list, or a range of packets" << std::endl;
0099   COUT << "  You can combine ranges and lists:" << std::endl;
0100   COUT << "   > ddump -p 1001 -T -i" << std::endl;
0101   COUT << "   > ddump -p 1001,1002 -T -i" << std::endl;
0102   COUT << "   > ddump -p 1001-1003 -T -i" << std::endl;
0103   COUT << "   > ddump -p 1001-1002,1003 -T -i" << std::endl;
0104 
0105   COUT << std::endl;
0106 
0107   COUT << "  List of options: " << std::endl;
0108   COUT << " -e <event number>" << std::endl;
0109   COUT << " -c <number> get nth event (-e gives event with number n)" << std::endl;
0110   COUT << " -n <number> repeat for n events (0: until end of stream)" << std::endl;
0111   COUT << " -p <Packet Id>" << std::endl;
0112   COUT << " -t <event type>" << std::endl;
0113   COUT << " -i <print event identity>" << std::endl;
0114   COUT << " -I <print in-depth packet identity (default is short form)>" << std::endl;
0115   COUT << " -f (stream is a file)" << std::endl;
0116   COUT << " -T (stream is a test stream)" << std::endl;
0117   COUT << " -r (stream is a rcdaq monitoring stream)" << std::endl;
0118   COUT << " -O (stream is a legacy ONCS format file)" << std::endl;
0119   COUT << " -g use generic dump" << std::endl;
0120   COUT << " -d numbers are std::decimal (default std::hex) for generic dump" << std::endl;
0121   COUT << " -o numbers are octal (default std::hex) for generic dump" << std::endl;
0122   COUT << " -s for a generic dump, send packet raw payloadc to stdout for further manipulation" << std::endl;
0123   COUT << " -x like -s, but also include the packet header" << std::endl;
0124   COUT << " -v verbose" << std::endl;
0125   COUT << " -h this message" << std::endl << std::endl;
0126   exit(0);
0127 }
0128 
0129 #if defined(SunOS) || defined(Linux) || defined(OSF1)
0130 void sig_handler(int i)
0131 #else
0132   void sig_handler(...)
0133 #endif
0134 {
0135   COUT << "sig_handler: signal seen " << std::endl;
0136   if (it) delete it;
0137   exit(0);
0138 }
0139 
0140 int rangeParser ( const std::string string, std::vector<int> &selection)
0141 {
0142   std::vector<std::string>::const_iterator it, itr;
0143   std::vector<std::string> strs,r;
0144 
0145 //  std::vector<int>::const_iterator vit;
0146   int low,high,i;
0147   boost::split(strs,string, boost::is_any_of(","));
0148 
0149   for (it= strs.begin(); it!= strs.end(); ++it)
0150     {
0151       boost::split(r,*it,boost::is_any_of("-"));
0152 
0153       itr = r.begin();
0154       low = high =boost::lexical_cast<int>(r[0]);
0155       itr++;
0156       if(itr!=r.end())
0157     {
0158       high = boost::lexical_cast<int>(r[1]);
0159     }
0160       for(i=low;i<=high;++i)
0161     {
0162       selection.push_back(i);
0163     }
0164     }
0165 
0166   //  for(vit= selection.begin(); vit!= selection.end(); ++vit)
0167   //  {
0168   //    std::cout<<*vit<<std::endl;
0169   //  }
0170   return 0;
0171 
0172 }
0173 
0174 int 
0175 main(int argc, char *argv[])
0176 {
0177 
0178   if (argc < 2) exitmsg();
0179 
0180   int c;
0181   int status;
0182 
0183   int eventnumber =0;
0184   int countnumber =0;
0185   int repeatcount =1;
0186   int subeventid =0;
0187 
0188   int eventtypemin = 1;  
0189   int eventtypemax = 7;  
0190   int eventtype = 1;
0191   int type_is_a_range = 1;
0192   int verbose = 0;
0193 
0194 
0195   int ittype = FILEEVENTITERATOR;
0196   int dumpstyle = EVT_HEXADECIMAL;
0197   int identify = 0;
0198   int fullidentify = 0;
0199   int listframe = 0;
0200   int listHistory = 0;
0201   int listError = 0;
0202   int generic = 0;
0203 
0204   extern char *optarg;
0205   extern int optind;
0206 
0207   std::vector<int> packetSelection;
0208 
0209   while ((c = getopt(argc, argv, "n:c:e:p:t:idsxfrghIFTOHEv")) != EOF)
0210     switch (c) 
0211       {
0212       case 'e':
0213     if ( !sscanf(optarg, "%d", &eventnumber) ) exitmsg();
0214     break;
0215 
0216       case 'c':
0217     if ( !sscanf(optarg, "%d", &countnumber) ) exitmsg();
0218     break;
0219 
0220       case 'n':
0221     if ( !sscanf(optarg, "%d", &repeatcount) ) exitmsg();
0222     break;
0223 
0224       case 'p':
0225     //  if ( !sscanf(optarg, "%d", &subeventid) ) exitmsg();
0226     if ( rangeParser ( optarg, packetSelection) ) exitmsg();
0227     subeventid=1;  // yes, select
0228     break;
0229 
0230       case 't':
0231     if (*optarg == 'S' || *optarg == 's' )
0232       {
0233         type_is_a_range =1;
0234         eventtypemin=8;
0235         eventtypemax=20;
0236       }
0237     else if (*optarg == 'A' || *optarg == 'a')
0238       {
0239         type_is_a_range =1;
0240         eventtypemin=1;
0241         eventtypemax=20;
0242       }
0243     else if (*optarg == 'D' || *optarg == 'd')
0244       {
0245         type_is_a_range =1;
0246         eventtypemin=1;
0247         eventtypemax=7;
0248       }
0249     else
0250       {  
0251         if ( !sscanf(optarg, "%d", &eventtype) ) exitmsg();
0252         type_is_a_range =0;
0253       }
0254     break;
0255 
0256       case 'i':
0257     identify = 1;
0258     break;
0259 
0260       case 'I':
0261     fullidentify = 1;
0262     break;
0263 
0264       case 'F':
0265     listframe = 1;
0266     break;
0267 
0268       case 'H':
0269     listHistory = 1;
0270     break;
0271 
0272       case 'E':
0273     listError = 1;
0274     break;
0275 
0276       case 'g':
0277     generic = 1;
0278     break;
0279 
0280       case 'T':
0281     ittype = TESTEVENTITERATOR;
0282     break;
0283 
0284       case 'f':
0285     ittype = FILEEVENTITERATOR;
0286     break;
0287 
0288       case 'r':
0289     ittype = RCDAQEVENTITERATOR;
0290     break;
0291 
0292       case 'O':
0293     ittype = ONCSEVENTITERATOR;
0294     break;
0295 
0296       case 'o':
0297     dumpstyle = EVT_OCTAL;
0298     break;
0299 
0300       case 'd':
0301     dumpstyle = EVT_DECIMAL;
0302     break;
0303 
0304       case 's':
0305     dumpstyle = EVT_RAW;
0306     break;
0307 
0308       case 'x':
0309     dumpstyle = EVT_RAW_WH;
0310     break;
0311 
0312       case 'v':   // verbose
0313     verbose++;
0314     break;
0315 
0316       case 'h':
0317     exithelp();
0318     break;
0319       }
0320 
0321   
0322 
0323   if ( eventnumber && countnumber) evtcountexitmsg();
0324 
0325   signal(SIGKILL, sig_handler);
0326   signal(SIGTERM, sig_handler);
0327   signal(SIGINT,  sig_handler);
0328 
0329   // see if we can open the file
0330   it = 0;
0331   status = 0;
0332   
0333   switch (ittype)
0334     {
0335     case RCDAQEVENTITERATOR:
0336       if ( optind+1>argc) 
0337     {
0338       std::string host = "localhost";
0339     
0340       if ( getenv("RCDAQHOST")  )
0341         {
0342           host = getenv("RCDAQHOST");
0343         }
0344       
0345       it = new rcdaqEventiterator(host.c_str(), status);
0346 
0347     }
0348       else
0349     {
0350       it = new rcdaqEventiterator(argv[optind], status);
0351     }
0352       break;
0353 
0354     case  TESTEVENTITERATOR:
0355       it = new testEventiterator();
0356       status =0;
0357       break;
0358 
0359     case  FILEEVENTITERATOR:
0360       if ( optind+1>argc) exitmsg();
0361       it = new fileEventiterator(argv[optind], status);
0362       break;
0363      
0364     case  ONCSEVENTITERATOR:
0365       if ( optind+1>argc) exitmsg();
0366       it = new oncsEventiterator(argv[optind], status);
0367       break;
0368 
0369       status = 1;
0370       break;
0371       
0372     default:
0373       exitmsg();
0374       break;
0375     }
0376 
0377   if (status)
0378     {
0379       delete it;
0380       it = 0;
0381       COUT << "Could not open input stream" << std::endl;
0382       exit(1);
0383     }
0384 
0385   // set the verbosity of the iterator
0386   it->setVerbosity(verbose);
0387 
0388   // ok. now go through the events
0389   Event *evt;
0390   Packet *s;
0391   int i;
0392 
0393   evt = it->getNextEvent();
0394 
0395   // first check if our requested event number is already past:
0396 
0397   //  if ( eventnumber && evt->getEvtSequence() > eventnumber)
0398   // {
0399   //   COUT << "requested Event number " << eventnumber<< 
0400   //    " but found " <<  evt->getEvtSequence() << " already" << std::endl;
0401   //   delete evt;
0402   //   evt = 0;
0403   // }
0404 
0405   int take_this;
0406   int count = 0;
0407   int accepted_count = 0;
0408 
0409   while (evt)    // as long as there is a next event...
0410     {
0411       take_this = 1;
0412       count++;
0413 
0414       if ( eventnumber )
0415     {
0416       if ( evt->getEvtSequence() == eventnumber)
0417         eventnumber = 0;
0418       else
0419         take_this = 0;
0420     }
0421 
0422 
0423       if ( countnumber && count < countnumber)
0424     take_this = 0;
0425 
0426       if ( type_is_a_range)
0427     {
0428       if ( (evt->getEvtType() < eventtypemin) || 
0429            (evt->getEvtType() > eventtypemax) 
0430            ) take_this = 0;
0431     }
0432       else
0433     {
0434       if (evt->getEvtType() != eventtype) take_this = 0;
0435     }
0436     
0437       if (take_this)
0438     {
0439       if (identify) evt->identify();
0440 
0441       if ( subeventid)
0442         {
0443           std::vector<int>::const_iterator vit;
0444           for (vit= packetSelection.begin(); vit!= packetSelection.end(); ++vit)
0445         {
0446           if ( (s = evt->getPacket(*vit)) )  // get the subevent
0447             {
0448               if (listframe)  evt->listFrame (*vit);
0449               if (fullidentify) s->fullIdentify();
0450               if (generic) s->gdump(dumpstyle);
0451               else s->dump();
0452               if (listHistory) evt->listHistory();
0453               if (listError) evt->listError();
0454               delete s;
0455             }
0456         }
0457         }
0458       else
0459         {
0460           if (listframe)  evt->listFrame();
0461           Packet *p[10000];
0462           int nw = evt->getPacketList(p, 10000);
0463           for (i=0; i<nw; i++)
0464         {
0465           if (fullidentify) p[i]->fullIdentify();
0466           if (generic) p[i]->gdump(dumpstyle);
0467           else p[i]->dump();
0468           delete p[i];
0469         }
0470           if (listHistory) evt->listHistory();
0471           if (listError) evt->listError();
0472         }
0473       
0474         
0475       delete evt;
0476       if ( repeatcount==0 ||  ++accepted_count < repeatcount) evt = it->getNextEvent();
0477       else  evt = 0;
0478 
0479     }
0480       else
0481     {
0482       delete evt;
0483       evt = it->getNextEvent(); // try to get the next event
0484     }
0485     }
0486     
0487   delete it;
0488   it = 0;
0489   return 0;
0490 }
0491