Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4EventServer/TGo4MbsSource.cxx

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4MbsSource.h"
00017 
00018 #include <iostream.h>
00019 
00020 #include "TGo4MbsEvent.h"
00021 #include "TGo4MbsSubEvent.h"
00022 
00023 #include "Go4Log/TGo4Log.h"
00024 #include "Go4Event/TGo4EventErrorException.h"
00025 #include "Go4Event/TGo4EventTimeoutException.h"
00026 #include "Go4Event/TGo4EventEndException.h"
00027 
00028 const UInt_t TGo4MbsSource::fguLONGBYCHAR= sizeof(Int_t) / sizeof(Char_t);
00029 
00030 const UInt_t TGo4MbsSource::fguSHORTBYCHAR= sizeof(Short_t) / sizeof(Char_t);
00031 
00032 const UInt_t TGo4MbsSource::fguLONGBYSHORT= sizeof(Int_t) / sizeof(Short_t);
00033 
00034 const UInt_t TGo4MbsSource::fguEVHEBYCHAR= sizeof(s_evhe) /  sizeof(Char_t);
00035 
00036 
00037 TGo4MbsSource::TGo4MbsSource(const char* name, Int_t mode)
00038 : TGo4EventSource(name), fiMode(mode),
00039    fxEvent(0), fxBuffer(0), fxInfoHeader(0),
00040    fbIsOpen(kFALSE), fbDataCopyMode(kFALSE), fuEventCounter(0),fuStartEvent(0) ,fuStopEvent(0),
00041    fuEventInterval(0), fiTimeout(-1)
00042 {
00043    TRACE((15,"TGo4MbsSource::TGo4MbsSource(Text_t*, Int_t)",__LINE__, __FILE__));
00044 // Open() call will be done by subclasses ctors, so we can overwrite Open() method
00045 //cout <<"TGo4MbsSource with data copy mode="<<fbDataCopyMode << endl;
00046 
00047 }
00048 
00049 
00050 TGo4MbsSource::TGo4MbsSource()
00051 : TGo4EventSource("default mbs source"),fiMode(0),
00052   fxEvent(0), fxBuffer(0), fxInfoHeader(0),
00053   fbIsOpen(kFALSE),fbDataCopyMode(kFALSE),fuEventCounter(0),fuStartEvent(0) ,fuStopEvent(0),
00054   fuEventInterval(0), fiTimeout(-1)
00055 {
00056    TRACE((15,"TGo4MbsSource::TGo4MbsSource()",__LINE__, __FILE__));
00057 }
00058 
00059 TGo4MbsSource::~TGo4MbsSource()
00060 {
00061   TRACE((15,"TGo4MbsSource::~TGo4MbsSource()",__LINE__, __FILE__));
00062   Close();
00063 }
00064 
00065 void TGo4MbsSource::SetPrintEvent(Int_t num, Int_t sid, Int_t longw, 
00066                                   Int_t hexw, Int_t dataw)
00067 {
00068     fxPrEventPar.fiNum=num;
00069     fxPrEventPar.fiSid=sid;
00070     fxPrEventPar.fiLong=longw;
00071     fxPrEventPar.fiHex=hexw;
00072     fxPrEventPar.fiData=dataw;
00073 }
00074 
00075 
00076 void TGo4MbsSource::BuildMbsEvent(TGo4MbsEvent* target)
00077 {
00078 TRACE((12,"TGo4MbsSource::BuildMbsEvent(TGo4MbsEvent*)",__LINE__, __FILE__));
00079 
00080    if(fxEvent!=0 && GetEventStatus()==GETEVT__SUCCESS)
00081       {
00082       // check for printevent mode here:
00083       if(fxPrEventPar.fiNum>0)
00084          {
00085             f_evt_type(fxBuffer,
00086                         (s_evhe *) fxEvent,
00087                         fxPrEventPar.fiSid,
00088                         fxPrEventPar.fiLong,
00089                         fxPrEventPar.fiHex,
00090                         fxPrEventPar.fiData);
00091             cout << endl; // flush cout buffer
00092             fxPrEventPar.fiNum--;
00093          }
00094       // we have a valid event, proceed
00095         Char_t* endofevent= (Char_t*) (fxEvent) +
00096                                  (fxEvent->l_dlen) * fguSHORTBYCHAR +
00097                               fguEVHEBYCHAR ;
00098       //cout << "end of event "<< endofevent <<endl;
00099       s_ves10_1* subevent; // pointer to subevent
00100       Char_t* subevtpointer; // dito, in bytes
00101       TGo4MbsSubEvent* subtarget;
00102       TGo4MbsSubEvent* subtargetindex; // target subevent
00103       // copy event header information to our class
00104       target->SetValid(kTRUE); // reset target if previously was set to false
00105       target->SetDlen(fxEvent->l_dlen);
00106       target->SetType(fxEvent->i_type);
00107       target->SetSubtype(fxEvent->i_subtype);
00108       target->SetTrigger(fxEvent->i_trigger);
00109       target->SetCount(fxEvent->l_count);
00110       target->SetDummy(fxEvent->i_dummy);
00111       Int_t totalsubdatalength=0; // check counter for total datalength of subevents
00112       if(fxEvent->l_dlen > 4)
00113          {
00114             // we have subevent data after the event header, proceed:
00115             subevent= (s_ves10_1*) (fxEvent + 1);
00116                   // first subevent header starts after event header
00117             // loop over subevents:
00118             Int_t datalength=0; // direct dlen from subevent header (in Short_t!)
00119             Int_t fieldlength=0; // actual size of the target Int_t data field
00120            while((datalength=subevent->l_dlen) >0)
00121                   {
00122                   totalsubdatalength+=datalength-2+sizeof(s_ves10_1)/sizeof(Short_t);
00123                   if(datalength>fxEvent->l_dlen)
00124                      {
00125                         TGo4Log::Debug(" !!! MbsSource --  SUBEVENTS LENGTH mismatch!!! skipping event #%d",fxEvent->l_count);
00126                         TGo4Log::Debug("\t sub dlen:%d, event dlen:%d ",datalength-2, fxEvent->l_dlen-4);
00127                         target->SetValid(kFALSE);
00128                         break;
00129                      }
00130 
00131                   if(datalength>2)
00132                      {
00133                         fieldlength=   (datalength-2) / fguLONGBYSHORT ; // field is Int_t
00134                      }
00135                   else
00136                      {
00137                         TGo4Log::Debug(" !!! MbsSource --  EMPTY subevent #%d ",fxEvent->l_count);
00138                         fieldlength =0; // for empty subevents (<- W.M.)
00139                      }
00140 
00141                   target->ResetIterator();
00142                   subtarget=0; // reset
00143                   while ( ( subtargetindex= target->NextSubEvent(kTRUE) ) !=0 )
00144                      {
00145                         // check ids, later more elegant with complete longword
00146 //                        if((subtargetindex->GetProcid()== subevent->i_procid)
00147 //                           && (subtargetindex->GetSubcrate() == subevent->h_subcrate)
00148 //                           && (subtargetindex->GetControl() == subevent->h_control)   )
00149                         //   note: speed does not differ much here!
00150 
00151                         // get pointer to complete id longword in structures:
00152                         Int_t* subtargetid= (Int_t*) &((subtargetindex->fxHeader).fsProcid);
00153                         Int_t* subeventid= (Int_t*) &(subevent->i_procid);
00154                         if(*subtargetid == *subeventid)
00155                         {
00156                               // subevent ids match:
00157                               if(!subtargetindex->fbIsFilled)
00158                                  {
00159                                     // this has not been filled before, we fill this one
00160                                     subtarget=subtargetindex;
00161                                     break; // leave the loop, fill later
00162                                  }
00163                               else
00164                                  {
00165                                      // was already filled in this cycle, continue for next sub
00166                                  }
00167                            }
00168                         else
00169                            {
00170                               // no match, try next subevent
00171                            }
00172                      } // while (subtargetindex...)
00173                   if(!subtarget)
00174                      {
00175                         // we found no matching id, create new TObjArray entry
00176                         subtarget = new TGo4MbsSubEvent(fieldlength);
00177                         if(!fbDataCopyMode)
00178                            {
00179                               subtarget->fbIsDataOwner=kFALSE;
00180                               delete [] (subtarget->fiData); // remove default field
00181                               subtarget->fiAllocLen=0;
00182                            }
00183                         TGo4Log::Debug(" Created new output subevent for event %d\n\tpid:%d subcrate:%d ctrl:%d",
00184                                  fxEvent->l_count ,subevent->i_procid,subevent->h_subcrate, subevent->h_control);
00185                         target->fxSubEvArray->AddLast(subtarget);
00186                      }
00187                   else
00188                      {   }
00190                   subtarget->SetDlen(subevent->l_dlen);
00191                   subtarget->SetType(subevent->i_type);
00192                   subtarget->SetSubtype(subevent->i_subtype);
00193                   subtarget->SetProcid(subevent->i_procid);
00194                   subtarget->SetSubcrate(subevent->h_subcrate);
00195                   subtarget->SetControl(subevent->h_control);
00196                   void* data = (void*) (subevent+1); // data field starts after header structure
00197                   if(fbDataCopyMode)
00198                      {
00199                      subtarget->fbIsDataOwner=kTRUE;
00200                      subtarget->ReAllocate(fieldlength); // reallocate field if necessary
00202                      if(datalength>2)
00203                         {
00204                            memcpy((void*) (subtarget->fiData),
00205                                     data,
00206                                     (datalength-2)*sizeof(Short_t) );
00207                         }
00208                      else
00209                         {
00210                            // empty subevent, do not copy
00211                         }
00212                      }
00213                   else
00214                      {
00215                         // set reference to external data field in subevent
00216                         subtarget->fbIsDataOwner=kFALSE;
00217                         subtarget->fiAllocLen=fieldlength;
00218                         if(datalength>2)
00219                            subtarget->fiData= (Int_t*) data;
00220                         else
00221                            subtarget->fiData= 0; // reset for empty subevent
00222                      }// if(fbDataCopyMode)
00223                   subtarget->fbIsFilled=kTRUE; // remember we filled this one, never overwrite!
00225                   subevtpointer = ( (Char_t*) (subevent) +
00226                                          datalength * fguSHORTBYCHAR +
00227                                             fguEVHEBYCHAR );
00228                  subevent = (s_ves10_1*) subevtpointer;
00229                   if ((void*) (subevent) >= endofevent)
00230                      {
00231                      //cout << "found end of event, breaking.."<< endl;
00232                      break;
00233                      }
00234               } // while((datalength=subevent->l_dlen) >0)
00235             if(totalsubdatalength!=fxEvent->l_dlen-4)
00236                      {
00237                         TGo4Log::Debug(" !!! MbsSource --  SUBEVENTS TOTAL LENGTH mismatch!!! disabling event #%d",fxEvent->l_count);
00238                   TGo4Log::Debug("\t subdlen sum:%d, event dlen:%d ",totalsubdatalength, fxEvent->l_dlen-4);
00239                   target->SetValid(kFALSE);
00240                     }
00241          }
00242       else // if(fxEvent->dlen>0)
00243          {
00244             // sorry, no subevents after event header
00245             TGo4Log::Debug(" !!! MbsSource --  NO SUBEVENTS!!! ");
00246             SetErrMess("!!! BuildMbsEvent: --  NO SUBEVENTS!!!");
00247             throw TGo4EventTimeoutException(this); // no subevts=timeout
00248          } // end if (fxEvent->dlen>0)
00249       }
00250    else
00251       {
00252          // somethings wrong, display error message from f_evt_error()
00253             TGo4Log::Debug(" !!! Mbs Source --  ERROR: %s !!! ",GetErrMess());
00254             throw TGo4EventErrorException(this);
00255       }
00256 }
00257 
00258 Int_t TGo4MbsSource::NextEvent()
00259 {
00260 TRACE((12,"TGo4MbsSource::NextEvent()",__LINE__, __FILE__));
00261    // skip and sample mode introduced without changed gsievt functions for first tests
00262    UInt_t eventstep;
00263    if(fuEventInterval)
00264       {
00265          eventstep=fuEventInterval;
00266       }
00267    else
00268       {
00269          eventstep=1;
00270       }
00271    // test if we had reached the last event:
00272    if(fuStopEvent!=0 && fuEventCounter>=fuStopEvent)
00273       {
00274          SetEventStatus(GETEVT__NOMORE);
00275       }
00276    else
00277       {
00278          // go to the start event if necessary
00279          if(fuEventCounter<fuStartEvent)
00280             {
00281                while(fuEventCounter++<fuStartEvent){}
00282             }
00283          else {}
00284          UInt_t counter;
00285          for(counter=fuEventCounter; counter<fuEventCounter+eventstep; ++counter)
00286             {
00287             // retrieve the event, skip all events until end of the step
00288             Int_t status=f_evt_get_event(
00289                                        &fxInputChannel,
00290                                        (Int_t **) &fxEvent,
00291                                        (Int_t **) &fxBuffer);
00292             SetEventStatus(status);
00293             if(status!=0)
00294                   break;
00295             }
00296          fuEventCounter=counter; // index of next event
00297       }
00298 if(GetEventStatus()!=0)
00299    {
00300       Text_t buffer[TGo4EventSource::fguTXTLEN];
00301       f_evt_error(GetEventStatus(),buffer,1); // provide text message for later output
00302       SetErrMess(buffer);
00303    }
00304 
00305    switch(GetEventStatus())
00306       {
00307           case 0:
00308              return 0;
00309              break;
00310           case GETEVT__TIMEOUT:
00311              throw TGo4EventTimeoutException(this);
00312              break;
00313           case GETEVT__NOMORE:
00314              throw TGo4EventEndException(this);
00315              break;
00316           default:
00317              throw TGo4EventErrorException(this);
00318              break;
00319      }
00320    cout << "MbsSource::NextEvent --  NEVER COME HERE" << endl;
00321    return GetEventStatus();
00322 }
00323 
00324 Int_t TGo4MbsSource::Open()
00325 {
00326 TRACE((12,"TGo4MbsSource::Open()",__LINE__, __FILE__));
00327 
00328 if(fbIsOpen)
00329    return -1;
00330 //cout << "Open of TGo4MbsSource"<< endl;
00331 // open connection/file
00332    Int_t status    = f_evt_get_open(
00333                               fiMode,
00334                               const_cast<Text_t*>( GetName() ),
00335                               &fxInputChannel,
00336                               (Char_t**) &fxInfoHeader,
00337                               0,
00338                               0
00339                               );
00340 //status= f_evt_get_open(mode, GetName(), &fxInputChannel, 0 , 0, 0); // no info header
00341    SetCreateStatus(status);
00342 if(GetCreateStatus() !=GETEVT__SUCCESS)
00343    {
00344 //     TGo4Log::Debug(" Mbs Source --  !!! failed to open input from type %d:  %s!!! ",
00345 //        fiMode, GetName());
00346   Text_t buffer[TGo4EventSource::fguTXTLEN];
00347   f_evt_error(GetCreateStatus(),buffer,1); // provide text message for later output
00348 //
00349 //   snprintf(buffer,TGo4EventSource::fguTXTLEN-1," Mbs Source --  !!! failed to open input from type %d:  %s!!! ",
00350 //        fiMode, GetName());
00351    SetErrMess(buffer);
00352    fbIsOpen=kFALSE;
00353    throw TGo4EventErrorException(this);
00354    }
00355 else
00356    {
00357       f_evt_timeout(&fxInputChannel, fiTimeout);
00358       TGo4Log::Debug(" Mbs Source --  opened input from type %d:  %s . Timeout=%d s",
00359                fiMode, GetName(), fiTimeout);
00360 
00361       fbIsOpen=kTRUE;
00362    }
00363    return status;
00364 }
00365 
00366 Int_t TGo4MbsSource::Close()
00367 {
00368 TRACE((12,"TGo4MbsSource::Close()",__LINE__, __FILE__));
00369 if(!fbIsOpen)
00370    return -1;
00371 //cout << "Close of TGo4MbsSource"<< endl;
00372    Int_t rev=GetCreateStatus();
00373 // close connection/file
00374 if(rev == GETEVT__SUCCESS)
00375    {
00376       f_evt_get_close(&fxInputChannel);
00377       fbIsOpen=kFALSE;
00378    }
00379 else
00380    {
00381       // do nothing (for streamer)
00382    }
00383 
00384    return rev;
00385 
00386 
00387 }
00388 
00389 
00390 s_bufhe * TGo4MbsSource::GetBufferHeader()
00391 {
00392    return (s_bufhe*) f_evt_get_buffer_ptr(&fxInputChannel);
00393 }
00394 
00395 
00396 ClassImp(TGo4MbsSource)
00397 
00398 
00399 
00400 
00401 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:55:53 2005 for Go4-v2.10-5 by doxygen1.2.15