//*-- Author :    Damir Buskulic   11/07/00

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// VConditionSet                                                        //
//                                                                      //
// Class for managing sets of conditions, i.e. conditions               //
// that may have overlap(s) in time.                                    //
// A condition set is used as a selection and a pointer to a particular //
// point in time where the selection expression is verified.            //
// It is used in sequential access of frames or vectors satisfying some //
// conditions. Suppose for example that one wants to access all the     //
// frames that have triggered the condition "Trig3.amplitude>3", where  //
// Trig3 is a condition (or trigger) defined in the frames.             //
// If vd is a pointer to a valid info database containing               //
// the information, one would do :                                      //
//                                                                      //
// vega[] co = new VConditionSet(vd,"Trig3.amp>3")                      //
// vega[] fr = frchannel->GetNextFrame(co)                              //
//                                                                      //
// that would return him the first frame satisfying the condition.      //
// Issueing fr = frchannel->GetNextFrame(co) repeatedly will return     //
// sequentialy all the frames that satisfy the condition.               //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "VConditionSetFMDB.h"

ClassImp(VConditionSetFMDB)

//______________________________________________________________________________
VConditionSetFMDB::VConditionSetFMDB() : VConditionSet()
{
//   Condition set default constructor
}

//______________________________________________________________________________
VConditionSetFMDB::VConditionSetFMDB(VFrameMetaDB* metadb, VConditionFormula* condf, VConditionFormula* self) :
                   VConditionSet()
{
//   Condition set constructor
//   "metadb" is the parent metadatabase
//   "condf"  is a condition formula used to determine the active conditions
//   "self"   is a selection formula. Used in case one wants to follow a set
//            of condition, given another set is present.
   
   Int_t i, nselcond;
   
   mInfoDB = metadb;
   mCondFormula = 0;
   mSelFormula  = 0;
   if (condf) {
      mCondFormula = new VConditionFormula(*condf);
      nselcond = condf->GetNConditions();
      for (i=0;i<nselcond;i++) SetActive(condf->GetConditionNumber(i));
   }
   if (self) {
      mSelFormula = new VConditionFormula(*self);
      nselcond = self->GetNConditions();
      for (i=0;i<nselcond;i++) SetSelection(self->GetConditionNumber(i));
   }
}

//______________________________________________________________________________
VConditionSetFMDB::VConditionSetFMDB(VFrameMetaDB* metadb, const char* condfs, const char* selfs) :
                   VConditionSet()
{
//   Condition set constructor
//   "metadb" is the parent metadatabase
//   "condf"  is a condition expression used to determine the active conditions
//   "self"   is a selection expression. Used in case one wants to follow a set
//            of conditions, given another set is present.
   Int_t i, nselcond;
   
   mInfoDB = metadb;
   if (condfs && strlen(condfs) != 0) mCondFormulaString = condfs;
   if (selfs && strlen(selfs) != 0)  mSelFormulaString  = selfs;
   mCondFormula = 0;
   mSelFormula  = 0;
   if (condfs && strlen(condfs) != 0) {
      mCondFormula = new VConditionFormula("condf",condfs,mInfoDB);
      nselcond = mCondFormula->GetNConditions();
      for (i=0;i<nselcond;i++) SetActive(mCondFormula->GetConditionNumber(i));
   }
   if (selfs && strlen(selfs) != 0) {
      mSelFormula = new VConditionFormula("self",selfs,mInfoDB);
      nselcond = mSelFormula->GetNConditions();
      for (i=0;i<nselcond;i++) SetSelection(mSelFormula->GetConditionNumber(i));
   }
}

//______________________________________________________________________________
VConditionSetFMDB::~VConditionSetFMDB()
{
//   Condition set default destructor
}

