CMS 3D CMS Logo

Classes | Public Member Functions | Private Member Functions | Private Attributes

FWFileEntry Class Reference

#include <FWFileEntry.h>

List of all members.

Classes

struct  Filter

Public Member Functions

void closeFile ()
fwlite::Eventevent ()
TFile * file ()
std::list< Filter * > & filters ()
int firstSelectedEvent ()
 FWFileEntry (const std::string &name)
FWTEventListglobalSelection ()
bool hasActiveFilters ()
bool hasSelectedEvents ()
bool isEventSelected (int event)
int lastEvent ()
int lastSelectedEvent ()
void needUpdate ()
int nextSelectedEvent (int event)
void openFile ()
int previousSelectedEvent (int event)
TTree * tree ()
void updateFilters (const FWEventItemsManager *eiMng, bool isOR)
virtual ~FWFileEntry ()

Private Member Functions

bool filterEventsWithCustomParser (Filter *filter)
 FWFileEntry (const FWFileEntry &)
const FWFileEntryoperator= (const FWFileEntry &)
void runFilter (Filter *fe, const FWEventItemsManager *eiMng)

Private Attributes

fwlite::Eventm_event
TTree * m_eventTree
TFile * m_file
std::list< Filter * > m_filterEntries
FWTEventListm_globalEventList
std::string m_name
bool m_needUpdate

Detailed Description

Definition at line 37 of file FWFileEntry.h.


Constructor & Destructor Documentation

FWFileEntry::FWFileEntry ( const std::string &  name)

Definition at line 20 of file FWFileEntry.cc.

References openFile().

FWFileEntry::~FWFileEntry ( ) [virtual]

Definition at line 27 of file FWFileEntry.cc.

References i, m_filterEntries, and m_globalEventList.

{
   for(std::list<Filter*>::iterator i = m_filterEntries.begin(); i != m_filterEntries.end(); ++i)
      delete (*i)->m_eventList;

   delete m_globalEventList;
}
FWFileEntry::FWFileEntry ( const FWFileEntry ) [private]

Member Function Documentation

void FWFileEntry::closeFile ( )

Definition at line 62 of file FWFileEntry.cc.

References m_event, and m_file.

Referenced by CmsShowNavigator::appendFile(), and CmsShowNavigator::openFile().

{
   if (m_file) {
      printf("Reading %lld bytes in %d transactions.\n",
             m_file->GetBytesRead(), m_file->GetReadCalls());
      m_file->Close();
      delete m_file;
   }
   if (m_event) delete m_event;
}
fwlite::Event* FWFileEntry::event ( ) [inline]

Definition at line 61 of file FWFileEntry.h.

References m_event.

{ return m_event; }
TFile* FWFileEntry::file ( ) [inline]

Definition at line 60 of file FWFileEntry.h.

References m_file.

{ return m_file; }
bool FWFileEntry::filterEventsWithCustomParser ( Filter filter) [private]

Definition at line 304 of file FWFileEntry.cc.

References fwlite::Event::atEnd(), FWTEventList::Enter(), align_tpl::filter, filters(), fwLog, fwlite::Handle< T >::getByLabel(), i, edm::EventBase::id(), iEvent, getHLTprescales::index, j, fwlog::kDebug, fwlog::kWarning, list(), m_event, FWFileEntry::Filter::m_eventList, FWEventSelector::m_expression, m_file, FWFileEntry::Filter::m_needsUpdate, FWFileEntry::Filter::m_selector, FWEventSelector::m_triggerProcess, elec_selection::selection(), edm::TriggerNames::size(), fwlite::Event::to(), fwlite::Event::toBegin(), edm::TriggerNames::triggerIndex(), and fwlite::Event::triggerNames().

Referenced by runFilter().

