CMS 3D CMS Logo

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

fwlite::MultiChainEvent Class Reference

#include <MultiChainEvent.h>

Inheritance diagram for fwlite::MultiChainEvent:
fwlite::EventBase edm::EventBase

List of all members.

Public Types

typedef std::pair
< edm::EventID, edm::EventID
event_id_range
typedef std::map< edm::EventID,
Long64_t > 
sec_file_index_map
typedef std::map
< event_id_range, Long64_t > 
sec_file_range_index_map

Public Member Functions

bool atEnd () const
virtual edm::EventAuxiliary const & eventAuxiliary () const
Long64_t eventIndex () const
Long64_t eventIndexSec () const
virtual Long64_t fileIndex () const
const std::vector
< edm::BranchDescription > & 
getBranchDescriptions () const
virtual const std::string getBranchNameFor (const std::type_info &, const char *, const char *, const char *) const
virtual bool getByLabel (const std::type_info &, const char *, const char *, const char *, void *) const
edm::EDProduct const * getByProductID (edm::ProductID const &) const
fwlite::LuminosityBlock const & getLuminosityBlock ()
const std::vector< std::string > & getProcessHistory () const
fwlite::Run const & getRun ()
TFile * getTFile () const
TFile * getTFileSec () const
bool isValid () const
 MultiChainEvent (const std::vector< std::string > &iFileNames1, const std::vector< std::string > &iFileNames2, bool useSecFileMapSorted=false)
 operator bool () const
const MultiChainEventoperator++ ()
ChainEvent const * primary () const
edm::ProcessHistory const & processHistory () const
ChainEvent const * secondary () const
virtual Long64_t secondaryFileIndex () const
Long64_t size () const
bool to (Long64_t iIndex)
 Go to the event at index iIndex.
bool to (edm::EventID id)
 Go to event with event id "id".
bool to (edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event)
 Go to event with given run , lumi (if non-zero), and event number.
bool to (edm::RunNumber_t run, edm::EventNumber_t event)
 Go to event with given run and event number.
const MultiChainEventtoBegin ()
virtual edm::TriggerNames const & triggerNames (edm::TriggerResults const &triggerResults) const
virtual edm::TriggerResultsByName triggerResultsByName (std::string const &process) const
virtual ~MultiChainEvent ()

Static Public Member Functions

static void throwProductNotFoundException (const std::type_info &, const char *, const char *, const char *)

Private Member Functions

 MultiChainEvent (const Event &)
const MultiChainEventoperator= (const Event &)
bool toSec (edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event)
 Go to event with given run, lumi, and event number.
bool toSec (const edm::EventID &id)
bool toSec (Long64_t iIndex)
 Go to the event from secondary files at index iIndex.
bool toSec (edm::RunNumber_t run, edm::EventNumber_t event)
 Go to event with given run and event number.

Private Attributes

boost::shared_ptr< ChainEventevent1_
boost::shared_ptr< ChainEventevent2_
boost::shared_ptr
< internal::MultiProductGetter
getter_
sec_file_range_index_map secFileMapSorted_
bool useSecFileMapSorted_

Detailed Description

Definition at line 50 of file MultiChainEvent.h.


Member Typedef Documentation

Definition at line 56 of file MultiChainEvent.h.

Definition at line 55 of file MultiChainEvent.h.

Definition at line 57 of file MultiChainEvent.h.


Constructor & Destructor Documentation

MultiChainEvent::MultiChainEvent ( const std::vector< std::string > &  iFileNames1,
const std::vector< std::string > &  iFileNames2,
bool  useSecFileMapSorted = false 
)

Definition at line 54 of file MultiChainEvent.cc.

References gather_cfg::cout, event1_, event2_, getter_, secFileMapSorted_, and useSecFileMapSorted_.