//______________________________________________________________________________
 Int_t VConditionSetFMDB::NearestGESet(Double_t time)
{
//  Jumps to the condition set that contains time "time" or the nearest
//  next one. Loads the relevant conditions as current.
//  returns 0 = error, 1 = OK
   
   Int_t i, cok;
   
// Loads the nearest conditions after time "time" without bothering about
// intersections
   for (i=0; i<mActiveConditions->GetSize();i++) {
      if ((*mActiveConditions)[i] || (*mSelectionConditions)[i]) {
         cok = ((VFrameMetaDB*)mInfoDB)->LoadNearestGECondition(time, i);
         if (cok<0) return 0;
         SetCurCondition(i,((VFrameMetaDB*)mInfoDB)->GetLoadedCondition(i));
      }
   }

// If no intersection, jump to next set.
   if (GetIntersectionStart()>GetIntersectionEnd()) {
      return NextSet();
   } else {
      return 1;
   }
}

//______________________________________________________________________________
 Int_t VConditionSetFMDB::NearestGEFormSet(Double_t time)
{
//  Jumps to the condition set that contains time "time" and satisfies recorded
// formula or the nearest next one. Loads the relevant conditions as current.
//  returns 0 = error, 1 = OK
   
   Int_t i, cok;
   
// Loads the nearest conditions after time "time" without bothering about
// intersections
   
   if (!mCondFormula && !mSelFormula) return NextFormSet();
   
   for (i=0; i<mActiveConditions->GetSize();i++) {
      if ((*mActiveConditions)[i] || (*mSelectionConditions)[i]) {
         cok = ((VFrameMetaDB*)mInfoDB)->LoadNearestGECondition(time, i);
         if (cok<0) return 0;
         SetCurCondition(i,((VFrameMetaDB*)mInfoDB)->GetLoadedCondition(i));
      }
   }

// If no intersection, jump to next set.
   if (GetIntersectionStart()<=GetIntersectionEnd()) {
      if (!mCondFormula && !mSelFormula) return 1;
      if (mSelFormula) {
         if (mCondFormula) { 
            if (mSelFormula->EvalInstance(this) &&
                mCondFormula->EvalInstance(this)) return 1;
         } else {
            if (mSelFormula->EvalInstance(this)) return 1;
         }
      } else {
            if (mCondFormula->EvalInstance(this)) return 1;
      }
   }
   return NextFormSet();
}

//______________________________________________________________________________
 Int_t VConditionSetFMDB::NextSet()
{
//  Jumps to the next condition set in time (with respect to current)
//  if a selection is present, will also require that the next set
//  intersects with the conditions forming the selection.
//  WARNING : if a selection is used, it may happen to return twice the same
//  set for two consecutive calls to NextSet(), in case more than one
//  selection is intersecting with that set.
//  returns 0 = error, 1 = OK
   
   Int_t i, nearnext;
   Double_t nearnexttime;
   Int_t cok;

// First, load the conditions following the current ones and set
// the following conditions start
   for (i=0; i<mActiveConditions->GetSize();i++) {
      if ((*mActiveConditions)[i] || (*mSelectionConditions)[i]) {
         cok = ((VFrameMetaDB*)mInfoDB)->LoadNearestGECondition((*mCurConditionEnd)[i]+1e-4, i);
         if (cok<0) {
            (*mFolConditionStart)[i] = -1;
         } else {
            (*mFolConditionStart)[i] = ((VFrameMetaDB*)mInfoDB)->GetLoadedCondition(i)->GetStartTime();
         }
      }
   }
   
// Search the conditions until there is an intersection
   while (1) {
      nearnext=-1;
      nearnexttime = 1e20;
// Look at the nearest of the following conditions
      for (i=0; i<mActiveConditions->GetSize();i++) {
         if ( ((*mActiveConditions)[i] || (*mSelectionConditions)[i]) && (*mFolConditionStart)[i] > 0 && (*mFolConditionStart)[i] < nearnexttime) {
            nearnext = i;
            nearnexttime = (*mFolConditionStart)[i];
         }
      }
      
      if (nearnext == -1) return 0; // No following conditions, end of the search
// Load this nearest condition as the current one and load the following
      if ((*mFolConditionStart)[nearnext] > 0) {
         SetCurCondition(nearnext,((VFrameMetaDB*)mInfoDB)->GetLoadedCondition(nearnext));
      } else {
         return 0;
      }
      cok = ((VFrameMetaDB*)mInfoDB)->LoadNextCondition(nearnext);
      if (cok<0) {
         (*mFolConditionStart)[nearnext] = -1;
      } else {
         (*mFolConditionStart)[nearnext] = ((VFrameMetaDB*)mInfoDB)->GetLoadedCondition(nearnext)->GetStartTime();
      }
// Load all the conditions that are finished
      for (i=0; i<mActiveConditions->GetSize();i++) {
         if ( ((*mActiveConditions)[i] || (*mSelectionConditions)[i]) && (*mCurConditionEnd)[i] < (*mCurConditionStart)[nearnext]) {
            if ((*mFolConditionStart)[i] > 0) {
               SetCurCondition(i,((VFrameMetaDB*)mInfoDB)->GetLoadedCondition(i));
            } else {
               return 0;
            }
            cok = ((VFrameMetaDB*)mInfoDB)->LoadNextCondition(i);
            if (cok<0) {
               (*mFolConditionStart)[i] = -1;
            } else {
               (*mFolConditionStart)[i] = ((VFrameMetaDB*)mInfoDB)->GetLoadedCondition(i)->GetStartTime();
            }
         }
      }
      
// Test that there is an intersection
      if (GetIntersectSelectStart()<GetIntersectSelectEnd()) {
         break;   // yes, we are at the next set
      }
   }
   
   if (gDebug) {
      printf("Next condition set : n");
      for (i=0; i<mActiveConditions->GetSize();i++) {
         printf("   condition %s  start, end : %f %fn",((VFrameMetaDB*)mInfoDB)->GetConditionNames()->GetConditionName(i),
                     (*mCurConditionStart)[i],(*mCurConditionEnd)[i]);
      }
      printf("   selection start, end : %f %fn",GetIntersectSelectStart(),GetIntersectSelectEnd());
   }
   
   return 1;
}

