Skip to content

Groovy syntactic sugar

Andrey edited this page Oct 3, 2021 · 2 revisions

The code for Groovy syntactic sugar

  1. Place the code below in Sugar.groovy file and place in the directory my: my/Sugar.groovy
  2. Compile the classes: groovyc -d classes my/Sugar.groovy
  3. Make jar file: jar cvf sugar.jar -C classes .
  4. Place jar file sugar.jar in groovy's default lib location, e.g. $HOME/.groovy/lib
package my

import org.jlab.groot.data.H1F
import org.jlab.groot.data.H2F
import org.jlab.groot.data.TDirectory
import org.jlab.clas.physics.LorentzVector
import org.jlab.clas.pdg.PDGDatabase

class Sugar {
  static void enable() {
    H2F.metaClass.projectionX = {name,title=""->
      H1F h1 = delegate.projectionX()
      h1.setName(name)
      h1.setTitle(title)
      return h1
    }

    H2F.metaClass.projectionY = {name,title=""->
      H1F h1 = delegate.projectionY()
      h1.setName(name)
      h1.setTitle(title)
      return h1
    }

    TDirectory.metaClass.writeDataSet = {
      def pwd = delegate.getDir().getDirectoryPath()
      it.getName().split('/').dropRight(1).findAll().each{
        delegate.mkdir(it)
        delegate.cd(it)
      }
      delegate.add(it.getName().split('/')[-1], it)
      if(pwd=='/') delegate.cd()
      else delegate.cd(pwd)
    }

    LorentzVector.metaClass.static.withPID = {pid,x,y,z->
      double m = PDGDatabase.getParticleMass(pid)
      double e = Math.sqrt(x*x+y*y+z*z+m*m)
      return new LorentzVector(x,y,z,e)
    }

    LorentzVector.metaClass.plus = {
      LorentzVector a = new LorentzVector(delegate)
      a.add(it)
      return a
    }

    LorentzVector.metaClass.minus = {
      LorentzVector a = new LorentzVector(delegate)
      a.sub(it)
      return a
    }

  }
}

Clone this wiki locally