{
  event1_ = boost::shared_ptr<ChainEvent> ( new ChainEvent( iFileNames1 ) );
  event2_ = boost::shared_ptr<ChainEvent> ( new ChainEvent( iFileNames2 ) );

  getter_ = boost::shared_ptr<internal::MultiProductGetter>( new internal::MultiProductGetter( this) );

  event1_->setGetter( getter_ );
  event2_->setGetter( getter_ );

  useSecFileMapSorted_ = useSecFileMapSorted;

  if ( !useSecFileMapSorted_ ) {
    std::cout << "------------------------------------------------------------------------" << std::endl;
    std::cout << "WARNING! What you are about to do may be very slow." << std::endl;
    std::cout << "The 2-file solution in FWLite works with very simple assumptions." << std::endl;
    std::cout << "It will linearly search through the files in the secondary file list for Products." << std::endl;
    std::cout << "There are speed improvements available to make this run faster." << std::endl;
    std::cout << "***If your secondary files are sorted with a run-range within a file, (almost always the case) " << std::endl;
    std::cout << "***please use the option useSecFileMapSorted=true in this constructor. " << std::endl;
    std::cout << "    > usage: MultiChainEvent( primaryFiles, secondaryFiles, true);" << std::endl;
    std::cout << "------------------------------------------------------------------------" << std::endl;    
    
  }

  if ( useSecFileMapSorted_ ) {

    std::cout << "------------------------------------------------------------------------" << std::endl;    
    std::cout << "This MultiChainEvent is now creating a (run_range)_2 ---> file_index_2 map" << std::endl;
    std::cout << "for the 2-file solution. " << std::endl;
    std::cout << "This is assuming the files you are giving me are sorted by run,event pairs within each secondary file." << std::endl;
    std::cout << "If this is not true (rarely the case), set this option to false." << std::endl;
    std::cout << "    > usage: MultiChainEvent( primaryFiles, secondaryFiles, false);" << std::endl;
    std::cout << "------------------------------------------------------------------------" << std::endl;    
    // speed up secondary file access with a (run,event)_1 ---> index_2 map
    

    // Loop over events, when a new file is encountered, store the first run number from this file,
    // and the last run number from the last file. 
    TFile * lastFile = 0;
    std::pair<event_id_range,Long64_t> eventRange;
    bool firstFile = true;

    bool foundAny = false;

    for( event2_->toBegin();
         ! event2_->atEnd();
         ++(*event2_)) {
      // if we have a new file, cache the "first"
      if ( lastFile != event2_->getTFile() ) {

        // if this is not the first file, we have an entry.
        // Add it to the list.
        if ( !firstFile ) {
          foundAny = true;
          event_id_range toAdd = eventRange.first;
          secFileMapSorted_[ toAdd ] = eventRange.second;
        }
        // always add the "first" event id to the cached event range
        eventRange.first.first = event2_->event()->id();
        lastFile = event2_->getTFile();
      }
      // otherwise, cache the "second" event id in the cached event range.
      // Upon the discovery of a new file, this will be used as the
      // "last" event id in the cached event range. 
      else {
        eventRange.first.second = event2_->event()->id();
        eventRange.second = event2_->eventIndex();
      }
      firstFile = false;
    }
    // due to the invailability of a "look ahead" operation, we have one additional "put" to make
    // after the loop (which puts the "last" event, not "this" event.
    if ( foundAny ) {
      event_id_range toAdd = eventRange.first;
      secFileMapSorted_[ toAdd ] = eventRange.second;
    }
//     std::cout << "Dumping run range to event id list:" << std::endl;
//     for ( sec_file_range_index_map::const_iterator mBegin = secFileMapSorted_.begin(),
//          mEnd = secFileMapSorted_.end(),
//          mit = mBegin;
//        mit != mEnd; ++mit ) {
//       char buff[1000];
//       event2_->to( mit->second );
//       sprintf(buff, "[%10d,%10d - %10d,%10d] ---> %10d",
//            mit->first.first.run(),
//            mit->first.first.event(),
//            mit->first.second.run(),
//            mit->first.second.event(),
//            mit->second );
//       std::cout << buff << std::endl;
//     }
  }

}
MultiChainEvent::~MultiChainEvent ( ) [virtual]

Definition at line 157 of file MultiChainEvent.cc.

{
}
fwlite::MultiChainEvent::MultiChainEvent ( const Event ) [private]

Member Function Documentation

bool MultiChainEvent::atEnd ( ) const [virtual]

Implements fwlite::EventBase.

Definition at line 384 of file MultiChainEvent.cc.

References event1_.

{
  return event1_->atEnd();
}
edm::EventAuxiliary const & MultiChainEvent::eventAuxiliary ( ) const [virtual]

Implements edm::EventBase.