//______________________________________________________________________________
 Int_t VConditionSetFMDB::NextFormSet()
{
//  Jumps to the next condition set in time (with respect to current)
//  that verifies the condition formula if present.
//  if a selection is present, will also require that the set
//  intersects with the conditions forming the selection.
//  WARNING : if a selection is used, it may happen to return twice the same
//  set for two consecutive calls to NextSet(), in case more than one
//  selection is intersecting with that set.
//  returns 0 = error, 1 = OK

   if (!mCondFormula && !mSelFormula) return NextSet();
   if (mSelFormula) {
      if (mCondFormula) { 
         while (NextSet()) {
            if (mSelFormula->EvalInstance(this) &&
                mCondFormula->EvalInstance(this)) return 1;
         }
      } else {
         while (NextSet()) {
            if (mSelFormula->EvalInstance(this)) return 1;
         }
      }
   } else {
      while (NextSet()) {
            if (mCondFormula->EvalInstance(this)) return 1;
      }
   }
   return 0;
}

//______________________________________________________________________________
 Double_t VConditionSetFMDB::GetIntersectionStart()
{
//  Returns the start time of the overlap of this set of conditions
   
   Double_t startmax = 0;
   Int_t i;
   
   for (i=0; i<mCurConditionStart->GetSize();i++) {
      if ((*mCurConditionStart)[i]>0 &&(*mCurConditionStart)[i]>startmax && (*mActiveConditions)[i])
         startmax = (*mCurConditionStart)[i];
   }
   return startmax;
}

//______________________________________________________________________________
 Double_t VConditionSetFMDB::GetIntersectionEnd()
{
//  Returns the end time of the overlap of this set of conditions
   
   Double_t endmin = 1e20;
   Int_t i;
   
   for (i=0; i<mCurConditionEnd->GetSize();i++) {
      if ((*mCurConditionEnd)[i] >0 &&(*mCurConditionEnd)[i]<endmin && (*mActiveConditions)[i])
         endmin = (*mCurConditionEnd)[i];
   }
   return endmin;
}

