Exemple #1
0
bool CRectangle::Contains(const CRectangle& rc) const
{
    int left1, top1, right1, bottom1;
    int left2, top2, right2, bottom2;
    left1 = GetX();
    top1 = GetY();
    right1 = GetX() + GetW();
    bottom1 = GetY() + GetH();

    left2 = rc.GetX();
    top2 = rc.GetY();
    right2 = rc.GetX() + rc.GetW();
    bottom2 = rc.GetY() + rc.GetH();

//    return !(left1 < right2 && right1 > left2 &&
//        top1 > bottom2 && bottom1 < top2);
//    return (left1 < right2 || right1 > left2 ||
//        top1 > bottom2 || bottom1 < top2);
//    return ((left1 < right2) || (right1 > left2) ||
//        (top1 > bottom2) || (bottom1 < top2));
//    return (!(left1 < right2) || !(right1 > left2) ||
//        !(top1 > bottom2) || !(bottom1 < top2));
//    return (!(left1 < right2) && !(right1 > left2) &&
//        !(top1 > bottom2) && !(bottom1 < top2));

    bool xOverlap = valueInRange(left1, left2, right2) ||
        valueInRange(left2, left1, right1);

    bool yOverlap = valueInRange(top1, top2, bottom2) ||
        valueInRange(top2, top1, bottom1);

    return xOverlap && yOverlap;

}
Exemple #2
0
bool Hitbox::isIntersectingWith(const Hitbox& other){
	bool xOverlap = valueInRange(this->getX(),other.getX(), other.getX() + other.getWidth()) 
			|| valueInRange(other.getX(), this->getX(), this->getX() + this->getWidth());
	bool yOverlap = valueInRange(this->getY(),other.getY(),other.getY() + other.getHeight())
			|| valueInRange(other.getY(), this->getY(), this->getY() + this->getHeight());
	return xOverlap && yOverlap;
}
Exemple #3
0
bool rectIntersect(GRect *r1, GRect *r2)
{
    register bool xOverlap = valueInRange(r1->origin.x, r2->origin.x, r2->origin.x + r2->size.w - 1) ||
    valueInRange(r2->origin.x, r1->origin.x, r1->origin.x + r1->size.w - 1);
    
    register bool yOverlap = valueInRange(r1->origin.y, r2->origin.y, r2->origin.y + r2->size.h - 1) ||
    valueInRange(r2->origin.y, r1->origin.y, r1->origin.y + r1->size.h - 1);
    
    return xOverlap && yOverlap;
}
bool isRegionOverlapping(WebCLBuffer* srcBuffer, WebCLBuffer* destBuffer, const CCuint srcOffset, const CCuint dstOffset, const CCuint numBytes)
{
    if (!srcBuffer || !destBuffer)
        return false;

    if (srcBuffer->platformObject() != destBuffer->platformObject())
        return false;

    return valueInRange(dstOffset, srcOffset, (srcOffset + numBytes))
        || valueInRange(srcOffset, dstOffset, (dstOffset + numBytes));
}
Exemple #5
0
MTGCardInstance * CardDescriptor::match_or(MTGCardInstance * card)
{
    int found = 1;
    for (size_t i = 0; i < types.size(); i++)
    {
        found = 0;
        if (types[i] >= 0)
        {

            if (card->hasSubtype(types[i]) || (MTGAllCards::findType(card->getLCName(), false) == types[i]))
            {
                found = 1;
                break;
            }
        }
        else
        {
            if (!card->hasSubtype(-types[i]) && (MTGAllCards::findType(card->getLCName(), false) != -types[i]))
            {
                found = 1;
                break;
            }
        }
    }
    if (!found)
        return NULL;

    if (colors)
    {
        found = (colors & card->colors);
        if (!found)
            return NULL;
    }

    if (mColorExclusions)
    {
        found = mColorExclusions & card->colors;
        if (found)
            return NULL;
    }

    // Quantified restrictions are always AND-ed:
    if (powerComparisonMode && !valueInRange(powerComparisonMode, card->getPower(), power))
        return NULL;
    if (toughnessComparisonMode && !valueInRange(toughnessComparisonMode, card->getToughness(), toughness))
        return NULL;
    if (manacostComparisonMode && !valueInRange(manacostComparisonMode, card->getManaCost()->getConvertedCost(), convertedManacost))
        return NULL;
    if (nameComparisonMode && compareName != card->name)
        return NULL;
    return card;
}
void getIntensities()
{
  //sort the tree data
  Double_t gate_value[2];
  long long int entries;
  
  entries=gtree->GetEntries();
  printf("Number of entries in input tree: %Ld\n",entries);
  
  for (int i=0;i<entries;i++)
    {
      gtree->GetEntry(i);
      
      //printf("Values in gate leaf: %i\n",gateLeaf->GetNdata()); 
      
      if(use_gate_weights==true)//custom gates used with weights
        {
          if(use_custom_gates==1)//1D gate
            for(int j=0; j<gateLeaf->GetNdata(); j++) //deal with multiple fold events
              {
                gate_value[0] = gateLeaf->GetValue(j);
                for (int k=0;k<num_custom_gates;k++)
                  if(valueInRange(gate_value[0],custom_gates[k][0],custom_gates[k][1]))
                    numEv[k]++;
              }
            
          if(use_custom_gates==2)//2D gate
            for(int j=0; j<gateLeaf->GetNdata(); j++) //deal with multiple fold events
              for(int k=j+1; k<gateLeaf->GetNdata(); k++)
                {
                  gate_value[0] = gateLeaf->GetValue(j);
                  gate_value[1] = gateLeaf->GetValue(k);
                  for (int l=0;l<num_custom_gates;l++)
                    if((valueInRange(gate_value[0],custom_gates[l][0],custom_gates[l][1]))&&(valueInRange(gate_value[1],custom_gates[l][2],custom_gates[l][3])))
                      {
                        numEv[l]++;
                        break;
                      }
                    else if((valueInRange(gate_value[0],custom_gates[l][2],custom_gates[l][3]))&&(valueInRange(gate_value[1],custom_gates[l][0],custom_gates[l][1])))
                      {
                        numEv[l]++;
                        break;
                      }
                }
        }
    }
}
bool isRegionOverlapping(WebCLImage* source, WebCLImage* destination, const Vector<CCuint>& sourceOrigin,
    const Vector<CCuint>& destinationOrigin, const Vector<CCuint>& region)
{
    if (!source || !destination)
        return false;

    if (sourceOrigin.size() != 2 || destinationOrigin.size() != 2 || region.size() != 2)
        return false;

    if (source->platformObject() != destination->platformObject())
        return false;

    bool xOverlap = valueInRange(destinationOrigin[0], sourceOrigin[0], (region[0] + sourceOrigin[0]))
        || valueInRange(sourceOrigin[0], destinationOrigin[0], (destinationOrigin[0] + region[0]));
    bool yOverlap = valueInRange(destinationOrigin[1], sourceOrigin[1], (region[1] + sourceOrigin[1]))
        || valueInRange(sourceOrigin[1], destinationOrigin[1], (destinationOrigin[1] + region[1]));

    return xOverlap && yOverlap;
}
Exemple #8
0
MTGCardInstance * CardDescriptor::match_and(MTGCardInstance * card)
{
    MTGCardInstance * match = card;
    for (size_t i = 0; i < types.size(); i++)
    {
        if (types[i] >= 0)
        {
            if (!card->hasSubtype(types[i]) && !(MTGAllCards::findType(card->getLCName(), false) == types[i]))
            {
                match = NULL;
            }
        }
        else
        {
            if (card->hasSubtype(-types[i]) || (MTGAllCards::findType(card->getLCName(), false) == -types[i]))
            {
                match = NULL;
            }
        }
    }
    if ((colors & card->colors) != colors)
        match = NULL;

    if (mColorExclusions)
    {
        // if any of forbidden colors intersect with card colors
        if ((mColorExclusions & card->colors) != 0)
            match = NULL;
    }

    if (powerComparisonMode && !valueInRange(powerComparisonMode, card->getPower(), power))
        match = NULL;
    if (toughnessComparisonMode && !valueInRange(toughnessComparisonMode, card->getToughness(), toughness))
        match = NULL;
    if (manacostComparisonMode && !valueInRange(manacostComparisonMode, card->getManaCost()->getConvertedCost(), convertedManacost))
        match = NULL;
    if(nameComparisonMode && compareName != card->name)
        match = NULL;

    return match;
}
Exemple #9
0
int rectCollision(TRect A, TRect B, bool dontCalculate)
{
    bool xOverlap = valueInRange(A.left, B.left, B.left + B.right) ||
                    valueInRange(B.left, A.left, A.left + A.right);

    bool yOverlap = valueInRange(A.top, B.top, B.top + B.bottom) ||
                    valueInRange(B.top, A.top, A.top + A.bottom);

    if ((xOverlap == false) || (yOverlap == false))
        return NO_COLLISION;
    if (dontCalculate)
        return 1;

    TRect overlap;
    bool horizontal;
    overlap.left = valueInRange(A.left, B.left, B.left + B.right)?A.left:B.left;
    overlap.top  = valueInRange(A.top, B.top, B.top + B.bottom)?A.top:B.top;
    overlap.right= min(B.left+B.right,A.left+A.right);
    overlap.bottom = min(B.top+B.bottom, A.top+A.bottom);
    if (overlap.Width()<overlap.Height())
        horizontal = true;
    else
        horizontal = false;
    if (horizontal)
        if (A.left < B.left)
                return COLLISION_RIGHT;
        else
                return COLLISION_LEFT;
    else if (A.top < B.top)
                return COLLISION_DOWN;
         else
                return COLLISION_UP;
}
Exemple #10
0
MTGCardInstance * CardDescriptor::match(MTGCardInstance * card)
{

    MTGCardInstance * match = card;
    if (mode == CD_AND)
    {
        match = match_and(card);
    }
    else if (mode == CD_OR)
    {
        match = match_or(card);
    }

    //Abilities
    BasicAbilitiesSet set = basicAbilities & card->basicAbilities;
    if (set != basicAbilities)
        return NULL;
    
    BasicAbilitiesSet excludedSet = mAbilityExclusions & card->basicAbilities;
    if (excludedSet.any())
        return NULL;


    if ((tapped == -1 && card->isTapped()) || (tapped == 1 && !card->isTapped()))
    {
        match = NULL;
    }

    if ((fresh == -1 && card->fresh) || (fresh == 1 && !card->fresh))
    {
        match = NULL;
    }

    if ((isMultiColored == -1 && card->isMultiColored) || (isMultiColored == 1 && !card->isMultiColored))
    {
        match = NULL;
    }
        if ((isLeveler == -1 && card->isLeveler) || (isLeveler == 1 && !card->isLeveler))
        {
            match = NULL;
        }
        if ((CDenchanted == -1 && card->enchanted) || (CDenchanted == 1 && !card->enchanted))
        {
            match = NULL;
        }
        if ((CDdamaged == -1 && card->wasDealtDamage) || (CDdamaged == 1 && !card->wasDealtDamage))
        {
            match = NULL;
        }
    if(CDopponentDamaged == -1 || CDopponentDamaged == 1)
    {
        Player * p = card->controller()->opponent();//controller()->opponent();
        if ((CDopponentDamaged == -1 && card->damageToOpponent && card->controller() == p) || (CDopponentDamaged == 1 && !card->damageToOpponent && card->controller() == p)
            || (CDopponentDamaged == -1 && card->damageToController && card->controller() == p->opponent()) || (CDopponentDamaged == 1 && !card->damageToController && card->controller() == p->opponent()))
        {
            match = NULL;
        }
        if ((CDcontrollerDamaged == -1 && card->damageToController && card->controller() == p) || (CDcontrollerDamaged == 1 && !card->damageToController && card->controller() == p)
            || (CDcontrollerDamaged == -1 && card->damageToOpponent && card->controller() == p->opponent()) || (CDcontrollerDamaged == 1 && !card->damageToOpponent && card->controller() == p->opponent()))
        {
            match = NULL;
        }
    }
        if ((isToken == -1 && card->isToken) || (isToken == 1 && !card->isToken))
        {
            match = NULL;
        }
    if (attacker == 1)
    {
        if (defenser == &AnyCard)
        {
            if (!card->attacker && !card->defenser)
                match = NULL;
        }
        else
        {
            if (!card->attacker)
                match = NULL;
        }
    }
    else if (attacker == -1)
    {
        if (defenser == &NoCard)
        {
            if (card->attacker || card->defenser)
                match = NULL;
        }
        else
        {
            if (card->attacker)
                match = NULL;
        }
    }
    else
    {
        if (defenser == &NoCard)
        {
            if (card->defenser)
                match = NULL;
        }
        else if (defenser == &AnyCard)
        {
            if (!card->defenser)
                match = NULL;
        }
        else
        {
            // we don't care about the attack/blocker state
        }
    }

    //Counters
    if (anyCounter)
    {
        if (!(card->counters->mCount))
        {
            match = NULL;
        }
        else
        {
            int hasCounter = 0;
            for (int i = 0; i < card->counters->mCount; i++)
            {
                if (card->counters->counters[i]->nb > 0)
                    hasCounter = 1;
            }
            if (!hasCounter)
                match = NULL;
        }
    }
    else
    {
        if (counterComparisonMode)
        {
            Counter * targetCounter = card->counters->hasCounter(counterName.c_str(), counterPower, counterToughness);
            if (targetCounter)
            {
                if (!valueInRange(counterComparisonMode, targetCounter->nb, counterNB))
                    match = NULL;
            }
            else
            {
                if (counterComparisonMode != COMPARISON_LESS && counterComparisonMode != COMPARISON_AT_MOST)
                    match = NULL;
            }
        }
    }

    return match;
}
Exemple #11
0
 inline T valueInRange(const T value, const T range) {
     return valueInRange(value, -range, range);
 }
