CMS 3D CMS Logo

Public Member Functions | Private Types | Private Member Functions | Private Attributes

MultiTrajectoryStateAssembler Class Reference

#include <MultiTrajectoryStateAssembler.h>

List of all members.

Public Member Functions

void addInvalidState (const double)
 Adds (the weight of an) invalid state to the list.
void addState (const TrajectoryStateOnSurface)
TrajectoryStateOnSurface combinedState ()
TrajectoryStateOnSurface combinedState (const float weight)
 MultiTrajectoryStateAssembler ()

Private Types

typedef std::vector
< TrajectoryStateOnSurface
MultiTSOS
typedef TrajectoryStateOnSurface TSOS

Private Member Functions

void addStateVector (const MultiTSOS &)
bool invalidCombinedState () const
 Checks status of combined state.
bool prepareCombinedState ()
 Preparation of combined state (cleaning & sorting)
void removeSmallWeights ()
void removeWrongPz ()
 Removes states with local p_z != average p_z.
TrajectoryStateOnSurface reweightedCombinedState (const double) const

Private Attributes

bool combinationDone
float minFractionalWeight
float minValidFraction
bool sortStates
double theInvalidWeightSum
bool thePzError
MultiTSOS theStates
double theValidWeightSum

Detailed Description

Collects trajectory states and returns a MultiTrajectoryState.

Definition at line 13 of file MultiTrajectoryStateAssembler.h.


Member Typedef Documentation

Definition at line 17 of file MultiTrajectoryStateAssembler.h.

Definition at line 16 of file MultiTrajectoryStateAssembler.h.


Constructor & Destructor Documentation

MultiTrajectoryStateAssembler::MultiTrajectoryStateAssembler ( )

Definition at line 8 of file MultiTrajectoryStateAssembler.cc.

References minFractionalWeight, minValidFraction, and sortStates.

                                                              :
  combinationDone(false),
  thePzError(false),
  theValidWeightSum(0.),
  theInvalidWeightSum(0.)
{
  //
  // parameters (could be configurable)
  //
  sortStates = false;
  minValidFraction = 0.01;
  minFractionalWeight = 1.e-6;
  //   //
  //   // Timers
  //   //
  //   if ( theTimerAdd==0 ) {
  //     theTimerAdd = 
  //       &(*TimingReport::current())[string("MultiTrajectoryStateAssembler::addState")]; 
  //     theTimerAdd->switchCPU(false);
  //     theTimerComb = 
  //       &(*TimingReport::current())[string("MultiTrajectoryStateAssembler::combinedState")]; 
  //     theTimerComb->switchCPU(false);
  //   }
}  

Member Function Documentation

void MultiTrajectoryStateAssembler::addInvalidState ( const double  weight)

Adds (the weight of an) invalid state to the list.

Definition at line 87 of file MultiTrajectoryStateAssembler.cc.

References theInvalidWeightSum, and CommonMethods::weight().

                                                                        {
  //
  // change status of combination (contains at least one invalid state)
  //
  theInvalidWeightSum += weight;
}
void MultiTrajectoryStateAssembler::addState ( const TrajectoryStateOnSurface  tsos)

Adds a new TrajectoryStateOnSurface to the list of components

Definition at line 33 of file MultiTrajectoryStateAssembler.cc.

References addStateVector(), combinationDone, TrajectoryStateOnSurface::components(), Exception, and TrajectoryStateOnSurface::isValid().

Referenced by GsfMultiStateUpdator::update(), and GsfMaterialEffectsUpdator::updateState().

                                                                                 {
  //   // Timer
  //   TimeMe t(*theTimerAdd,false);
  //
  // refuse to add states after combination has been done
  //
  if ( combinationDone )
    throw cms::Exception("LogicError") 
      << "MultiTrajectoryStateAssembler: trying to add states after combination";
  //
  // Verify validity of state to be added
  //
  if ( !tsos.isValid() )
    throw cms::Exception("LogicError") << "MultiTrajectoryStateAssembler: trying to add invalid state";
  //
  // Add components (i.e. state to be added can be single or multi state)
  //
  MultiTSOS components(tsos.components());
  addStateVector(components);
}
void MultiTrajectoryStateAssembler::addStateVector ( const MultiTSOS states) [private]

Adds a vector of trajectory states to the list of components

Definition at line 54 of file MultiTrajectoryStateAssembler.cc.

