5 from collections 
import namedtuple
 
    7 from .sugar 
import RunLumi
 
    8 from .events 
import iov_yielder
 
   11     from .db 
import fetch_iovs
 
   14         return run << 32 | lumi
 
   17         return x >> 32, x & 0xFFFFFFFF
 
   21         A dummy fetch_iovs for testing. Returns two runs, one 100 LBs long  
   22         and the other 50 long. 
   24         T = namedtuple(
"EOR_VAL", 
"since, until RunNumber")
 
   26         return map(T._make, [(
rlumi(  1, 0), 
rlumi(1,  100),           1),
 
   33     Truncate input iovs so that their length does not exceed the length  
   36     Also clean up records which go from (run, 0) to (run+1, 0) 
   38     `run_lengths` should be a dictionary {run_number: lumiblock count} 
   42         Given an iov, correct its since and until if necessary 
   46         if iov.since.run == iov.until.run-1 
and iov.until.lumi == 0:
 
   47             iov = iov._replace(until=
RunLumi(run, 0xFFFFFFFF))
 
   49         if run 
in run_lengths:
 
   50             run_length = run_lengths[run]
 
   51             if iov.until.lumi > run_length:
 
   52                 iov = iov._replace(until=
RunLumi(run, run_length))
 
   54         if iov.since >= iov.until:
 
   59     return [_ 
for _ 
in map(fix_iov, iovs) 
if _ 
is not None]
 
   63     since = 
min(
min(o.since 
for o 
in objects) 
for objects 
in iovs)
 
   64     until = 
max(
max(o.until 
for o 
in objects) 
for objects 
in iovs)
 
   67                              with_channel=
False, what=[])
 
   69     from .oracle 
import atlas_runs_set
 
   72     run_iovs = (iov 
for iov 
in run_iovs 
if iov.since.run 
in atlas_runs)
 
   84     result_iovs = [[] 
for i 
in iovs]
 
   85     active_states, ended_states = [
set() 
for i 
in iovs], [
set() 
for i 
in iovs]
 
   87     def bind_to_run(run_iov, active_states):
 
   89         Given a run_iov and a set of active iovs, emit iovs which are bound 
   92         for active_state 
in sorted(active_states):
 
   93             iov = active_state._replace(
 
   94                 since=
max(run_iov.since+1, active_state.since),
 
   95                 until=
min(run_iov.until,   active_state.until))
 
   97             if iov.since != iov.until:
 
  101     for position, index, beginning, iov 
in iov_yielder(run_iovs, *iovs):
 
  108                 for active, ended 
in zip(active_states, ended_states):
 
  109                     active.difference_update(ended)
 
  114                 for i, chan_active_states 
in enumerate(active_states):
 
  115                     result_iovs[i].
extend(bind_to_run(iov, chan_active_states))
 
  121             action = active_states[index] 
if beginning 
else ended_states[index]
 
  128     IOV = namedtuple(
"IOV", 
"since until channel state")
 
  135         A pretty convertor for (since, until) 
  138                 self.channel, self.state)
 
  140         return "IOV(since=(%i, %i), until=(%i, %i), channel=%i, state=%s)" % args
 
  141     IOV.__repr__ = __repr__
 
  156     from pprint 
import pprint
 
  160 if __name__ == 
"__main__":