MTGCardInstance * CardDescriptor::match(MTGCardInstance * card)
{

    MTGCardInstance * match = card;
    if (mode == CD_AND)
    {
        match = match_and(card);
    }
    else if (mode == CD_OR)
    {
        match = match_or(card);
    }
	//Position in zone for reveal
	if ( zonePosition ) {
		vector< MTGCardInstance* > zoneCards = card->currentZone->cards;
		int nZoneCards = static_cast< int >( zoneCards.size() );
		bool posFound = false;
		for ( int i = 1; i <= zonePosition; i++ ) {
			if ( zoneCards[ nZoneCards - i ] == card ) {
				posFound = true;
				break;
			}
		}
		if ( !posFound ) {
			return NULL;
		}
	}
    //Abilities
    BasicAbilitiesSet set = basicAbilities & card->basicAbilities;
    if (set != basicAbilities)
        return NULL;
    
    BasicAbilitiesSet excludedSet = mAbilityExclusions & card->basicAbilities;
    if (excludedSet.any())
        return NULL;


    if ((tapped == -1 && card->isTapped()) || (tapped == 1 && !card->isTapped()))
    {
        match = NULL;
    }

    if ((fresh == -1 && card->fresh) || (fresh == 1 && !card->fresh))
    {
        match = NULL;
    }

    if ((entersBattlefield == -1 && card->entersBattlefield) || (entersBattlefield == 1 && !card->entersBattlefield))
    {
        match = NULL;
    }
    
    if ((CDgeared == -1 && card->equipment > 0) || (CDgeared == 1 && card->equipment < 1))
    {
        match = NULL;
    }
    
    if (CDblocked == -1)
    {
        if(!card->isAttacker())
            match = NULL;
        else
        {
            if(card->isBlocked())
                match = NULL;
        }
    }

    if (CDblocked == 1)
    {
        if(!card->isAttacker())
            match = NULL;
        else
        {
            if(!card->isBlocked())
                match = NULL;
        }
    }
    
    if ((CDcanProduceC == -1 && card->canproduceC == 1) || (CDcanProduceC == 1 && card->canproduceC == 0))
    {
        match = NULL;
    }
    
    if ((CDcanProduceG == -1 && card->canproduceG == 1) || (CDcanProduceG == 1 && card->canproduceG == 0))
    {
        match = NULL;
    }
    
    if ((CDcanProduceU == -1 && card->canproduceU == 1) || (CDcanProduceU == 1 && card->canproduceU == 0))
    {
        match = NULL;
    }
    
    if ((CDcanProduceR == -1 && card->canproduceR == 1) || (CDcanProduceR == 1 && card->canproduceR == 0))
    {
        match = NULL;
    }
    
    if ((CDcanProduceB == -1 && card->canproduceB == 1) || (CDcanProduceB == 1 && card->canproduceB == 0))
    {
        match = NULL;
    }
    
    if ((CDcanProduceW == -1 && card->canproduceW == 1) || (CDcanProduceW == 1 && card->canproduceW == 0))
    {
        match = NULL;
    }
    
    if ((CDnocolor == -1 && card->getColor() == 0))
    {
        match = NULL;
    }
    else if(CDnocolor == 1)
    {
        if(!card->has(Constants::DEVOID))
            if(card->getColor()>0)
                match = NULL;
    }

    if ((isMultiColored == -1 && card->isMultiColored) || (isMultiColored == 1 && !card->isMultiColored))
    {
        match = NULL;
    }
        if ((isLeveler == -1 && card->isLeveler) || (isLeveler == 1 && !card->isLeveler))
        {
            match = NULL;
        }
        if ((CDenchanted == -1 && card->enchanted) || (CDenchanted == 1 && !card->enchanted))
        {
            match = NULL;
        }
        if ((CDdamaged == -1 && card->wasDealtDamage) || (CDdamaged == 1 && !card->wasDealtDamage))
        {
            match = NULL;
        }

        if ((CDdamager == -1 && (card->damageToOpponent || card->damageToController || card->damageToCreature)) 
                || (CDdamager == 1 && !(card->damageToOpponent || card->damageToController || card->damageToCreature)))
        {
            match = NULL;
        }

    if(CDopponentDamaged == -1 || CDopponentDamaged == 1)
    {
        Player * p = card->controller()->opponent();//controller()->opponent();
        if ((CDopponentDamaged == -1 && card->damageToOpponent && card->controller() == p)
            || (CDopponentDamaged == 1 && !card->damageToOpponent && card->controller() == p)
            || (CDopponentDamaged == -1 && card->damageToController && card->controller() == p->opponent())
            || (CDopponentDamaged == 1 && !card->damageToController && card->controller() == p->opponent()))
        {
            match = NULL;
        }
        if ((CDcontrollerDamaged == -1 && card->damageToController && card->controller() == p)
            || (CDcontrollerDamaged == 1 && !card->damageToController && card->controller() == p)
            || (CDcontrollerDamaged == -1 && card->damageToOpponent && card->controller() == p->opponent())
            || (CDcontrollerDamaged == 1 && !card->damageToOpponent && card->controller() == p->opponent()))
        {
            match = NULL;
        }
    }
        if ((isToken == -1 && card->isToken) || (isToken == 1 && !card->isToken))
        {
            match = NULL;
        }
    if (attacker == 1)
    {
        if (defenser == &AnyCard)
        {
            if (!card->attacker && !card->defenser)
                match = NULL;
        }
        else
        {
            if (!card->attacker)
                match = NULL;
        }
    }
    else if (attacker == -1)
    {
        if (defenser == &NoCard)
        {
            if (card->attacker || card->defenser)
                match = NULL;
        }
        else
        {
            if (card->attacker)
                match = NULL;
        }
    }
    else
    {
        if (defenser == &NoCard)
        {
            if (card->defenser)
                match = NULL;
        }
        else if (defenser == &AnyCard)
        {
            if (!card->defenser)
                match = NULL;
        }
        else
        {
            // we don't care about the attack/blocker state
        }
    }

    //Counters
    if (anyCounter)
    {
        if (!(card->counters->mCount))
        {
            match = NULL;
        }
        else
        {
            int hasCounter = 0;
            for (int i = 0; i < card->counters->mCount; i++)
            {
                if (card->counters->counters[i]->nb > 0)
                    hasCounter = 1;
            }
            if (!hasCounter)
                match = NULL;
        }
    }
    else
    {
        if (counterComparisonMode)
        {
            Counter * targetCounter = card->counters->hasCounter(counterName.c_str(), counterPower, counterToughness);
            if (targetCounter)
            {
                if (!valueInRange(counterComparisonMode, targetCounter->nb, counterNB))
                    match = NULL;
            }
            else
            {
                if (counterComparisonMode != COMPARISON_LESS && counterComparisonMode != COMPARISON_AT_MOST)
                    match = NULL;
            }
        }
    }

    return match;
}
Exemple #13
0
int main(int argc, char *argv[])
{

  FILE *list;
  TFile *inp;
  randGen = new TRandom3();

  if(argc!=2)
    {
      printf("\ntree2tree parameter_file\n");
      printf("-----------------------------\nSeparates the ROOT tree specified in the parameter file into a ROOT tree containing a subset of the original data, according to the sort scheme specified in the parameter file.\n\n");
      exit(-1);
    }

  readConfigFile(argv[1],"tree2tree"); //grab data from the parameter file
  
  //sort list of ROOT files
  if(listMode==true)
    { 
      //read in tree list file
      if((list=fopen(inp_filename,"r"))==NULL)
        {
          printf("ERROR: Cannot open the input list file %s!\n",inp_filename);
          exit(-1);
        } 
      //scan the list file for ROOT files and put their data into the output hitogram
      while(fscanf(list,"%s",str)!=EOF)
        {
  
          inp = new TFile(str,"read");
          if((gtree = (TTree*)inp->Get(gate_tree_name))==NULL)
            {
              printf("The specified tree named %s doesn't exist, trying default name 'tree'.\n",gate_tree_name);
              if((gtree = (TTree*)inp->Get("tree"))==NULL)//try the default tree name
                {
                  printf("ERROR: The specified tree named %s (within the ROOT file) cannot be opened!\n",gate_tree_name);
                  exit(-1);
                }
            }
          printf("Tree in %s read out.\n",inp_filename);

          if((gateLeaf = gtree->GetLeaf(gate_path))==NULL)
            if((gateBranch = gtree->GetBranch(gate_path))==NULL)
              {
                printf("ERROR: Gate data path '%s' doesn't correspond to a branch or leaf in the tree!\n",gate_path);
                exit(-1);
              }
          if(gateLeaf==NULL)
            gateLeaf = (TLeaf*)gateBranch->GetListOfLeaves()->First(); //get the first leaf from the specified branch
          printf("Path to gate data set.\n");
          
          //set up the output file (must be done before setting up the output tree)
          TFile *f;   
          if(output_specified==true)
            {
              char tmp[256];
              strcpy(tmp,out_filename);
              f = new TFile(strcat(tmp,str),"recreate");
            }
          else
            f = new TFile(strcat(str,"_out"),"recreate");
          TTree *sortedTree = gtree->CloneTree(0);
          printf("Output tree set up.\n");
          
          //sort the tree data
          Double_t gate_value[2];
          long long int entries;
          bool dropFlag=true;
          
          entries=gtree->GetEntries();
          printf("Number of entries in input tree: %Ld\n",entries);
          
          for (int i=0;i<entries;i++)
            {
              gtree->GetEntry(i);
              
              //printf("Values in gate leaf: %i\n",gateLeaf->GetNdata()); 
              
              dropFlag=true;
              if(use_gate_weights==true)//custom gates used with weights
                {
                  if(use_custom_gates==1)//1D gate
                    for(int j=0; j<gateLeaf->GetNdata(); j++) //deal with multiple fold events
                      {
                        gate_value[0] = gateLeaf->GetValue(j);
                        for (int k=0;k<num_custom_gates;k++)
                          if(valueInRange(gate_value[0],custom_gates[k][0],custom_gates[k][1]))
                            if(randGen->Uniform()<gate_weight[k])
                              dropFlag=false;
                      }
                  
                  if(use_custom_gates==2)//2D gate
                    for(int j=0; j<gateLeaf->GetNdata(); j++) //deal with multiple fold events
                      for(int k=j+1; k<gateLeaf->GetNdata(); k++)
                        {
                          gate_value[0] = gateLeaf->GetValue(j);
                          gate_value[1] = gateLeaf->GetValue(k);
                          for (int l=0;l<num_custom_gates;l++)
                            if((valueInRange(gate_value[0],custom_gates[l][0],custom_gates[l][1]))&&(valueInRange(gate_value[1],custom_gates[l][2],custom_gates[l][3])))
                              {
                                if(randGen->Uniform()<gate_weight[l])
                                  dropFlag=false;
                                break;
                              }
                            else if((valueInRange(gate_value[0],custom_gates[l][2],custom_gates[l][3]))&&(valueInRange(gate_value[1],custom_gates[l][0],custom_gates[l][1])))
                              {
                                if(randGen->Uniform()<gate_weight[l])
                                  dropFlag=false;
                                break;
                              }
                        }
                    
                }
              
              if(dropFlag==false)
                sortedTree->Fill();//keep the event
            }
            
          printf("Number of entries retained in output tree: %Ld\n",sortedTree->GetEntries());
          f->Write();
          delete f;//close the output file  
            
            
        }
    }
  
  
  
  //sort a single ROOT file  
  if(listMode==false)
    {
      //read in tree file
      inp = new TFile(inp_filename,"read");
      if((gtree = (TTree*)inp->Get(gate_tree_name))==NULL)
        {
          printf("The specified tree named %s doesn't exist, trying default name 'tree'.\n",gate_tree_name);
          if((gtree = (TTree*)inp->Get("tree"))==NULL)//try the default tree name
            {
              printf("ERROR: The specified tree named %s (within the ROOT file) cannot be opened!\n",gate_tree_name);
              exit(-1);
            }
        }
      printf("Tree in %s read out.\n",inp_filename);

      if((gateLeaf = gtree->GetLeaf(gate_path))==NULL)
        if((gateBranch = gtree->GetBranch(gate_path))==NULL)
          {
            printf("ERROR: Gate data path '%s' doesn't correspond to a branch or leaf in the tree!\n",gate_path);
            exit(-1);
          }
      if(gateLeaf==NULL)
        gateLeaf = (TLeaf*)gateBranch->GetListOfLeaves()->First(); //get the first leaf from the specified branch
      printf("Path to gate data set.\n");
      
      //set up the output file (must be done before setting up the output tree)
      TFile *f;
      if(output_specified==true)
        f = new TFile(out_filename,"recreate");
      else
        f = new TFile(strcat(inp_filename,"_out.root"),"recreate");
      TTree *sortedTree = gtree->CloneTree(0);
      printf("Output tree set up.\n");
      
      //sort the tree data
      Double_t gate_value[2];
      long long int entries;
      bool dropFlag=true;
      
      entries=gtree->GetEntries();
      printf("Number of entries in input tree: %Ld\n",entries);
      
      for (int i=0;i<entries;i++)
        {
          gtree->GetEntry(i);
          
          //printf("Values in gate leaf: %i\n",gateLeaf->GetNdata()); 
          
          dropFlag=true;
          if(use_gate_weights==true)//custom gates used with weights
            {
              if(use_custom_gates==1)//1D gate
                for(int j=0; j<gateLeaf->GetNdata(); j++) //deal with multiple fold events
                  {
                    gate_value[0] = gateLeaf->GetValue(j);
                    for (int k=0;k<num_custom_gates;k++)
                      if(valueInRange(gate_value[0],custom_gates[k][0],custom_gates[k][1]))
                        if(randGen->Uniform()<gate_weight[k])
                          dropFlag=false;
                  }
              
              if(use_custom_gates==2)//2D gate
                for(int j=0; j<gateLeaf->GetNdata(); j++) //deal with multiple fold events
                  for(int k=j+1; k<gateLeaf->GetNdata(); k++)
                    {
                      gate_value[0] = gateLeaf->GetValue(j);
                      gate_value[1] = gateLeaf->GetValue(k);
                      for (int l=0;l<num_custom_gates;l++)
                        if((valueInRange(gate_value[0],custom_gates[l][0],custom_gates[l][1]))&&(valueInRange(gate_value[1],custom_gates[l][2],custom_gates[l][3])))
                          {
                            if(randGen->Uniform()<gate_weight[l])
                              dropFlag=false;
                            break;
                          }
                        else if((valueInRange(gate_value[0],custom_gates[l][2],custom_gates[l][3]))&&(valueInRange(gate_value[1],custom_gates[l][0],custom_gates[l][1])))
                          {
                            if(randGen->Uniform()<gate_weight[l])
                              dropFlag=false;
                            break;
                          }
                    }
                
            }
          
          if(dropFlag==false)
            sortedTree->Fill();//keep the event
        }
        
      printf("Number of entries retained in output tree: %Ld\n",sortedTree->GetEntries());
      f->Write();
      delete f;//close the output file
    }
    
  return 0; //great success
}