References combinationDone, Exception, i, thePzError, theStates, and theValidWeightSum.

Referenced by addState().

{
  //
  // refuse to add states after combination has been done
  //
  if ( combinationDone )
    throw cms::Exception("LogicError") 
      << "MultiTrajectoryStateAssembler: trying to add states after combination";
  //
  // sum up weights (all components are supposed to be valid!!!) and
  // check for consistent pz
  //
  double sum(0.);
  double pzFirst = theStates.empty() ? 0. : theStates.front().localParameters().pzSign();
  for ( MultiTSOS::const_iterator i=states.begin();
        i!=states.end(); i++ ) {
    if ( !(i->isValid()) )
      throw cms::Exception("LogicError") 
        << "MultiTrajectoryStateAssembler: trying to add invalid state";
    // weights
    sum += i->weight();
    // check on p_z
    if ( !theStates.empty() && 
         pzFirst*i->localParameters().pzSign()<0. )  thePzError = true;
  }
  theValidWeightSum += sum;
  //
  // add to vector of states
  //
  theStates.insert(theStates.end(),states.begin(),states.end());
}
TrajectoryStateOnSurface MultiTrajectoryStateAssembler::combinedState ( const float  weight)

Returns the resulting MultiTrajectoryState renormalised to specified weight.

Definition at line 112 of file MultiTrajectoryStateAssembler.cc.

References prepareCombinedState(), and reweightedCombinedState().

                                                                                            {
  //   // Timer
  //   TimeMe t(*theTimerComb,false);
  //
  // Prepare resulting state vector
  //
  if ( !prepareCombinedState() )  return TSOS();
  //
  // return reweighted state
  //
  return reweightedCombinedState(newWeight);
}
TrajectoryStateOnSurface MultiTrajectoryStateAssembler::combinedState ( )

Returns the resulting MultiTrajectoryState with weight = sum of all valid components.

Definition at line 94 of file MultiTrajectoryStateAssembler.cc.

References prepareCombinedState(), reweightedCombinedState(), theInvalidWeightSum, theStates, and theValidWeightSum.

Referenced by GsfMultiStateUpdator::update(), and GsfMaterialEffectsUpdator::updateState().

                                                                       {
  //   // Timer
  //   TimeMe t(*theTimerComb,false);
  //
  // Prepare resulting state vector
  //
  if ( !prepareCombinedState() )  return TSOS();
  //
  // If invalid states in input: use reweighting
  //
  if ( theInvalidWeightSum>0. )  
    return reweightedCombinedState(theValidWeightSum+theInvalidWeightSum);
  //
  // Return new multi state without reweighting
  //
  return TSOS(new BasicMultiTrajectoryState(theStates));
}
bool MultiTrajectoryStateAssembler::invalidCombinedState ( ) const [inline, private]

Checks status of combined state.

Definition at line 49 of file MultiTrajectoryStateAssembler.h.

References theStates.

Referenced by prepareCombinedState(), and reweightedCombinedState().

  {
    //
    // Protect against empty combination (no valid input state)
    //
    return theStates.empty();
  }
bool MultiTrajectoryStateAssembler::prepareCombinedState ( ) [private]

Preparation of combined state (cleaning & sorting)

Definition at line 126 of file MultiTrajectoryStateAssembler.cc.

References combinationDone, invalidCombinedState(), minValidFraction, removeSmallWeights(), removeWrongPz(), python::multivaluedict::sort(), sortStates, theInvalidWeightSum, thePzError, theStates, and theValidWeightSum.

Referenced by combinedState().

                                                     {
  //
  // Protect against empty combination (no valid input state)
  //
  if ( invalidCombinedState() )  return false;
  //
  // Check for states with wrong pz
  //
  if ( thePzError )  removeWrongPz();
  //
  // Check for minimum fraction of valid states
  //
  double allWeights(theValidWeightSum+theInvalidWeightSum);
  if ( theInvalidWeightSum>0. && (theValidWeightSum/allWeights)<minValidFraction )  return false;
  //
  // remaining part to be done only once
  //
  if ( combinationDone )  return true;
  else  combinationDone = true;
  //
  // Remove states with negligible weights
  //
  removeSmallWeights();
  if ( invalidCombinedState() )  return false;
  //
  // Sort output by weights?
  //
  if ( sortStates ) 
    sort(theStates.begin(),theStates.end(),TrajectoryStateLessWeight());

  return true;
}
void MultiTrajectoryStateAssembler::removeSmallWeights ( ) [private]

