Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:57:55

0001 #define METimeInterval_cxx
0002 #include <assert.h>
0003 #include <fstream>
0004 #include <list>
0005 #include <iostream>
0006 #include <algorithm>
0007 using namespace std;
0008 #include <TString.h>
0009 
0010 #include "METimeInterval.hh"
0011 
0012 ClassImp( METimeInterval )
0013 
0014 int METimeInterval::instances=0;
0015  
0016 // public constructor
0017 METimeInterval::METimeInterval( ME::Time firstTime, ME::Time lastTime )
0018   : _firstTime( firstTime ), _lastTime (lastTime ),
0019     _previous(0), _next(0), _above(0), _below(0), _level(0), _good(true)
0020 {
0021   assert( _lastTime>_firstTime );
0022   instances++;
0023 }
0024 
0025 // private constructor
0026 METimeInterval::METimeInterval( ME::Time firstTime, ME::Time lastTime, METimeInterval* previous, METimeInterval* above, bool good )
0027   : _firstTime(firstTime), _lastTime(lastTime),
0028     _previous(previous), _next(0), _above(above), _below(0), _good(good)
0029 {
0030   instances++;
0031   if( _previous==0 ) 
0032     {
0033       assert( _above!=0 );
0034       assert( _firstTime == _above->_firstTime );
0035       assert( _lastTime == _above->_lastTime );
0036       _level = _above->_level + 1;
0037     }
0038   else
0039     {
0040       _level  = _previous->_level;
0041     }
0042   assert( _lastTime>_firstTime );
0043 }
0044 
0045 METimeInterval::~METimeInterval()
0046 {
0047   if( _next )  delete _next;  _next=0;
0048   if( _below ) delete _below; _below=0;
0049   if( _previous ) _previous->_next=0;
0050   else if( _above ) _above->_below=0;
0051   instances--;
0052 }
0053 
0054 void
0055 METimeInterval::split( const list<ME::Time>& times )
0056 {  
0057   if( _below!=0 ) _below->split( times );
0058   else
0059     {
0060       _below = new METimeInterval( _firstTime, _lastTime, 0, this );
0061       METimeInterval* current = _below;
0062       list<ME::Time> theList( times );
0063       theList.unique();
0064       theList.sort();
0065       list<ME::Time>::iterator it;
0066       for( it=theList.begin(); it!=theList.end(); ++it )
0067     {
0068       ME::Time time = *it;
0069       if( time<=_firstTime ) continue;
0070       if( time>=_lastTime  ) break;
0071       current = current->cut( time );
0072     }  
0073     }
0074   if( _next ) _next->split( times );
0075 }
0076 
0077 METimeInterval*
0078 METimeInterval::cut( ME::Time time )
0079 {
0080   if( time<=_firstTime || time>=_lastTime ) return this;
0081   _next = new METimeInterval( time, _lastTime, this, _above );
0082   _lastTime = time;  
0083   return _next;
0084 }
0085 
0086 void 
0087 METimeInterval::print( unsigned level )
0088 {
0089   if( _level==level )
0090     {
0091       oneLine();
0092     }
0093   if( _below ) _below->print( level );
0094   if( _next  ) _next->print( level );
0095 }
0096 
0097 void 
0098 METimeInterval::oneLine()
0099 {
0100   cout << "firstTime/lastTime/level ";
0101   cout << _firstTime << "/" << _lastTime << "/" << _level << endl;
0102 }
0103 
0104 TString
0105 METimeInterval::inBrackets()
0106 {
0107   TString str;
0108   str += "[";
0109   str += _firstTime;
0110   str += ";";
0111   str += _lastTime;
0112   str += "[";
0113   return str;
0114 }
0115 
0116 METimeInterval*
0117 METimeInterval::get( ME::Time time, unsigned level )
0118 {
0119    if( _level>level )
0120      {
0121        assert( _above!=0 );
0122        return _above->get( time, level );
0123      }
0124    if( time<_firstTime )
0125     {
0126       if( _previous ) return _previous->get( time, level );
0127       return 0;
0128     }
0129   if( time>=_lastTime ) 
0130     {
0131       if( _next ) return _next->get( time, level );
0132       if( time==_lastTime )
0133     {
0134       //      if( _below ) return _below->get( time, level );
0135       return 0;  // last run is excluded !
0136     }
0137       return 0;
0138     }
0139   if( _level==level ) return this;
0140   if( _below ) return _below->get( time, level );
0141   return this; // this is as far as it gets
0142 }
0143 
0144 METimeInterval*
0145 METimeInterval::first( unsigned level )
0146 {
0147   if( _level<level ) 
0148     {
0149       if( _below ) return _below->first( level ); 
0150       return 0;
0151     }
0152   if( _level>level ) 
0153     {
0154       if( _above ) return _above->first( level ); 
0155       return 0;
0156     }
0157   return this->firstIn();
0158 }
0159 
0160 METimeInterval*
0161 METimeInterval::last( unsigned level )
0162 {
0163   if( _level<level ) 
0164     {
0165       if( _below->lastIn() ) return _below->lastIn()->last( level ); 
0166       return 0;
0167     }
0168   if( _level>level ) 
0169     {
0170       if( _above->lastIn() ) return _above->lastIn()->last( level ); 
0171       return 0;
0172     }
0173   return this->lastIn();
0174 }
0175 
0176 METimeInterval*
0177 METimeInterval::firstIn()
0178 {
0179   if( _previous ) return _previous->firstIn(); 
0180   return this;
0181 }
0182 METimeInterval*
0183 METimeInterval::lastIn()
0184 {
0185   if( _next ) return _next->lastIn(); 
0186   return this;
0187 }
0188 
0189 METimeInterval*
0190 METimeInterval::next()
0191 {
0192   if( _next ) return _next; 
0193   if( _above==0 ) return 0;
0194   METimeInterval* _above_next = _above->next();
0195   if( _above_next==0 ) return 0;
0196   METimeInterval* _above_next_below = _above_next->_below;
0197   if( _above_next_below==0 ) return 0;
0198   return _above_next_below->firstIn();
0199 }
0200 
0201 METimeInterval*
0202 METimeInterval::previous()
0203 {
0204   if( _previous ) return _previous; 
0205   if( _above!=0 && _above->_previous!=0 && _above->_previous->_below!=0 ) 
0206     return _above->_previous->_below->lastIn();
0207   return 0;
0208 }