Definition at line 336 of file MultiChainEvent.cc.

References event1_.

{
   return event1_->eventAuxiliary();
}
Long64_t fwlite::MultiChainEvent::eventIndex ( ) const [inline]

Definition at line 108 of file MultiChainEvent.h.

References event1_.

{ return event1_->eventIndex(); }
Long64_t fwlite::MultiChainEvent::eventIndexSec ( ) const [inline]

Definition at line 109 of file MultiChainEvent.h.

References event2_.

{ return event2_->eventIndex(); }
virtual Long64_t fwlite::MultiChainEvent::fileIndex ( ) const [inline, virtual]

Reimplemented from fwlite::EventBase.

Definition at line 120 of file MultiChainEvent.h.

References event1_.

      { return event1_->eventIndex(); }
const std::vector< edm::BranchDescription > & MultiChainEvent::getBranchDescriptions ( ) const

Definition at line 318 of file MultiChainEvent.cc.

References event1_.

{
  return event1_->getBranchDescriptions();
}
const std::string MultiChainEvent::getBranchNameFor ( const std::type_info &  iType,
const char *  iModule,
const char *  iInstance,
const char *  iProcess 
) const [virtual]

Implements fwlite::EventBase.

Definition at line 309 of file MultiChainEvent.cc.

References event1_.

{
  return event1_->getBranchNameFor(iType,iModule,iInstance,iProcess);
}
bool MultiChainEvent::getByLabel ( const std::type_info &  iType,
const char *  iModule,
const char *  iInstance,
const char *  iProcess,
void *  iValue 
) const [virtual]

This function should only be called by fwlite::Handle<>

Implements fwlite::EventBase.

Definition at line 342 of file MultiChainEvent.cc.

References event1_, and event2_.

{
  bool ret1 = event1_->getByLabel(iType,iModule,iInstance,iProcess,iValue);
  if ( !ret1 ) {
    (const_cast<MultiChainEvent*>(this))->toSec(event1_->id());
    bool ret2 = event2_->getByLabel(iType,iModule,iInstance,iProcess,iValue);
    if ( !ret2 ) return false;
  }
  return true;
}
edm::EDProduct const * MultiChainEvent::getByProductID ( edm::ProductID const &  iID) const

Definition at line 357 of file MultiChainEvent.cc.

References event1_, event2_, Exception, and parseEventContent::prod.

Referenced by fwlite::internal::MultiProductGetter::getIt().

{
  // First try the first file
  edm::EDProduct const * prod = event1_->getByProductID(iID);
  // Did not find the product, try secondary file
  if ( 0 == prod ) {
    (const_cast<MultiChainEvent*>(this))->toSec(event1_->id());
    prod = event2_->getByProductID(iID);
    if ( 0 == prod ) {
      throw cms::Exception("ProductNotFound") << "Cannot find product " << iID;
    }
  }
  return prod;
}
fwlite::LuminosityBlock const& fwlite::MultiChainEvent::getLuminosityBlock ( ) [inline]

Definition at line 111 of file MultiChainEvent.h.

References event1_.

                                                        {
        return event1_->getLuminosityBlock();
      }
const std::vector< std::string > & MultiChainEvent::getProcessHistory ( ) const

Definition at line 324 of file MultiChainEvent.cc.

References event1_.

{
  return event1_->getProcessHistory();
}
fwlite::Run const& fwlite::MultiChainEvent::getRun ( ) [inline]

Definition at line 115 of file MultiChainEvent.h.

References event1_.

                                {
        return event1_->getRun();
      }
TFile* fwlite::MultiChainEvent::getTFile ( ) const [inline]

Definition at line 101 of file MultiChainEvent.h.

References event1_.

                              {
        return event1_->getTFile();
      }
TFile* fwlite::MultiChainEvent::getTFileSec ( ) const [inline]

Definition at line 104 of file MultiChainEvent.h.

References event2_.

                                 {
        return event2_->getTFile();
      }
bool MultiChainEvent::isValid ( void  ) const

Definition at line 374 of file MultiChainEvent.cc.

References event1_.

{
  return event1_->isValid();
}
MultiChainEvent::operator bool ( ) const

Definition at line 378 of file MultiChainEvent.cc.

References event1_.