{
   std::string selection(filterEntry->m_selector->m_expression);

   boost::regex re_spaces("\\s+");
   selection = boost::regex_replace(selection,re_spaces,"");
   if (selection.find("&&") != std::string::npos &&
       selection.find("||") != std::string::npos )
   {
      // Combination of && and || operators not supported.
      return false;
   }

   fwlite::Handle<edm::TriggerResults> hTriggerResults;
   edm::TriggerNames const* triggerNames(0);
   try
   {
      hTriggerResults.getByLabel(*m_event,"TriggerResults","", filterEntry->m_selector->m_triggerProcess.c_str());
      triggerNames = &(m_event->triggerNames(*hTriggerResults));
   }
   catch(...)
   {
      fwLog(fwlog::kWarning) << " failed to get trigger results with process name "<<  filterEntry->m_selector->m_triggerProcess << std::endl;
      return false;
   }
   
   // std::cout << "Number of trigger names: " << triggerNames->size() << std::endl;
   // for (unsigned int i=0; i<triggerNames->size(); ++i)
   //  std::cout << " " << triggerNames->triggerName(i);
   //std::cout << std::endl;
   
   bool junction_mode = true; // AND
   if (selection.find("||")!=std::string::npos)
      junction_mode = false; // OR

   boost::regex re("\\&\\&|\\|\\|");

   boost::sregex_token_iterator i(selection.begin(), selection.end(), re, -1);
   boost::sregex_token_iterator j;

   // filters and how they enter in the logical expression
   std::vector<std::pair<unsigned int,bool> > filters;

   while (i != j)
   {
      std::string filter = *i++;
      bool flag = true;
      if (filter[0] == '!')
      {
         flag = false;
         filter.erase(filter.begin());
      }
      unsigned int index = triggerNames->triggerIndex(filter);
      if (index == triggerNames->size()) 
      {
         // Trigger name not found.
         return false;
      }
      filters.push_back(std::make_pair(index, flag));
   }
   if (filters.empty())
      return false;

   if (filterEntry->m_eventList)
      filterEntry->m_eventList->Reset();
   else
       filterEntry->m_eventList = new FWTEventList();
   FWTEventList* list = filterEntry->m_eventList;

   // loop over events
   edm::EventID currentEvent = m_event->id();
   unsigned int iEvent = 0;

   for (m_event->toBegin(); !m_event->atEnd(); ++(*m_event))
   {
      hTriggerResults.getByLabel(*m_event,"TriggerResults","", filterEntry->m_selector->m_triggerProcess.c_str());
      std::vector<std::pair<unsigned int,bool> >::const_iterator filter = filters.begin();
      bool passed = hTriggerResults->accept(filter->first) == filter->second;
      while (++filter != filters.end())
      {
         if (junction_mode)
            passed &= hTriggerResults->accept(filter->first) == filter->second;
         else
            passed |= hTriggerResults->accept(filter->first) == filter->second;
      }
      if (passed)
         list->Enter(iEvent);
      ++iEvent;
   }
   m_event->to(currentEvent);

   filterEntry->m_needsUpdate = false;
   
   fwLog(fwlog::kDebug) << "FWFile::filterEventsWithCustomParser file [" << m_file->GetName() << "], filter [" << filterEntry->m_selector->m_expression << "], selected [" << list->GetN() << "]"  << std::endl;
   
   return true;
}
std::list<Filter*>& FWFileEntry::filters ( ) [inline]
int FWFileEntry::firstSelectedEvent ( )

Definition at line 86 of file FWFileEntry.cc.

References m_globalEventList.

{
   if (m_globalEventList->GetN() > 0)
   {
      return m_globalEventList->GetEntry(0);
   }
   else
   {
      return -1;
   }
}
FWTEventList* FWFileEntry::globalSelection ( ) [inline]

Definition at line 63 of file FWFileEntry.h.

References m_globalEventList.

{ return m_globalEventList; }
bool FWFileEntry::hasActiveFilters ( )

Definition at line 139 of file FWFileEntry.cc.

References m_filterEntries.

{
   for (std::list<Filter*>::iterator it = m_filterEntries.begin(); it != m_filterEntries.end(); ++it)
   {
      if ((*it)->m_selector->m_enabled)
         return true;
   }

   return false;
}
bool FWFileEntry::hasSelectedEvents ( )

Definition at line 81 of file FWFileEntry.cc.

References m_globalEventList.

{
   return m_globalEventList->GetN() > 0;
}
bool FWFileEntry::isEventSelected ( int  event)

Definition at line 75 of file FWFileEntry.cc.

References m_globalEventList.

{
   int idx = m_globalEventList->GetIndex(tree_entry);
   return idx >= 0;
}
int FWFileEntry::lastEvent ( ) [inline]

Definition at line 79 of file FWFileEntry.h.

References m_eventTree.

{ return m_eventTree->GetEntries() -1; }
int FWFileEntry::lastSelectedEvent ( )

Definition at line 98 of file FWFileEntry.cc.

References m_globalEventList.

{
   if (m_globalEventList->GetN() > 0)
      return m_globalEventList->GetEntry(m_globalEventList->GetN() - 1);
   else
      return -1;
}
void FWFileEntry::needUpdate ( ) [inline]

Definition at line 84 of file FWFileEntry.h.

References m_needUpdate.

{ m_needUpdate = true; }
int FWFileEntry::nextSelectedEvent ( int  event)

Definition at line 106 of file FWFileEntry.cc.

References list(), and m_globalEventList.

{
   // Find next selected event after the current one.
   // This returns the index in the selected event list.
   // If none exists -1 is returned.

   const Long64_t *list = m_globalEventList->GetList();
   Long64_t val = tree_entry;
   Long64_t idx = TMath::BinarySearch(m_globalEventList->GetN(), list, val);
   ++idx;
   if (idx >= m_globalEventList->GetN() || idx < 0)
      return -1;
   return list[idx];
}
void FWFileEntry::openFile ( )

