CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Fireworks/Core/src/CmsShowNavigator.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     newVersion
00004 // Class  :     CmsShowNavigator
00005 // $Id: CmsShowNavigator.cc,v 1.111 2011/09/12 20:30:58 amraktad Exp $
00006 //
00007 
00008 #include "DataFormats/FWLite/interface/Event.h"
00009 #include "Fireworks/Core/src/CmsShowMain.h"
00010 
00011 #include "Fireworks/Core/interface/FWEventItem.h"
00012 
00013 // system include files
00014 #include <boost/regex.hpp>
00015 #include "TROOT.h"
00016 #include "TTree.h"
00017 #include "TFile.h"
00018 #include "TError.h"
00019 #include "TGTextEntry.h"
00020 #include "TGNumberEntry.h"
00021 #include "TBranch.h"
00022 #include "TAxis.h"
00023 
00024 #include  <TApplication.h>
00025 #include  <TSystem.h>
00026 #include  <TObject.h>
00027 
00028 // user include files
00029 #include "Fireworks/Core/interface/CmsShowNavigator.h"
00030 #include "Fireworks/Core/interface/CSGAction.h"
00031 #include "Fireworks/Core/interface/FWEventItemsManager.h"
00032 #include "Fireworks/Core/interface/FWGUIEventFilter.h"
00033 #include "Fireworks/Core/interface/FWTEventList.h"
00034 #include "Fireworks/Core/interface/FWGUIManager.h"
00035 #include "Fireworks/Core/interface/FWGUIEventSelector.h"
00036 #include "Fireworks/Core/interface/FWJobMetadataManager.h"
00037 #include "Fireworks/Core/interface/FWConfiguration.h"
00038 #include "Fireworks/Core/interface/Context.h"
00039 #include "Fireworks/Core/interface/fwLog.h"
00040 
00041 //
00042 // constructors and destructor
00043 //
00044 CmsShowNavigator::CmsShowNavigator(const CmsShowMain &main):
00045    FWNavigatorBase(main),
00046    m_currentEvent(0),
00047 
00048    m_filterState(kOff),
00049    m_filterMode(kOr),
00050 
00051    m_filesNeedUpdate(true),
00052    m_newFileOnNextEvent(false),
00053 
00054    m_maxNumberOfFilesToChain(1),
00055 
00056    m_main(main),
00057    m_guiFilter(0)
00058 {
00059    m_guiFilter = new FWGUIEventFilter(this);
00060    filterStateChanged_.connect(boost::bind(&FWGUIEventFilter::updateFilterStateLabel, m_guiFilter, _1));
00061 }
00062 
00063 CmsShowNavigator::~CmsShowNavigator()
00064 {
00065    delete m_guiFilter;
00066 }
00067 
00068 //
00069 // member functions
00070 //
00071 
00072 bool
00073 CmsShowNavigator::openFile(const std::string& fileName)
00074 {
00075    fwLog(fwlog::kDebug) << "CmsShowNavigator::openFile [" << fileName << "]" << std::endl;
00076    FWFileEntry* newFile = 0;
00077    try
00078    {
00079       newFile = new FWFileEntry(fileName, m_main.getVersionCheck());
00080    }
00081    catch (std::exception& iException)
00082    {
00083       fwLog(fwlog::kError) <<"Navigator::openFile ecaught exception FWFileEntry constructor " << iException.what()<<std::endl;
00084 
00085       delete newFile;
00086       return false;
00087    }
00088 
00089    try 
00090    {
00091       // delete all previous files
00092       while ( m_files.size() > 0 )
00093       {
00094          FWFileEntry* file = m_files.front();
00095          m_files.pop_front();
00096          file->closeFile();
00097          delete file;
00098       }
00099 
00100       m_files.push_back(newFile);
00101       setCurrentFile(m_files.begin());
00102 
00103       // set filters
00104       for (std::list<FWEventSelector*>::iterator i = m_selectors.begin(); i != m_selectors.end(); ++i)
00105          newFile->filters().push_back(new FWFileEntry::Filter(*i));
00106 
00107       if (m_filterState != kOff)
00108          updateFileFilters();
00109 
00110       return true;
00111    }
00112    catch (std::exception& iException) 
00113    {
00114       fwLog(fwlog::kError) <<"Navigator::openFile caught exception "<<iException.what()<<std::endl;
00115       return false;
00116    }
00117 }
00118 
00119 bool
00120 CmsShowNavigator::appendFile(const std::string& fileName, bool checkFileQueueSize, bool live)
00121 {
00122    fwLog(fwlog::kDebug) << "CmsShowNavigator::appendFile [" << fileName << "]" << std::endl;
00123    FWFileEntry* newFile  = 0;
00124    try
00125    {
00126       newFile = new FWFileEntry(fileName, m_main.getVersionCheck());
00127    }
00128    catch(std::exception& iException)
00129    {
00130       fwLog(fwlog::kError) <<"Navigator::appendFile caught exception FWFileEntry constructor "<<iException.what()<<std::endl;
00131       delete newFile;
00132       return false;
00133    }
00134 
00135    try
00136    {  
00137       if (checkFileQueueSize)
00138       {
00139          int toErase = m_files.size() - (m_maxNumberOfFilesToChain + 1);
00140          while (toErase > 0)
00141          {
00142             FileQueue_i si = m_files.begin();
00143             if (m_currentFile == si)
00144                si++;
00145             FWFileEntry* file = *si;
00146             file->closeFile();
00147             delete file;
00148             
00149             m_files.erase(si);
00150             --toErase;
00151          }
00152 
00153          if (m_files.size() > m_maxNumberOfFilesToChain)
00154             fwLog(fwlog::kWarning) << "  " <<  m_files.size() << " chained files more than maxNumberOfFilesToChain \n" <<  m_maxNumberOfFilesToChain << std::endl;
00155       }
00156 
00157       m_files.push_back(newFile);
00158 
00159       // Needed for proper handling of first registered file when -port option is in effect.
00160       if (!m_currentFile.isSet())
00161          setCurrentFile(m_files.begin());
00162 
00163       // set filters
00164       for (std::list<FWEventSelector*>::iterator i = m_selectors.begin(); i != m_selectors.end(); ++i)
00165          newFile->filters().push_back(new FWFileEntry::Filter(*i));
00166 
00167       if (m_filterState != kOff)     
00168          updateFileFilters();
00169 
00170    }   
00171    catch(std::exception& iException)
00172    {
00173       std::cerr <<"Navigator::openFile caught exception "<<iException.what()<<std::endl;
00174       return false;
00175    }
00176 
00177    return true;
00178 }
00179 
00180 //______________________________________________________________________________
00181 
00182 void CmsShowNavigator::setCurrentFile(FileQueue_i fi)
00183 {
00184    if (fwlog::presentLogLevel() == fwlog::kDebug)
00185    {
00186       int cnt = 0;
00187       for (FileQueue_i i = m_files.begin(); i!= m_files.end(); i++)
00188       {
00189          if ( i == fi) break;
00190          cnt++;
00191       }
00192       
00193       fwLog(fwlog::kDebug) << "CmsShowNavigator::setCurrentFile [" << (*fi)->file()->GetName() << "] file idx in chain [" << cnt << "/" << m_files.size() -1 << "]" << std::endl;
00194    }
00195    
00196    m_currentFile = fi;
00197    fileChanged_.emit((*m_currentFile)->file());
00198 }
00199 
00200 void
00201 CmsShowNavigator::goTo(FileQueue_i fi, int event)
00202 {
00203    if (fi != m_currentFile)
00204       setCurrentFile(fi);
00205 
00206    if (fwlog::presentLogLevel() == fwlog::kDebug)
00207    {
00208       int total = (*fi)->tree()->GetEntries();
00209       fwLog(fwlog::kDebug) << "CmsShowNavigator::goTo  current file event [" << event  << "/" << total -1<< "]"  << std::endl;
00210 
00211       CpuInfo_t  cpuInfo;
00212       MemInfo_t  memInfo;
00213       ProcInfo_t procInfo;
00214       gSystem->GetCpuInfo(&cpuInfo, 0);
00215       gSystem->GetMemInfo(&memInfo);
00216       gSystem->GetProcInfo(&procInfo);
00217 
00218       time_t curtime;
00219       time(&curtime);
00220       
00221       fwLog(fwlog::kDebug) << "Current Time: "  << ctime(&curtime);
00222       fwLog(fwlog::kDebug) << "memInfo.fMemUsed \t"  << memInfo.fMemUsed << std::endl;
00223       fwLog(fwlog::kDebug) << "memInfo.fSwapUsed\t" << memInfo.fSwapUsed << std::endl; 
00224       fwLog(fwlog::kDebug) << "procInfo.fMemResident\t" << procInfo.fMemResident << std::endl;
00225       fwLog(fwlog::kDebug) << "procInfo.fMemVirtual\t" << procInfo.fMemVirtual << std::endl;
00226       fwLog(fwlog::kDebug) << "cpuInfo.fLoad1m \t" << cpuInfo.fLoad1m << std::endl;
00227       fwLog(fwlog::kDebug) << "cpuInfo.fLoad5m \t" << cpuInfo.fLoad5m << std::endl;
00228    }
00229    
00230    (*m_currentFile)->event()->to(event);
00231    (*m_currentFile)->tree()->LoadTree(event);
00232    m_currentEvent = event;
00233 
00234    newEvent_.emit();
00235 }
00236 
00237 void
00238 CmsShowNavigator::goToRunEvent(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event)
00239 {
00240    for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00241    {
00242       Long64_t index = (*file)->event()->indexFromEventId(run, lumi, event);
00243       if (index >= 0)
00244       {
00245          goTo(file, index);
00246          break;
00247       }
00248    }
00249 }
00250 
00251 //______________________________________________________________________________
00252 
00253 void
00254 CmsShowNavigator::firstEvent()
00255 {
00256    FileQueue_i x = m_files.begin();
00257    if (m_filterState == kOn)
00258    {
00259       while (x != m_files.end())
00260       {
00261          if ((*x)->hasSelectedEvents())
00262          {
00263             goTo(x, (*x)->firstSelectedEvent());
00264             return;
00265          }
00266          ++x;
00267       }
00268    }
00269    else
00270    {
00271       goTo(x, 0);
00272    }
00273 }
00274 
00275 void
00276 CmsShowNavigator::lastEvent()
00277 {
00278    FileQueue_i x = m_files.end();
00279    if (m_filterState == kOn)
00280    {
00281       while (x != m_files.begin())
00282       {
00283          --x;
00284          if ((*x)->hasSelectedEvents())
00285          {
00286             goTo(x, (*x)->lastSelectedEvent());
00287             return;
00288          }
00289       }
00290    }
00291    else
00292    {
00293       --x;
00294       goTo(x, (*x)->lastEvent());
00295    }
00296 }
00297 
00298 //______________________________________________________________________________
00299 
00300 bool
00301 CmsShowNavigator::nextSelectedEvent()
00302 {
00303    int nextEv = (*m_currentFile)->nextSelectedEvent(m_currentEvent);
00304    if (nextEv > -1)
00305    {
00306       goTo(m_currentFile, nextEv);
00307       return true;
00308    }
00309    else
00310    {
00311       FileQueue_i i = m_currentFile; ++i;
00312       while (i != m_files.end())
00313       {
00314          if ((*i)->hasSelectedEvents())
00315          {
00316             goTo(i, (*i)->firstSelectedEvent());
00317             return true;
00318          }
00319          ++i;
00320       }
00321    }
00322 
00323    return false;
00324 }
00325 
00326 //______________________________________________________________________________
00327 
00328 void
00329 CmsShowNavigator::nextEvent()
00330 {
00331    if (m_newFileOnNextEvent)
00332    {
00333       FileQueue_i last = m_files.end(); --last;
00334       if (m_filterState == kOn)
00335          goTo(last, (*last)->firstSelectedEvent());
00336       else
00337          goTo(last, 0);
00338 
00339       m_newFileOnNextEvent = false;
00340       return;
00341    }
00342    
00343    if (m_filterState == kOn)
00344    {
00345       nextSelectedEvent();
00346       return;
00347    }
00348    else
00349    {
00350       if (m_currentEvent < (*m_currentFile)->lastEvent())
00351       {
00352          goTo(m_currentFile, m_currentEvent + 1);
00353       }
00354       else
00355       {
00356          FileQueue_i x = m_currentFile ; ++x;
00357          if (x != m_files.end())
00358          {
00359             goTo(x, 0);
00360          }
00361       }
00362    }
00363 }
00364 
00365 //______________________________________________________________________________
00366 
00367 bool
00368 CmsShowNavigator::previousSelectedEvent()
00369 {
00370    int prevEv = (*m_currentFile)->previousSelectedEvent(m_currentEvent);
00371    if (prevEv > -1)
00372    {
00373       goTo(m_currentFile, prevEv);
00374       return true;
00375    }
00376    else
00377    {
00378       FileQueue_i i(m_currentFile); i.previous(m_files);
00379       while (i != m_files.end())
00380       {
00381          if ((*i)->hasSelectedEvents())
00382          {
00383             goTo(i, (*i)->lastSelectedEvent());
00384             return true;
00385          }
00386          i.previous(m_files);
00387       }
00388    }
00389    return false;
00390 }
00391 
00392 //______________________________________________________________________________
00393 
00394 void
00395 CmsShowNavigator::previousEvent()
00396 {
00397    if (m_filterState == kOn)
00398    {
00399       previousSelectedEvent();
00400    }
00401    else
00402    {
00403       if (m_currentEvent > 0)
00404       {
00405          goTo(m_currentFile, m_currentEvent - 1);
00406       }
00407       else
00408       {
00409          // last event in previous file
00410          FileQueue_i x = m_currentFile;
00411          if (x != m_files.begin())
00412          {
00413             --x;
00414             goTo(x, (*x)->lastEvent());
00415          }
00416       }
00417    }
00418 }
00419 
00420 //______________________________________________________________________________
00421 
00422 void
00423 CmsShowNavigator::toggleFilterEnable()
00424 {
00425    // callback
00426 
00427    fwLog(fwlog::kInfo) << "CmsShowNavigator::toggleFilterEnable filters enabled [" << ( m_filterState == kOff) << "]" << std::endl;
00428 
00429    if (m_filterState == kOff)
00430    {
00431       m_filterState = kOn;
00432       m_guiFilter->setupDisableFilteringButton(true);
00433 
00434       updateFileFilters();
00435    }
00436    else
00437    {
00438       m_filterState = kOff;
00439       m_guiFilter->setupDisableFilteringButton(false);
00440    }
00441 
00442    filterStateChanged_.emit(m_filterState);
00443 }
00444 
00445 void
00446 CmsShowNavigator::withdrawFilter()
00447 {
00448    fwLog(fwlog::kInfo) << "CmsShowNavigator::witdrawFilter" << std::endl;
00449    m_filterState = kWithdrawn;
00450    filterStateChanged_.emit(m_filterState);
00451 }
00452 
00453 void
00454 CmsShowNavigator::resumeFilter()
00455 {
00456    fwLog(fwlog::kInfo) << "CmsShowNavigator::resumeFilter" << std::endl;
00457    m_filterState = kOn;
00458    filterStateChanged_.emit(m_filterState);
00459 }
00460 
00461 void
00462 CmsShowNavigator::updateFileFilters()
00463 {
00464    // run filters on files
00465    std::list<FWFileEntry::Filter>::iterator it;
00466    for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00467    {
00468       if ( m_filesNeedUpdate) (*file)->needUpdate();
00469       (*file)->updateFilters(m_main.context()->eventItemsManager(), m_filterMode == kOr);
00470    }
00471    updateSelectorsInfo();
00472    m_filesNeedUpdate = false;
00473  
00474    int nSelected = getNSelectedEvents();
00475    if (nSelected)
00476    {
00477       // go to the nearest selected event/file
00478       if (!(*m_currentFile)->isEventSelected(m_currentEvent))
00479       {
00480          if (!nextSelectedEvent())
00481             previousSelectedEvent();
00482       }
00483 
00484       if (m_filterState == kWithdrawn)
00485          resumeFilter();
00486 
00487       postFiltering_.emit();
00488    }
00489    else
00490    {
00491       withdrawFilter();
00492    }
00493 
00494    if (fwlog::presentLogLevel() == fwlog::kDebug)
00495    {
00496       fwLog(fwlog::kDebug) << "CmsShowNavigator::updateFileFilters selected events over files [" << getNSelectedEvents() << "/" << getNTotalEvents() << "]" << std::endl;
00497    }
00498 }
00499 
00500 //=======================================================================
00501 void
00502 CmsShowNavigator::removeFilter(std::list<FWEventSelector*>::iterator si)
00503 {
00504    fwLog(fwlog::kDebug) << "CmsShowNavigator::removeFilter " << (*si)->m_expression << std::endl;
00505 
00506    std::list<FWFileEntry::Filter*>::iterator it;
00507    for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00508    {
00509       for (it = (*file)->filters().begin(); it != (*file)->filters().end(); ++it)
00510       {
00511          if ((*it)->m_selector == *si)
00512          {
00513             FWFileEntry::Filter* f = *it;
00514             delete f;
00515             (*file)->filters().erase(it);
00516             break;
00517          }
00518       }
00519    }
00520 
00521    delete *si;
00522    m_selectors.erase(si);
00523    m_filesNeedUpdate = true;
00524 }
00525 
00526 void
00527 CmsShowNavigator::addFilter(FWEventSelector* ref)
00528 {
00529    fwLog(fwlog::kDebug) << "CmsShowNavigator::addFilter " << ref->m_expression << std::endl;
00530 
00531    FWEventSelector* selector = new FWEventSelector(ref);
00532    m_selectors.push_back(selector);
00533 
00534    for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00535    {
00536       (*file)->filters ().push_back(new FWFileEntry::Filter(selector));
00537    }
00538    m_filesNeedUpdate = true;
00539 }
00540 
00541 void
00542 CmsShowNavigator::changeFilter(FWEventSelector* selector, bool updateFilter)
00543 {
00544    fwLog(fwlog::kDebug) << "CmsShowNavigator::changeFilter " << selector->m_expression << std::endl;
00545 
00546    std::list<FWFileEntry::Filter*>::iterator it;
00547    for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00548    {
00549       for (it = (*file)->filters().begin(); it != (*file)->filters().end(); ++it)
00550       {
00551          if ((*it)->m_selector == selector)
00552          {
00553             if (updateFilter)  (*it)->m_needsUpdate = true;
00554             (*it)->m_selector->m_expression  = selector->m_expression;
00555             break;
00556          }
00557       }
00558    }
00559    m_filesNeedUpdate = true;
00560 }
00561 
00562 void
00563 CmsShowNavigator::applyFiltersFromGUI()
00564 {
00565    m_filesNeedUpdate = false;
00566 
00567    // check if filters are set ON
00568    if (m_filterState == kOff)
00569    {
00570       m_filesNeedUpdate = true;
00571       m_filterState = kOn;
00572       m_guiFilter->setupDisableFilteringButton(true);
00573    }
00574 
00575    // compare changes and then call updateFileFilters
00576    std::list<FWEventSelector*>::iterator    si = m_selectors.begin();
00577    std::list<FWGUIEventSelector*>::iterator gi = m_guiFilter->guiSelectors().begin();
00578 
00579    if (m_filterMode != m_guiFilter->getFilterMode()) {
00580       m_filterMode = m_guiFilter->getFilterMode();
00581       m_filesNeedUpdate = true;
00582    }
00583 
00584    while (si != m_selectors.end() || gi != m_guiFilter->guiSelectors().end())
00585    {
00586       if (gi == m_guiFilter->guiSelectors().end() && si != m_selectors.end())
00587       {
00588          removeFilter(si++);
00589       }
00590       else if (si == m_selectors.end() && gi != m_guiFilter->guiSelectors().end() )
00591       {
00592          addFilter((*gi)->guiSelector());
00593          (*gi)->setOrigSelector(m_selectors.back());
00594          ++gi;
00595       }
00596       else
00597       {
00598          if (*si == (*gi)->origSelector())
00599          {
00600             FWEventSelector* g  = (*gi)->guiSelector();
00601             FWEventSelector* o = *si;
00602             bool filterNeedUpdate = o->m_expression != g->m_expression;
00603             if (filterNeedUpdate || o->m_enabled != g->m_enabled) {
00604                *o = *g;
00605                changeFilter(*si, filterNeedUpdate);
00606             }
00607             else
00608             {
00609                o->m_description = g->m_description;
00610             }
00611             ++si; ++gi;
00612          }
00613          else if ((*gi)->origSelector() == 0)
00614          {
00615             addFilter((*gi)->guiSelector());
00616             (*gi)->setOrigSelector(m_selectors.back());
00617             ++gi;
00618          }
00619          else
00620          {
00621             removeFilter(si++);
00622          }
00623       }
00624    }
00625 
00626    if ( m_filesNeedUpdate )
00627       updateFileFilters();
00628 
00629    filterStateChanged_.emit(m_filterState);
00630 
00631 }
00632 //______________________________________________________________________________
00633 // helpers for gui state
00634 
00635 bool
00636 CmsShowNavigator::isFirstEvent()
00637 {
00638    if (m_filterState == kOn)
00639    {
00640       FileQueue_i firstSelectedFile;
00641       for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00642       {
00643          if ((*file)->hasSelectedEvents())
00644          {
00645             firstSelectedFile = file;
00646             break;
00647          }
00648       }
00649 
00650       if (firstSelectedFile == m_currentFile)
00651          return (*m_currentFile)->firstSelectedEvent() == m_currentEvent;
00652    }
00653    else
00654    {
00655       if (m_currentFile == m_files.begin())
00656       {
00657          return m_currentEvent == 0;
00658       }
00659    }
00660    return false;
00661 }
00662 
00663 bool
00664 CmsShowNavigator::isLastEvent()
00665 {
00666    if (m_filterState == kOn)
00667    {
00668       FileQueue_i lastSelectedFile;
00669       for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00670       {
00671          if ((*file)->hasSelectedEvents())
00672             lastSelectedFile = file;
00673       }
00674       if (lastSelectedFile == m_currentFile)
00675          return (*m_currentFile)->lastSelectedEvent() == m_currentEvent;
00676    }
00677    else
00678    {
00679       FileQueue_i lastFile = m_files.end();
00680       --lastFile;
00681       if (m_currentFile == lastFile)
00682       {
00683          return (*m_currentFile)->lastEvent() == m_currentEvent;
00684       }
00685    }
00686    return false;
00687 }
00688 
00689 //______________________________________________________________________________
00690 void
00691 CmsShowNavigator::updateSelectorsInfo()
00692 {
00693  
00694    // reset
00695    std::list<FWEventSelector*>::const_iterator sel = m_selectors.begin();
00696    while ( sel != m_selectors.end())
00697    {
00698       (*sel)->m_selected = 0;
00699       (*sel)->m_updated  = true;
00700       ++sel;
00701    }
00702 
00703    // loop file filters
00704    std::list<FWFileEntry::Filter*>::iterator i;
00705    for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00706    {
00707       std::list<FWFileEntry::Filter*>& filters = (*file)->filters();
00708       for (i = filters.begin(); i != filters.end(); ++i)
00709       {
00710          if ((*i)->m_eventList)
00711          {
00712             (*i)->m_selector->m_selected += (*i)->m_eventList->GetN();
00713          }
00714 
00715          if ((*i)->m_needsUpdate) 
00716             (*i)->m_selector->m_updated = false;
00717       }
00718    }
00719    if (m_guiFilter) 
00720    {
00721       std::list<FWGUIEventSelector*>::const_iterator gs = m_guiFilter->guiSelectors().begin();
00722       while ( gs !=  m_guiFilter->guiSelectors().end())
00723       {
00724          (*gs)->updateNEvents();
00725          ++gs;
00726       }
00727    }
00728 }
00729 
00730 int
00731 CmsShowNavigator::getNSelectedEvents()
00732 {
00733    int sum = 0;
00734    for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00735    {
00736       if ((*file)->globalSelection())
00737          sum += (*file)->globalSelection()->GetN();
00738    }
00739    return sum;
00740 }
00741 
00742 int
00743 CmsShowNavigator::getNTotalEvents()
00744 {
00745    int sum = 0;
00746    for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00747    {
00748       sum += (*file)->tree()->GetEntries();
00749    }
00750 
00751    return sum;
00752 }
00753 
00754 const char*
00755 CmsShowNavigator::filterStatusMessage()
00756 {
00757    if (m_filterState == kOn)
00758       return Form("%d events are selected from %d.", getNSelectedEvents(), getNTotalEvents());
00759    else if (m_filterState == kOff)
00760       return "Filtering is OFF.";
00761    else
00762       return "Filtering is disabled.";
00763 }
00764 
00765 bool
00766 CmsShowNavigator::canEditFiltersExternally()
00767 {
00768    bool haveActiveFilters = false;
00769    for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00770    {
00771       if ((*file)->hasActiveFilters())
00772       {
00773          haveActiveFilters = true;
00774          break;
00775       }
00776    }
00777 
00778    bool btnEnabled = haveActiveFilters;
00779 
00780    if (m_guiFilter && m_guiFilter->isOpen())
00781       btnEnabled = false;
00782 
00783    return btnEnabled;
00784 }
00785 
00786 void
00787 CmsShowNavigator::editFiltersExternally()
00788 {
00789    editFiltersExternally_.emit(canEditFiltersExternally());
00790 }
00791 
00792 void
00793 CmsShowNavigator::showEventFilterGUI(const TGWindow* p)
00794 {
00795    if (m_guiFilter->IsMapped())
00796    {
00797       m_guiFilter->CloseWindow();
00798    }
00799    else
00800    {
00801       m_guiFilter->show(&m_selectors, m_filterMode, m_filterState);
00802       editFiltersExternally_.emit(canEditFiltersExternally());
00803    }
00804 }
00805 
00806 //______________________________________________________________________________
00807 
00808 void
00809 CmsShowNavigator::setFrom(const FWConfiguration& iFrom)
00810 {
00811    m_filesNeedUpdate = true;
00812 
00813    EFilterState oldFilterState =  m_filterState;
00814 
00815   
00816    m_selectors.clear();
00817 
00818    // selectors   
00819    const FWConfiguration* sConf = iFrom.valueForKey("EventFilters");
00820 
00821    if (sConf)
00822    {
00823       const FWConfiguration::KeyValues* keyVals = sConf->keyValues();
00824       if (keyVals) {
00825          for(FWConfiguration::KeyValuesIt it = keyVals->begin(); it!= keyVals->end(); ++it)
00826          {
00827             const FWConfiguration& conf = (*it).second;
00828             FWEventSelector* selector = new FWEventSelector();
00829             selector->m_expression   = conf.valueForKey("expression")->value();
00830             selector->m_description  = conf.valueForKey("comment")->value();
00831             selector->m_enabled      = atoi(conf.valueForKey("enabled")->value().c_str());
00832             if (conf.valueForKey("triggerProcess"))
00833                selector->m_triggerProcess = conf.valueForKey("triggerProcess")->value();
00834             m_selectors.push_back(selector);
00835          }
00836       }
00837    }
00838    else
00839    {
00840       int numberOfFilters = 0;
00841       const FWConfiguration* nfvalue = iFrom.valueForKey( "EventFilter_total" );
00842       if(nfvalue)
00843          numberOfFilters = atoi(nfvalue->value().c_str());
00844 
00845       for(int i=0; i<numberOfFilters; ++i)
00846       {
00847          FWEventSelector* selector = new FWEventSelector();
00848          {
00849             const FWConfiguration* value = iFrom.valueForKey( Form("EventFilter%d_enabled",i));
00850             assert(value);
00851             std::istringstream s(value->value());
00852             s>>selector->m_enabled;
00853          }
00854          selector->m_expression  = iFrom.valueForKey( Form("EventFilter%d_selection",i))->value();
00855          selector->m_description = iFrom.valueForKey( Form("EventFilter%d_comment",i) )->value();
00856 
00857          
00858          if (strstr(selector->m_expression.c_str(), "HLT"))
00859             selector->m_triggerProcess = "HLT";
00860          
00861          m_selectors.push_back(selector);
00862       }
00863    }
00864 
00865    // filter mode
00866    {
00867       const FWConfiguration* value = iFrom.valueForKey( "EventFilter_mode" );
00868       if (value)
00869       {
00870          std::istringstream s(value->value());
00871          s>> m_filterMode;
00872       }
00873    }
00874    
00875    // filter on
00876    {
00877       const FWConfiguration* value = iFrom.valueForKey( "EventFilter_enabled" );
00878       std::istringstream s(value->value());
00879       int x;
00880       s>> x;
00881       m_filterState = x ? kOn : kOff;
00882    }
00883    
00884 
00885    // redesplay new filters in event filter dialog if already mapped
00886    if (m_guiFilter)
00887    {
00888       m_guiFilter->reset();
00889       if (m_guiFilter->IsMapped()) m_guiFilter->show(&m_selectors, m_filterMode, m_filterState);
00890    }
00891    
00892    if (!m_files.empty())
00893    {
00894       // change filters in existing files 
00895       for (FileQueue_i file = m_files.begin(); file != m_files.end(); ++file)
00896       { 
00897          (*file)->filters().clear();
00898          for (std::list<FWEventSelector*>::iterator i = m_selectors.begin(); i != m_selectors.end(); ++i)
00899             (*file)->filters().push_back(new FWFileEntry::Filter(*i));
00900       }
00901       
00902       
00903       // run new filters if enabled, else just reset 
00904       if  (m_filterState == kOn)
00905          updateFileFilters();
00906       else
00907          postFiltering_.emit();
00908    }
00909    // update CmsShowMainFrame checkBoxIcon and button text
00910    if (oldFilterState != m_filterState)
00911       filterStateChanged_.emit(m_filterState);
00912 }
00913 
00914 void
00915 CmsShowNavigator::addTo(FWConfiguration& iTo) const
00916 {
00917    // selectors
00918    FWConfiguration tmp;
00919    int cnt = 0;
00920    for (std::list<FWEventSelector*>::const_iterator sel = m_selectors.begin(); sel != m_selectors.end(); ++sel) 
00921    {
00922       FWConfiguration filter;
00923 
00924       filter.addKeyValue("expression",FWConfiguration((*sel)->m_expression));
00925       filter.addKeyValue("enabled", FWConfiguration((*sel)->m_enabled ? "1" : "0"));
00926       filter.addKeyValue("comment", FWConfiguration((*sel)->m_description));
00927   
00928       if (!(*sel)->m_triggerProcess.empty())
00929          filter.addKeyValue("triggerProcess",FWConfiguration((*sel)->m_triggerProcess));
00930 
00931       tmp.addKeyValue(Form("------Filter[%d]------", cnt), filter,true);
00932       ++cnt;
00933    }
00934 
00935    iTo.addKeyValue("EventFilters", tmp,true);
00936 
00937    // mode
00938    {
00939       std::stringstream s;
00940       s<< m_filterMode;
00941       iTo.addKeyValue("EventFilter_mode", s.str());
00942    }
00943 
00944    // enabled
00945    iTo.addKeyValue("EventFilter_enabled",FWConfiguration( m_filterState == kOn ? "1" : "0"));
00946 }
00947 
00948 std::vector<std::string>& 
00949 CmsShowNavigator::getProcessList() const
00950 {
00951   return  m_main.context()->metadataManager()->processNamesInJob();
00952 }
00953 
00954 const edm::EventBase* 
00955 CmsShowNavigator::getCurrentEvent() const
00956 {
00957    return m_currentFile.isSet() ? (*m_currentFile)->event() : 0; 
00958 }
00959 
00960 const char*
00961 CmsShowNavigator::frameTitle()
00962 {
00963    if (m_files.empty()) return "";
00964 
00965    int nf = 0;
00966    for (FileQueue_t::const_iterator i = m_files.begin(); i!= m_files.end(); i++)
00967    {
00968 
00969       if ( i == m_currentFile) break;
00970       nf++;
00971    }
00972 
00973    TString name = (*m_currentFile)->file()->GetName();
00974    int l = name.Last('/');
00975    if (l != kNPOS)
00976       name.Remove(0, l+1);
00977 
00978    return Form("%s [%d/%d], event [%d/%d]", name.Data(),
00979                nf+1,  (int) m_files.size(),
00980                m_currentEvent+1, (*m_currentFile)->lastEvent()+1);
00981 }