{
  return *event1_;
}
const MultiChainEvent & MultiChainEvent::operator++ ( ) [virtual]

Implements fwlite::EventBase.

Definition at line 178 of file MultiChainEvent.cc.

References event1_.

{
   event1_->operator++();
   return *this;
}
const MultiChainEvent& fwlite::MultiChainEvent::operator= ( const Event ) [private]
ChainEvent const* fwlite::MultiChainEvent::primary ( ) const [inline]

Definition at line 132 of file MultiChainEvent.h.

References event1_.

{ return &*event1_;}
edm::ProcessHistory const & MultiChainEvent::processHistory ( ) const [virtual]

Implements edm::EventBase.

Definition at line 330 of file MultiChainEvent.cc.

References event1_.

{
  return event1_->processHistory();
}
ChainEvent const* fwlite::MultiChainEvent::secondary ( ) const [inline]

Definition at line 133 of file MultiChainEvent.h.

References event2_.

{ return &*event2_;}
virtual Long64_t fwlite::MultiChainEvent::secondaryFileIndex ( ) const [inline, virtual]

Reimplemented from fwlite::EventBase.

Definition at line 122 of file MultiChainEvent.h.

References event2_.

      { return event2_->eventIndex(); }
Long64_t MultiChainEvent::size ( void  ) const

Definition at line 390 of file MultiChainEvent.cc.

References event1_.

{
  return event1_->size();
}
void MultiChainEvent::throwProductNotFoundException ( const std::type_info &  iType,
const char *  iModule,
const char *  iInstance,
const char *  iProcess 
) [static]

Definition at line 446 of file MultiChainEvent.cc.

                                                                {
  ChainEvent::throwProductNotFoundException(iType,iModule,iInstance,iProcess);
}
bool MultiChainEvent::to ( edm::RunNumber_t  run,
edm::EventNumber_t  event 
)

Go to event with given run and event number.

Definition at line 208 of file MultiChainEvent.cc.

References to().

{
   return to( run, 0U, event );
}
bool MultiChainEvent::to ( Long64_t  iIndex)

Go to the event at index iIndex.

Definition at line 186 of file MultiChainEvent.cc.

References event1_.

Referenced by to().

{
  return event1_->to( iIndex );
}
bool MultiChainEvent::to ( edm::RunNumber_t  run,
edm::LuminosityBlockNumber_t  lumi,
edm::EventNumber_t  event 
)

Go to event with given run , lumi (if non-zero), and event number.

Definition at line 201 of file MultiChainEvent.cc.

References event1_.

{
   return event1_->to( run, lumi, event );
}
bool MultiChainEvent::to ( edm::EventID  id)

Go to event with event id "id".

Definition at line 194 of file MultiChainEvent.cc.

References event(), edm::EventBase::luminosityBlock(), DTTTrigCorrFirst::run, and to().

{
  return to(id.run(), id.luminosityBlock(), id.event());
}
const MultiChainEvent & MultiChainEvent::toBegin ( ) [virtual]

Implements fwlite::EventBase.

Definition at line 299 of file MultiChainEvent.cc.

References event1_.

{
   event1_->toBegin();
   return *this;
}
bool MultiChainEvent::toSec ( Long64_t  iIndex) [private]

Go to the event from secondary files at index iIndex.

Go to the event at index iIndex.

Definition at line 216 of file MultiChainEvent.cc.

References event2_.

Referenced by toSec().

{
   return event2_->to( iIndex );
}
bool MultiChainEvent::toSec ( const edm::EventID id) [private]

Definition at line 223 of file MultiChainEvent.cc.

References event2_, Exception, first, and secFileMapSorted_.