Definition at line 35 of file FWFileEntry.cc.

References m_event, m_eventTree, m_file, and m_name.

Referenced by FWFileEntry().

{
   gErrorIgnoreLevel = 3000; // suppress warnings about missing dictionaries
   TFile *newFile = TFile::Open(m_name.c_str());
   if (newFile == 0 || newFile->IsZombie() || !newFile->Get("Events")) {
      //  std::cout << "Invalid file. Ignored." << std::endl;
      // return false;
      throw std::runtime_error("Invalid file. Ignored.");
   }
   gErrorIgnoreLevel = -1;
   m_file = newFile;
   m_event = new fwlite::Event(m_file);
   m_eventTree = dynamic_cast<TTree*>(m_file->Get("Events"));

   if (m_eventTree == 0)
   { 
      throw std::runtime_error("Cannot find TTree 'Events' in the data file");
   }

   // This now set in DataHelper
   //TTreeCache::SetLearnEntries(2);
   //m_eventTree->SetCacheSize(10*1024*1024);
   //TTreeCache *tc = (TTreeCache*) m_file->GetCacheRead();
   //tc->AddBranch(m_event->auxBranch_,kTRUE);
   //tc->StartLearningPhase();
}
const FWFileEntry& FWFileEntry::operator= ( const FWFileEntry ) [private]
int FWFileEntry::previousSelectedEvent ( int  event)

Definition at line 121 of file FWFileEntry.cc.

References list(), and m_globalEventList.

{
   // Find first selected event before current one.
   // This returns the index in the selected event list.
   // If none exists -1 is returned.

   const Long64_t *list = m_globalEventList->GetList();
   Long64_t val = tree_entry;
   Long64_t idx = TMath::BinarySearch(m_globalEventList->GetN(), list, val);
   if (list[idx] == val)
      --idx;
   if (idx >= 0)
      return list[idx];
   else
      return -1;
}
void FWFileEntry::runFilter ( Filter fe,
const FWEventItemsManager eiMng 
) [private]

Definition at line 196 of file FWFileEntry.cc.

References FWTEventList::Add(), b, FWEventItemsManager::begin(), FWEventItemsManager::end(), end, filterEventsWithCustomParser(), fwLog, fwlite::Event::getBranchNameFor(), FWEventItem::getPrimaryData(), i, fwlog::kDebug, fwlog::kWarning, FWEventItem::m_event, m_event, FWFileEntry::Filter::m_eventList, m_eventTree, FWEventSelector::m_expression, m_file, FWFileEntry::Filter::m_needsUpdate, FWFileEntry::Filter::m_selector, FWEventSelector::m_triggerProcess, FWEventItem::moduleLabel(), AlCaRecoCosmics_cfg::name, FWEventItem::processName(), FWEventItem::productInstanceLabel(), query::result, and FWEventItem::type().

Referenced by updateFilters().