Removes states with negligible weight (no renormalisation of total weight!).

Definition at line 188 of file MultiTrajectoryStateAssembler.cc.

References i, minFractionalWeight, theInvalidWeightSum, theStates, and theValidWeightSum.

Referenced by prepareCombinedState().

{
  //
  // check total weight
  //
  double totalWeight(theInvalidWeightSum+theValidWeightSum);
  if ( totalWeight == 0. ) {
    theStates.clear();
    return;
  }
  //
  // Loop until no more states are removed
  //
  bool redo;
  do {
    redo = false;
    for ( MultiTSOS::iterator i=theStates.begin();
          i!=theStates.end(); i++ ) {
      if ( (*i).weight()/totalWeight < minFractionalWeight ) {
        theStates.erase(i);
        redo = true;
        break;
      }
    }
  } while (redo);
}
void MultiTrajectoryStateAssembler::removeWrongPz ( ) [private]

Removes states with local p_z != average p_z.

Definition at line 216 of file MultiTrajectoryStateAssembler.cc.

References theInvalidWeightSum, theStates, and theValidWeightSum.

Referenced by prepareCombinedState().

                                              {
  //   edm::LogDebug("MultiTrajectoryStateAssembler") 
  //     << "MultiTrajectoryStateAssembler: found at least one state with inconsistent pz\n"
  //     << "  #state / weights before cleaning = " << theStates.size()
  //     << " / " << theValidWeightSum
  //     << " / " << theInvalidWeightSum;
  //
  // Calculate average pz
  //
  double meanPz(0.);
  for ( MultiTSOS::const_iterator is=theStates.begin();
        is!=theStates.end(); is++ ) {
    meanPz += is->weight()*is->localParameters().pzSign();
    //     edm::LogDebug("MultiTrajectoryStateAssembler") 
    //       << "  weight / pz / global position = " << is->weight() 
    //       << " " << is->localParameters().pzSign() 
    //       << " " << is->globalPosition();
  }
  meanPz /= theValidWeightSum;
  //
  // Now keep only states compatible with the average pz
  //
  //   double oldValidWeight(theValidWeightSum);
  theValidWeightSum = 0.;
  MultiTSOS oldStates(theStates);
  theStates.clear();
  for ( MultiTSOS::const_iterator is=oldStates.begin();
        is!=oldStates.end(); is++ ) {
    if ( meanPz*is->localParameters().pzSign()>=0. ) {
      theValidWeightSum += is->weight();
      theStates.push_back(*is);
    }
    else {
      theInvalidWeightSum += is->weight();
    }
  }
  //   edm::LogDebug("MultiTrajectoryStateAssembler") 
  //     << "  #state / weights after cleaning = " << theStates.size()
  //     << " / " << theValidWeightSum
  //     << " / " << theInvalidWeightSum;
}
TrajectoryStateOnSurface MultiTrajectoryStateAssembler::reweightedCombinedState ( const double  newWeight) const [private]

Returns the resulting MultiTrajectoryState with user-supplied total weight.

Definition at line 160 of file MultiTrajectoryStateAssembler.cc.

References i, invalidCombinedState(), theStates, and theValidWeightSum.

Referenced by combinedState().

                                                                                    {
  //
  // check status
  //
  if ( invalidCombinedState() )  return TSOS();
  //
  // scaling factor
  //
  double factor = theValidWeightSum>0. ? newWeight/theValidWeightSum : 1;
  //
  // create new vector of states & combined state
  //
  MultiTSOS reweightedStates;
  reweightedStates.reserve(theStates.size());
  for ( MultiTSOS::const_iterator i=theStates.begin();
        i!=theStates.end(); i++ ) {
    double oldWeight = i->weight();
    reweightedStates.push_back(TrajectoryStateOnSurface(i->localParameters(),
                                                        i->localError(),
                                                        i->surface(),
                                                        &(i->globalParameters().magneticField()),
                                                        i->surfaceSide(),
                                                        factor*oldWeight));
  }
  return TSOS(new BasicMultiTrajectoryState(reweightedStates));
}

Member Data Documentation

Definition at line 74 of file MultiTrajectoryStateAssembler.h.

Referenced by addState(), addStateVector(), and prepareCombinedState().

Definition at line 75 of file MultiTrajectoryStateAssembler.h.

Referenced by addStateVector(), and prepareCombinedState().