{
   // First try this file.
   if ( event2_->event_->to( id ) ) 
   {
      // Found it, return. 
      return true;
   }
   // Second, assume that the secondary files are each in run/event
   // order.  So, let's loop over all files and see if we can figure
   // out where the event ought to be.
   for ( sec_file_range_index_map::const_iterator mBegin = 
            secFileMapSorted_.begin(),
            mEnd = secFileMapSorted_.end(),
            mit = mBegin;
         mit != mEnd; 
         ++mit ) 
   {
      if ( id < mit->first.first || id > mit->first.second ) 
      {
         // We don't expect this event to be in this file, so don't
         // bother checking it right now.
         continue;
      }
      // If we're here, then we have a reasonable belief that this
      // event is in this secondary file.  This part is
      // expensive. switchToFile does memory allocations and opens the
      // files which becomes very time consuming. This should be done
      // as infrequently as possible.
      event2_->switchToFile( mit->second );
      // Is it here?
      if (event2_->to( id ))
      {
         // Yes!
         return true;
      }
      // if we assumed that the secondary files were not each in
      // order, but were non-overlapping, we could break here.  But at
      // this point, we might as well keep going.
   } // for loop over files

   // if we are still here, then we did not find the id in question,
   // do it the old fashioned way.  This will open up each secondary
   // file and explicitly check to see if the event is there.
   if (event2_->to(id))
   {
      return true;
   }
   // if we're still here, then there really is no matching event in
   // the secondary files.  Throw.
   throw cms::Exception("ProductNotFound") << "Cannot find id " 
                                           << id.run() << ", " 
                                           << id.event() 
                                           << " in secondary list. Exiting." 
                                           << std::endl;
   // to make the compiler happy
   return false;
}
bool MultiChainEvent::toSec ( edm::RunNumber_t  run,
edm::LuminosityBlockNumber_t  lumi,
edm::EventNumber_t  event 
) [private]

Go to event with given run, lumi, and event number.

Definition at line 284 of file MultiChainEvent.cc.

References toSec().

{
  return toSec( edm::EventID( run, lumi, event) );
}
bool MultiChainEvent::toSec ( edm::RunNumber_t  run,
edm::EventNumber_t  event 
) [private]

Go to event with given run and event number.

Definition at line 292 of file MultiChainEvent.cc.

References toSec().

{
  return toSec( edm::EventID( run, 0U, event) );
}
edm::TriggerNames const & MultiChainEvent::triggerNames ( edm::TriggerResults const &  triggerResults) const [virtual]

Implements edm::EventBase.

Definition at line 396 of file MultiChainEvent.cc.

References event1_, event2_, Exception, h::names, and edm::EventBase::triggerNames_().

{
  edm::TriggerNames const* names = triggerNames_(triggerResults);
  if (names != 0) return *names;

  event1_->fillParameterSetRegistry();
  names = triggerNames_(triggerResults);
  if (names != 0) return *names;

  // If we cannot find it in the primary file, this probably will
  // not help but try anyway
  event2_->to( event1_->id() );
  event2_->fillParameterSetRegistry();
  names = triggerNames_(triggerResults);
  if (names != 0) return *names;

  throw cms::Exception("TriggerNamesNotFound")
    << "TriggerNames not found in ParameterSet registry";
  return *names;
}
edm::TriggerResultsByName MultiChainEvent::triggerResultsByName ( std::string const &  process) const [virtual]

Implements edm::EventBase.

Definition at line 418 of file MultiChainEvent.cc.

References event1_, event2_, fwlite::Handle< T >::getByLabel(), fwlite::Handle< T >::isValid(), h::names, fwlite::Handle< T >::product(), and edm::EventBase::triggerNames_().

                                                                    {

  fwlite::Handle<edm::TriggerResults> hTriggerResults;
  hTriggerResults.getByLabel(*this,"TriggerResults","",process.c_str());
  if ( !hTriggerResults.isValid()) {
    return edm::TriggerResultsByName(0,0);
  }

  edm::TriggerNames const* names = triggerNames_(*hTriggerResults);

  if (names == 0) {
    event1_->fillParameterSetRegistry();
    names = triggerNames_(*hTriggerResults);
  }

  if (names == 0) {
    event2_->to( event1_->id() );
    event2_->fillParameterSetRegistry();
    names = triggerNames_(*hTriggerResults);
  }

  return edm::TriggerResultsByName(hTriggerResults.product(), names);
}

Member Data Documentation

boost::shared_ptr<ChainEvent> fwlite::MultiChainEvent::event1_ [private]
boost::shared_ptr<ChainEvent> fwlite::MultiChainEvent::event2_ [private]

Definition at line 159 of file MultiChainEvent.h.

Referenced by MultiChainEvent().

Definition at line 166 of file MultiChainEvent.h.

Referenced by MultiChainEvent(), and toSec().

Definition at line 165 of file MultiChainEvent.h.

Referenced by MultiChainEvent().