Posts Tagged ‘Python’

recreate L0Banks for LHCb HLT

Friday, July 11th, 2008

To recreate L0 Banks for LHCb HLT to you can use this skript (stole most of it from Hans). It uses bankKiller and EventNodeKiller to clean the old L0Banks and recreates them. At the end it checks for all channel decisions involving muons or hadrons and writes out the events that had any such decision.


#  strip rawdata from a dst, and replace L0DU bank with proper bank
#  Checks for channeldesicions and writes only mu/had triggers

from Gaudi.Configuration import *

importOptions('$STDOPTS/LHCbApplication.opts')   # import old opts file
importOptions('$STDOPTS/DstDicts.opts')          # import old opts file
importOptions('$DDDBROOT/options/DC06.opts')     # import old opts file

# L0 configuration
importOptions('$L0MUONROOT/options/l0muonAlg.opts') # import old opts file
importOptions('$L0DUROOT/options/L0DUConfig.opts')  # import old opts file

#castor cards for mbias L0-stripped events from bookkeeping
#importOptions('/afs/cern.ch/user/d/dijkstra/python/l0-run/mb1-1.opts') # import old opts file
importOptions('$MOOREROOT/options/DC06_L0_v1_lumi2.opts')

# Gaudi.Configuration.Configurables is a ConfFacade - Object
# it is created upon first import of Gaudi.Configuration
# LHCbAlgs.LHCbAlgsConf.EventNodeKiller  /software/releases/LHCB/LHCB_v23r7/InstallArea/python/LHCbAlgs
# L0Calo.L0CaloConf.L0CaloAlg            /software/releases/LBCOM/LBCOM_v6r18/InstallArea/python/L0Calo
# L0Muon.L0MuonConf.L0MuonAlg            /software/releases/LBCOM/LBCOM_v6r18/InstallArea/python/L0Muon
# PuVeto.PuVetoConf.PuVetoAlg            /software/releases/LBCOM/LBCOM_v6r18/InstallArea/python/PuVeto
# L0DU.L0DUConf.L0DUAlg                  /software/releases/LBCOM/LBCOM_v6r18/InstallArea/python/L0DU
# DAQEvent.DAQEventConf.bankKiller       /software/releases/LHCB/LHCB_v23r7/InstallArea/python/DAQEvent
from Configurables import EventNodeKiller,L0CaloAlg,L0MuonAlg,PuVetoAlg,L0DUAlg,bankKiller

RemoveL0Banks = bankKiller('RemoveL0Banks')
RemoveL0Banks.BankTypes =["L0DU", "L0Calo", "L0Muon"]
L0 = GaudiSequencer('L0')

#-----------------------------------------------------
# Run the whole L0 sequence including the L0Processors
#-----------------------------------------------------
L0.Members += [ "bankKiller/RemoveL0Banks"
               ,"L0CaloAlg/L0Calo"
               ,"L0MuonAlg/L0Muon"
               ,"PuVetoAlg/L0PuVeto"
               ,"L0DUAlg/L0DU"
               ,"EventNodeKiller/EventNodeKiller"
]
L0DU = L0DUAlg('L0DU')
L0DU.StoreInBuffer   = True

rawwriter  = OutputStream('RawWriter', Preload = False,
  ItemList = ["/Event#1","/Event/DAQ#1","/Event/DAQ/RawEvent#1","/Event/DAQ/ODIN#1"],
  Output   = "DATAFILE='/local_home/snies/DC06_L0_v1_lumi2_MuonHadron.dst' TYP='POOL_ROOTTREE' OPT='REC' ")
appConf    = ApplicationMgr( OutputLevel = INFO, AppName = 'L0DUextract')
appConf.OutStream = [rawwriter]
appConf.ExtSvc += ['DataOnDemandSvc']
DataOnDemandSvc().Algorithms += ["DATA='/Event/Trig/L0/L0DUReport' TYPE='GaudiSequencer/L0'"]
#appConf.TopAlg    = ['GaudiSequencer/L0']

EventNodeKiller().Nodes    = ["Rec", "MC", "Raw", "Gen", "Link", "pSim" , "Prev", "PrevPrev", "Next"]
EventSelector().PrintFreq  = 50

import GaudiPython
#from gaudigadgets import *

appMgr = GaudiPython.AppMgr()
sel    = appMgr.evtsel()  #Event Selector (could be used to specify input files)
evt    = appMgr.evtsvc()  #Get event service for later use to access the transient event store

appMgr.algorithm('RawWriter').Enable = False  # stop automatic execution of RawWriter

