APPLICATIONS/geb2daq.cxx

00001 // @(#)fROOT/APPLICATIONS:$Name:  $:$Id: geb2daq.cxx,v 1.10 2007/09/05 09:32:04 Diego_Faso Exp $
00002 // Author: Diego Faso <mailto:faso@to.infn.it>, 2006/07/25
00003 
00004 // the following lines are required in order to compile on Mac-OS
00005 //#ifdef __CINT__
00006 #if !defined(linux) && !defined(__linux) && !defined(__linux__)
00007 #undef __GNUC__
00008 #endif 
00009 
00010 #include <sys/ioctl.h>
00011 #include <sys/socket.h>
00012 #include <netinet/in.h>
00013 
00014 #include <Riostream.h>
00015 
00016 #include "TRandom.h"
00017 
00018 #include "TFndRun.h"
00019 #include "FROOT.h"
00020 
00021 Int_t verbose_level = 0;
00022 
00023 TString db_host = 0;
00024 TString data_path = 0;
00025 TString run_type = 0;
00026 Int_t first_run = 0;
00027 Int_t last_run  = 0;
00028 TFndRun *fRun = 0;
00029 
00030 UInt_t Udp_Port = 2007;
00031 Int_t  gebip = FROOT::Ip2Int(getenv("FND_GEBIP"));
00032 Int_t  nevts = std::numeric_limits<Int_t>::max();
00033 
00034 Int_t      fSdOnl;
00035 UInt_t     fAddLen;
00036 struct sockaddr_in ClAdd;
00037 struct sockaddr_in MyAdd;
00038 struct sockaddr *Cl = 0;
00039 struct sockaddr *My = 0;
00040 int l = 0;
00041 
00042 UInt_t *raw;
00043 Int_t cur_run=0;
00044 UInt_t ec=0;
00045 Int_t istop = 0;
00046 Int_t evlen = 0;
00047 
00048 void print_usage(){
00049   cout << endl << " ---------------------------------------------- " << endl; 
00050   cout << endl << "Hi, this is \"geb2daq\"" << endl;
00051   cout << " -> Available command-line arguments are:" << endl; 
00052   cout << "     - argv[1]: path to raw-data" << endl;
00053   cout << "     - argv[2]: name of the run-type to be processed (es: FINU-COSM-BHABHA-PEDE-...)" << endl;
00054   cout << "     - argv[3]: number of the first run to be processed " << endl;
00055   cout << "     - argv[4]: number of the last run to be processed " << endl;
00056   cout << "     - argv[5]: number of events to be processed " << endl;
00057   cout << "     - argv[6]: simulated DAQ-rate " << endl;
00058   cout << " -> Default arguments:" << endl;
00059   cout << "     - argv[4]: if empty the default is equal to argv[4] (single run processing)" << endl; 
00060   cout << "     - argv[5]: if empty the default is " << endl; 
00061   cout << "     - argv[6]: if empty the default is: random rate" << endl; 
00062   cout << " -> Example command: " << endl;   
00063   cout << " geb2daq $RDT FINU 1357 1359 1000" << endl << endl;   
00064   cout << " -> The DAQ-simulation will send data to the ip-address secified in \"$FND_GEBIP\"." << endl;
00065   cout << "     (actual value for is \"$FND_GEBIP\": 0x" << hex << gebip << dec << endl << endl; 
00066   cout << " ---------------------------------------------- " << endl << endl; 
00067 }
00068 
00069 
00070 void BuildFndRun(){
00071 
00072   //   cout << "Path to raw-data: \"" << data_path << "\"" << endl;
00073   fRun = new TFndRun();
00074   fRun->SetDataPath(data_path);
00075   fRun->SetRunType(run_type);
00076   fRun->SetMaxNumberOfEvents(nevts);
00077 
00078 }
00079 
00080 void create_socket(){
00081 
00082   cout << "Create Socket " << endl;
00083   Cl = (struct sockaddr*) &ClAdd;
00084   My = (struct sockaddr*) &MyAdd;
00085   
00086   fAddLen=sizeof(MyAdd);                
00087   bzero((char*)&MyAdd, fAddLen);
00088   MyAdd.sin_family      = AF_INET;
00089   MyAdd.sin_addr.s_addr = INADDR_ANY;     
00090   
00091   MyAdd.sin_port = htons(Udp_Port);
00092   fSdOnl = socket(AF_INET, SOCK_DGRAM, 0);
00093   bind(fSdOnl, My, fAddLen);
00094   
00095   l=0x10000;
00096   setsockopt(fSdOnl,SOL_SOCKET,SO_SNDBUF,&l,sizeof(l));
00097   
00098 }
00099 
00100 Int_t print_raw_ev(){
00101   
00102   cout << "Event: " << ec << endl;
00103   if(raw){
00104     evlen = raw[0];
00105     if(verbose_level > 1){
00106       for(Int_t i=0; i<5; i++) cout << "raw[ " << i << "]: " << raw[i] << endl;   
00107       //for(Int_t i=0; i<evlen; i++) cout << "raw[ " << i << "]: " << raw[i] << endl;   
00108     }
00109     return 0;
00110   }
00111   else{
00112     cout << " Raw event not available!" << endl;
00113     return -1;
00114   }
00115   
00116 }
00117 
00118 
00119 int check_socket(){
00120 
00121   Int_t check_result=0;
00122   //Int_t nn = 0;
00123   do {
00124     //cout << "check socket " << ++nn << endl;
00125     usleep(1000);
00126     ioctl(fSdOnl,FIONREAD,(Char_t *)&check_result);
00127   } while (check_result == 0);
00128   return check_result;
00129 }
00130 
00131 
00132 void send_one_event(){
00133 
00134   Int_t check_result=check_socket();
00135   if(check_result){
00136     if(verbose_level > 2) cout << "receive request" << endl;
00137     Int_t receive_err = recvfrom(fSdOnl ,(Char_t *)&check_result  ,sizeof(int) ,0 ,Cl ,&fAddLen);
00138     if(verbose_level > 2) cout << "get event: receive_err = " << receive_err << endl;
00139         
00140     ec++;
00141 
00142     // ---
00143     Int_t print_ret_val = 0;
00144     if(verbose_level > 1) print_ret_val = print_raw_ev();
00145     else if(verbose_level > 0 && ec%100==0) print_ret_val = print_raw_ev();
00146     if(print_ret_val !=0) cout << "PRINT-RAW error!" << endl;
00147     // ---
00148     sendto(fSdOnl ,(char*)raw,evlen*sizeof(int),0 ,Cl ,fAddLen);
00149   }
00150   else{
00151     cout << ".....................................\"check_result\"=0 ...!!!" << endl;
00152     sendto(fSdOnl ,&check_result ,sizeof(int) ,0 ,Cl ,fAddLen);  
00153   }
00154   return;
00155 }
00156 
00157 void event_loop(const Long_t &delay){
00158   if(verbose_level > 0) cout << "Start EVENT loop" << endl;
00159 
00160   Int_t initrun_val=0;
00161 
00162   for(cur_run=first_run;cur_run<=last_run;cur_run++){
00163     if(ec>nevts){
00164       cout  << "geb2daq message: Requested number of events reached." << endl;
00165       return;
00166     }
00167     if(verbose_level > 0) cout << "Initializing new run: run " << cur_run << " ev: " << ec << " ; requested evts: " << nevts << endl;
00168     initrun_val = fRun->InitNewRun(run_type,cur_run);
00169     if (initrun_val == -4) FROOT::TerminateFroot(0,TString("wrong argument received as \"run-type\" (\""+run_type)+"\").");
00170 
00171     Int_t exit_val = 0;
00172     while(exit_val == 0 &&  initrun_val==0) {
00173       raw = fRun->GetNextRawEvent(exit_val); 
00174       send_one_event();
00175       fRun->ClearEvent();
00176       
00177       if(delay > 0 ) usleep((ULong_t)delay);
00178       //      else usleep((ULong_t)(gRandom->Gaus(1000000,500000)));
00179       else usleep((ULong_t)(gRandom->Uniform(1,1500000)));
00180       
00181       usleep(100); // required anyway
00182     }
00183     
00184     if(verbose_level > 0) cout << "geb2daq message: End of Run Processing: " << ec << " events " << endl;   
00185     if(cur_run != last_run) fRun->FinishRun();
00186   }
00187 }
00188 
00189 Int_t main(Int_t argc, Char_t **argv) {
00190 
00191   //  for(Int_t i=0;i<argc;i++) cout << "argv[" << i << "]" << argv[i] << endl;
00192 
00193   verbose_level = 2;
00194 
00195 
00196   //  Float_t max_rate = std::numeric_limits<Float_t>::max(); // ev/s
00197   Float_t max_rate = -1;
00198 
00199   if(argc < 4){
00200     print_usage();
00201     return 0;
00202   }
00203 
00204   data_path = argv[1];
00205   run_type = argv[2];
00206   first_run = atoi(argv[3]);
00207   last_run = first_run;
00208 
00209   if(argc > 4) last_run = atoi(argv[4]);
00210   if(argc > 5) nevts = atoi(argv[5]);
00211   if(argc > 6) max_rate = atoi(argv[6]);
00212 
00213   Long_t Delay = (Long_t)(1e+6 * (Float_t)(1./max_rate)); // microseconds
00214   cout << "geb2daq message: " << nevts << " events will be sent to ip \"" << gebip << "\" (port " << Udp_Port << ")" << endl;
00215   if(Delay>0) cout << "geb2daq message: Maximum rate set to " << max_rate << " ev/s (delay: " << (ULong_t)(Delay/1000) << " ms)." << endl;
00216   else cout << "geb2daq message: simulated DAQ-rate set to \"random\" (delay range: 1ms - 10 s)." << endl;
00217   BuildFndRun();
00218   create_socket();
00219   event_loop(Delay);
00220   
00221   fRun->FinishRun(kTRUE);
00222   delete fRun;
00223   cout << endl << " ---> geb2daq process completed... bye bye ;)"<< endl;
00224 }

Generated on Tue Oct 16 15:40:46 2007 by  doxygen 1.5.2