Print

Print


Hi Tony,

thanks for you note and especially the example code. I have used the
method 3. to get what I need for now (I had a clumsier version of this
working before).

I am still confused by the use of the histogram(name) method -- the root
of my original question. It looks like it returns a Histogram object
that is trapped at compile time since (as per item 3.) one cannot use
the statistics methods on Histogram directly. This seems to contradict
example 1. (which would be very useful).

"Johnson, Tony S." wrote:

> 1) The histogram(name) method in EventAnalyzer returns a histogram.
> You can do anything with that histogram that you would do with a
> histogram created using new Histogram(name). E.g.
> histogram("xyz").fill(10);        double mean =
> histogram("xyz").getMean();        Histogram xyz = histogram("xyz");

// Example of histogram method access problem
// Bob Wilson. 10/21/00

import hep.analysis.*;
import hep.analysis.partition.*;
import hep.physics.*;

import hep.lcd.event.*;
import hep.lcd.util.driver.*;

import java.util.*;
import java.io.*;

final public class MCAnal_driverTest extends Driver
{
 public MCAnal_driverTest() throws IOException
    {
       ParticleProperties.setParticlePropertyProvider(new
PythiaParticlePropertyProvider());
       MCGenAnalTest genanal = new MCGenAnalTest();
       add(genanal);
    }
}


class MCGenAnalTest extends AbstractProcessor
{
   LCDEvent _data = null;

   public void process(LCDEvent event)
 {
   final LCDEvent data = (LCDEvent) event;
   _data = (LCDEvent) event;

  ParticleVector      pv = _data.getMCParticles();
  ParticleEnumeration e  = pv.particles();
  int num_pi = 0;

   while (e.hasMoreElements())
  {
  try
   {
    Particle   pc = e.nextParticle();
    ParticleType particle_type = pc.getType();
    int abspid = Math.abs(particle_type.getPDGID());

    if ( abspid == 211 ) {num_pi++;}

   }
  catch (UnknownParticleID x) {}

  }

  histogram("Npi").fill(num_pi);
double piMean = histogram("Npi").getMean();

  }
}

Compilation message:

----------- Compiling /data/home/bobw/jas/MCAnal_driverTest.java
/data/home/bobw/jas/MCAnal_driverTest.java:53: Method getMean() not
found in class hep.analysis.Histogram.
 double piMean = histogram("Npi").getMean();
                                         ^
1 error
----------- compile failed

One can fool the compiler with a cast:

 histogram("Npi").fill(num_pi);
 Histogram1D hist1 = (Histogram1D) histogram("Npi");
 double piMean = hist1.getMean();

but this crashes at run time.

Finally, for my continuing Java education ....

> 4) You can rename histograms with the rename method, but you must
> catch the RenameException, since some histograms may not support being
> renamed. In this instance you probably don't really want to use
> renaming.I've attached an example which illustrates all of this.Tony

In the catch of the following:

 histogram("Npi").fill(num_pi);
 try
 {
  histogram("Npi").rename("Fred");
 }
 catch (hep.analysis.NamedObject.RenameException reason)
{
  // Announce the rename problem here
 }

I get a compiler error if I use simply

  catch (RenameException reason)

(similar to the use of UnknownParticleID). Since NamedObject is in
hep.analysis, I thought that
    import hep.analysis.*;
in the header would allow the short form. Why not?

Thanks,
Bob