nevent=0  # loop variable
l0acc=0   # number of L0 accepted events

appMgr.run(1) #step one event
while nevent < 100000:
  nevent = nevent + 1 # increase loop variable
  appMgr.run(1)       # read one event

  if evt['Rec/Header'] == None : break  # probably end of input

  # check L0
  L0dir = evt['Trig/L0/L0DUReport']  # get a container
  # get channel decisions
  Hadron = L0dir.channelDecisionByName('Hadron')
  Muon   = L0dir.channelDecisionByName('Muon')
  DiMuon = L0dir.channelDecisionByName('DiMuon')
  MuonNoGlob = L0dir.channelDecisionByName('MuonNoGlob')

  if Hadron or Muon or DiMuon or MuonNoGlob:
    l0acc = l0acc + 1        # count as L0 accepted event
    rc = appMgr.algorithm('RawWriter').execute() # output the L0 accepted event
    # rc probably a return code ? should be checked ?

print 'L0 accepted',l0acc,' in ',nevent,' events.'
print 'Read/Wrote ',nevent,l0acc

interface python and erlang

Friday, June 20th, 2008

Since everybody and their dog is looking into erlang, i didn’ t want to miss the hipe and bought “Programming Erlang, Software for a Concurrent World” by Joe Armstrong.

So for a starter and since we really dig python here, i ported the C-code ports/example1.c on page 207 of the book to python.

#!/usr/bin/python2.4

# example1.py

import sys, struct

def twice(a):
  return 2*a

def sum(a,b):
  return a+b

while sys.stdin:
  try:
    len_raw  = sys.stdin.read(2)
    len      = struct.unpack('h',len_raw)[0]
    data_raw = sys.stdin.read(len)
    data     = struct.unpack(str(len)+'b',data_raw)
    fn       = data[0]
    x = 0
    if fn == 1:
      a = data[1]
      x = twice(a)
    elif fn == 2:
      a = data[1]
      b = data[2]
      x = sum(a,b)
    result = struct.pack('hb',1,x)
    sys.stdout.write(result)
    sys.stdout.flush()
  except:
    break

You can use it in combination with ports/example1.erl by Joe Armstrong,
just rename it to example1 and make it executeable.

Yum Python API

Friday, June 20th, 2008

Running larger sites demands for scripting node installation.

I like to use python for the task (surprise surprise). As it happens the mayor package managment tool for Red Hat, CentOS and Scientific Linux “yum” is itself written in python.

Since yum lacks documentation on how to use inside your own python code I have googled the web and found this nice page: Deciphering the Yum API.

For some reasons the code didn’t work for me on Scientific Linux 4 (Red Hat 3.4.6-9) which still uses python2.3. So I fiddled around a bit and got it working. Here are the updated examples.

Listing packages:

import yum

yb = yum.YumBase()
yb.doConfigSetup()
yb.doTsSetup()
yb.doRpmDBSetup()
for pkg in yb.rpmdb.getPkgList():
  print pkg

Searching packages:

In newer versions of yum there seems to be YumBase.searchGenerator, wich should be prefered for performance and memory footprint. However in the enterprise class distros i have to deal with, I could not use it. So I present a solution with YumBase.searchPackages.

import yum

yb = yum.YumBase()
yb.doConfigSetup()
yb.doRepoSetup()
yb.doSackSetup()
yb.doTsSetup()
yb.doRpmDBSetup()
fields = ['name']        # fields to look at
criteria = ["k3b"]       # strings to find in fields
matches = yb.searchPackages(fields, criteria)
for match in matches:
  print match

Ask if a certain package is installed:

import yum

yb = yum.YumBase()
yb.doConfigSetup()
yb.doTsSetup()
yb.doRpmDBSetup()
# prints 1 if installed else 0
print yb.rpmdb.installed('vim-enhanced')

Installing packages:

Install the editor joe. Note that we do not use the “standard” YumBase but the “command line” YumBaseCli. This class provides the installPkgs() function which magically sorts out which package we want by just saying “joe”. Note also that by using YumBaseCli in this way, we are bypassing some argument checks the command line yum would have done. To make sure yum doesn’t ask any questions we do “ybc.conf.setConfigOption(‘assumeyes’,True)”.
Beware of the consequences.

import sys

sys.path.append('/usr/share/yum-cli')

import cli

ybc = cli.YumBaseCli()
ybc.doConfigSetup()
ybc.doTsSetup()
ybc.doRpmDBSetup()
ybc.installPkgs(['joe'])
ybc.buildTransaction()
ybc.conf.setConfigOption('assumeyes',True)
ybc.doTransaction()