{
   if (!filter->m_selector->m_triggerProcess.empty())
   {
      filterEventsWithCustomParser(filter);
      return;
   }
    
   // parse selection for known Fireworks expressions
   std::string interpretedSelection = filter->m_selector->m_expression;
    
   for (FWEventItemsManager::const_iterator i = eiMng->begin(),
           end = eiMng->end(); i != end; ++i)
   {
      FWEventItem *item = *i;
      if (item == 0) 
         continue;
      //FIXME: hack to get full branch name filled
      if (item->m_event == 0) 
      {
         item->m_event = m_event;
         item->getPrimaryData();
         item->m_event = 0;
      }
      boost::regex re(std::string("\\$") + (*i)->name());
      std::string fullBranchName = m_event->getBranchNameFor(*(item->type()->GetTypeInfo()), 
                                                             item->moduleLabel().c_str(), 
                                                             item->productInstanceLabel().c_str(),
                                                             item->processName().c_str());
      
      interpretedSelection = boost::regex_replace(interpretedSelection, re,
                                                  fullBranchName + ".obj");
      // printf("selection after applying s/%s/%s/: %s\n",
      //     (std::string("\\$") + (*i)->name()).c_str(),
      //     ((*i)->m_fullBranchName + ".obj").c_str(),
      //     interpretedSelection.c_str());
   }

   m_file->cd();
   m_eventTree->SetEventList(0);
   
   // Since ROOT will leave any TBranches used in the filtering at the last event,
   // we need to be able to reset them to what fwlite::Event expects them to be
   // we do this by holding onto the old buffers and create temporary new ones.
   
   TObjArray* branches = m_eventTree->GetListOfBranches();
   std::vector<void*> previousBranchAddresses;
   previousBranchAddresses.reserve(branches->GetEntriesFast());
   {
      std::auto_ptr<TIterator> pIt( branches->MakeIterator());
      while(TObject* branchObj = pIt->Next()) {
         TBranch* b = dynamic_cast<TBranch*> (branchObj);
         if(0!=b) {
            const char * name = b->GetName();
            unsigned int length = strlen(name);
            if(length > 1 && name[length-1]!='.') {
               //this is not a data branch so we should ignore it
               previousBranchAddresses.push_back(0);
               continue;
            }
            //std::cout <<" branch '"<<b->GetName()<<"' "<<static_cast<void*>(b->GetAddress())<<std::endl;
            if(0!=b->GetAddress()) {
               b->SetAddress(0);
            }
            previousBranchAddresses.push_back(b->GetAddress());
         } else {
            previousBranchAddresses.push_back(0);
         }
      }
   }

   FWTEventList *flist = (FWTEventList*) gDirectory->Get("fworks_filter");
   if (flist == 0)
      flist = new FWTEventList("fworks_filter");

   Int_t result = m_eventTree->Draw(">>fworks_filter", interpretedSelection.c_str());
   
   if (filter->m_eventList)
      filter->m_eventList->Reset();
   else
      filter->m_eventList = new FWTEventList;

   filter->m_eventList->Add(flist);
      
   if (result < 0)
      fwLog(fwlog::kWarning) << "FWFile::runFilter in file [" << m_file->GetName() << "] filter [" << filter->m_selector->m_expression << "] is invalid." << std::endl;
   else      
      fwLog(fwlog::kDebug) << "FWFile::runFilter is file [" << m_file->GetName() << "], filter [" << filter->m_selector->m_expression << "] has ["  << flist->GetN() << "] events selected" << std::endl;

   // Set back the old branch buffers.
   {
      std::auto_ptr<TIterator> pIt( branches->MakeIterator());
      std::vector<void*>::const_iterator itAddress = previousBranchAddresses.begin();
      while(TObject* branchObj = pIt->Next()) {
         TBranch* b = dynamic_cast<TBranch*> (branchObj);
         if(0!=b && 0!=*itAddress) {
            b->SetAddress(*itAddress);
         }
         ++itAddress;
      }
   }

   filter->m_needsUpdate = false;
}
TTree* FWFileEntry::tree ( ) [inline]

Definition at line 62 of file FWFileEntry.h.

References m_eventTree.

{ return m_eventTree; }
void FWFileEntry::updateFilters ( const FWEventItemsManager eiMng,
bool  isOR 
)

Definition at line 151 of file FWFileEntry.cc.

References FWTEventList::Add(), fwLog, fwlog::kDebug, m_eventTree, m_file, m_filterEntries, m_globalEventList, m_needUpdate, and runFilter().

{
   if (!m_needUpdate)
      return;
   
   if (m_globalEventList)
      m_globalEventList->Reset();
   else
      m_globalEventList = new FWTEventList;

   for (std::list<Filter*>::iterator it = m_filterEntries.begin(); it != m_filterEntries.end(); ++it)
   {
      if ((*it)->m_selector->m_enabled && (*it)->m_needsUpdate)
      {
         runFilter(*it, eiMng);
      }
      // Need to re-check if enabled after filtering as it can be set to false
      // in runFilter().
      if ((*it)->m_selector->m_enabled)
      {
         if ((*it)->hasSelectedEvents())
         {
            if (globalOR || m_globalEventList->GetN() == 0)
            {
               m_globalEventList->Add((*it)->m_eventList);
            }
            else
            {
               m_globalEventList->Intersect((*it)->m_eventList);
            }
         }
         else if (!globalOR)
         {
            m_globalEventList->Reset();
            break;
         }
      }
   }
   
   fwLog(fwlog::kDebug) << "FWFileEntry::updateFilters in [" << m_file->GetName() << "]  global selection [" << m_globalEventList->GetN() << "/" <<  m_eventTree->GetEntries() << "]" << std::endl;

   m_needUpdate = false;
}

Member Data Documentation

Definition at line 97 of file FWFileEntry.h.

Referenced by closeFile(), event(), filterEventsWithCustomParser(), openFile(), and runFilter().

TTree* FWFileEntry::m_eventTree [private]

Definition at line 96 of file FWFileEntry.h.

Referenced by lastEvent(), openFile(), runFilter(), tree(), and updateFilters().

TFile* FWFileEntry::m_file [private]
std::list<Filter*> FWFileEntry::m_filterEntries [private]

Definition at line 101 of file FWFileEntry.h.

Referenced by filters(), hasActiveFilters(), updateFilters(), and ~FWFileEntry().

std::string FWFileEntry::m_name [private]

Definition at line 94 of file FWFileEntry.h.

Referenced by openFile().

bool FWFileEntry::m_needUpdate [private]

Definition at line 99 of file FWFileEntry.h.

Referenced by needUpdate(), and updateFilters().