//______________________________________________________________________________
 Double_t VConditionSetFMDB::GetIntersectSelectStart()
{
//  Returns the start time of the overlap of this set of conditions
// including the conditions used for selection
   
   Double_t startmax = 0;
   Int_t i;
   
   for (i=0; i<mCurConditionStart->GetSize();i++) {
      if ((*mCurConditionStart)[i] >0 &&(*mCurConditionStart)[i]>startmax)
         startmax = (*mCurConditionStart)[i];
   }
   return startmax;
}

//______________________________________________________________________________
 Double_t VConditionSetFMDB::GetIntersectSelectEnd()
{
//  Returns the end time of the overlap of this set of conditions
// including the conditions used for selection
   
   Double_t endmin = 1e20;
   Int_t i;
   
   for (i=0; i<mCurConditionEnd->GetSize();i++) {
      if ((*mCurConditionEnd)[i] >0 &&(*mCurConditionEnd)[i]<endmin)
         endmin = (*mCurConditionEnd)[i];
   }
   return endmin;
}

//______________________________________________________________________________
 Int_t VConditionSetFMDB::GetGroup()
{
//  Returns the group number of the overlap of this set of conditions
//  It is the group number of the condition starting the last among
//  all the participating conditions. 
   
   Double_t startmax = 0;
   Int_t i, indexmax;
   
   indexmax = -1;
   for (i=0; i<mCurConditionStart->GetSize();i++) {
      if ((*mCurConditionStart)[i] >0 &&(*mCurConditionStart)[i]>startmax) {
         startmax = (*mCurConditionStart)[i];
         indexmax = i;
      }
   }
   if (indexmax == -1) return -1;
   if (mCurConditions->At(indexmax)) {
         return ((VFrCondition*)mCurConditions->At(indexmax))->GetGroup();
   }
   return -1;
}

//______________________________________________________________________________
 void VConditionSetFMDB::SetCurCondition(Int_t conditionnumber, VFrCondition* cond)
{
// Copies the condition cond into the internal current condition of this set
// which has number "conditionnumber"
   
   cond->Copy(*((VFrCondition*)mCurConditions->At(conditionnumber)));
   (*mCurConditionStart)[conditionnumber] = cond->GetStartTime();
   (*mCurConditionEnd)[conditionnumber] = cond->GetEndTime();
}

//______________________________________________________________________________
 void VConditionSetFMDB::SetActive(Int_t conditionnumber)
{
// Sets the condition "conditionnumber" as active
   
   VFrCondition* condition;
   
   (*mActiveConditions)[conditionnumber] = 1;
// If not present, build new condition objects for active conditions
   if ( !(mCurConditions->At(conditionnumber))) {
      condition = new VFrCondition();
      mCurConditions->AddAt(condition,conditionnumber);
   }
}

//______________________________________________________________________________
 void VConditionSetFMDB::SetSelection(Int_t conditionnumber)
{
// Sets the condition "conditionnumber" as used for selection
   printf("heren");
   VFrCondition* condition;
   
   (*mSelectionConditions)[conditionnumber] = 1;
// If not present, build new condition objects for active/selection conditions
   if ( !(mCurConditions->At(conditionnumber))) {
      condition = new VFrCondition();
      mCurConditions->AddAt(condition,conditionnumber);
   }
}

//______________________________________________________________________________
 Double_t VConditionSetFMDB::GetLastConditionResult()
{
// Returns the result of the condition formula applied to the last condition set
   
   return mCondFormula->GetLastResult();
}

//______________________________________________________________________________
 Double_t VConditionSetFMDB::GetLastSelectionResult()
{
// Returns the result of the selection formula applied to the last condition set
   
   return mSelFormula->GetLastResult();
}

//______________________________________________________________________________
 Double_t VConditionSetFMDB::Eval(const char* formula)
{
// Evaluates the formula for the current condition set.
// Useful in case of interactive use.
   
   VConditionFormula* tmpformula;
   Double_t result;
   
   tmpformula = new VConditionFormula("tmpformula",formula,((VFrameMetaDB*)mInfoDB));
   result = tmpformula->EvalInstance(mCurConditions);
   
   delete tmpformula;
   return result;
}


- ROOT page - VEGA page - Class index - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to , or contact with any questions or problems regarding ROOT or VEGA.