/**
  * Generates a new instance of material with given characteristics
  *
  * @param name is material identifier
  * @param density is material density
  * @param composition is atomic composition by weight of material
  * @return null if material cannot be created
  */
 public static Material newMaterial(
     String name, double density, WeightedAtomicComposition composition) {
   Material material = new Material();
   material.setName(name);
   material.setDensity(density);
   material.setWeightedAtomicComposition(composition);
   return material;
 }
  public SingleMaterialMonteCarlo() {
    double energyEV = 1000000;

    System.out.println("energy " + energyEV / 1000000 + " MeV");

    double photo =
        material.getAttenuation(energyEV / 1000, AttenuationType.PHOTOELECTRIC_ABSORPTION);
    double compton =
        material.getAttenuation(energyEV / 1000, AttenuationType.INCOHERENT_ATTENUATION);
    System.out.println("cross section photo: " + photo + "cm" + " compton " + compton + "cm");
    System.out.println("mean free path compton " + 1 / compton + "cm");
    System.out.println("mean free path photo " + 1 / photo + "cm");

    double totalCrossSection =
        material.getAttenuation(
            energyEV / 1000, AttenuationType.TOTAL_WITHOUT_COHERENT_ATTENUATION);
    System.out.println("total attenuation " + 1 / totalCrossSection + "cm");
    System.out.println("total attenuation sum " + 1 / (photo + compton) + "cm");

    raytrace(energyEV, numRays);

    double mean = XRayTracer.computeMean(pathlengths);
    double stddev = XRayTracer.computeStddev(pathlengths, mean);
    System.out.println("pathlength = " + mean + " +- " + stddev);

    mean = XRayTracer.computeMean(xs);
    stddev = XRayTracer.computeStddev(xs, mean);
    System.out.println("x = " + mean + " +- " + stddev);

    mean = XRayTracer.computeMean(ys);
    stddev = XRayTracer.computeStddev(ys, mean);
    System.out.println("y = " + mean + " +- " + stddev);

    mean = XRayTracer.computeMean(zs);
    stddev = XRayTracer.computeStddev(zs, mean);
    System.out.println("z = " + mean + " +- " + stddev);

    // rayTrace(800000, numRays);

    // visualize the klein-nishina angle distribution and the rejection
    // sampling for different energies
    //		 visualizeKleinNishina(10000000*eV);
    visualizeKleinNishina(140000 * eV);

    // visualizeKleinNishina(2.75*eV);
    //
    // visualizeKleinNishina(55000*eV);

  }
  private void raytrace(double energyEV, int numRays) {
    Grid2D grid = new Grid2D(600, 500);
    FloatProcessor imp;
    imp = new FloatProcessor(grid.getWidth(), grid.getHeight());
    imp.setPixels(grid.getBuffer());

    //		 SimpleVector startPosition = new SimpleVector(40,grid.getHeight()/2/scale, 0);
    SimpleVector startPosition = new SimpleVector(0, 0, 0);

    for (int i = 0; i < numRays; ++i) {
      followRay(startPosition.clone(), new SimpleVector(1, 0, 0), energyEV, imp, 0, 0);
    }
    imp.drawString(
        grid.getWidth() / scale + "cm",
        grid.getWidth() / 2 - 20,
        grid.getHeight() - 10,
        Color.WHITE);

    grid.show("Energy: " + energyEV + "eV, Material: " + material.getName());

    System.out.println(
        "Rejection sampling: average misses per draw: " + (float) sumMisses / (float) numDraws);

    numDraws = 0;
    sumMisses = 0;
  }
  private void followRay(
      SimpleVector pos,
      SimpleVector dir,
      double energyEV,
      FloatProcessor imp,
      int scatterCount,
      double totalDistance) {
    if (energyEV <= 1 || scatterCount > 20000) {
      System.out.println("energy low, times scattered: " + scatterCount);
      return;
    }
    // follow ray until next interaction point
    SimpleVector oldPos = pos.clone();
    double dist = sampler.getDistanceUntilNextInteractionCm(material, energyEV);
    pos.add(dir.multipliedBy(dist));
    pathlengths.add(dist);
    // draw the entire path
    // imp.drawLine((int)(scale*oldPos.getElement(0)), (int)(scale*oldPos.getElement(1)),
    // (int)(scale*pos.getElement(0)), (int)(scale*pos.getElement(1)));
    // draw interaction points only
    imp.drawDot((int) (scale * pos.getElement(0)), (int) (scale * pos.getElement(1)));

    // choose compton or photoelectric effect
    double photo =
        material.getAttenuation(energyEV / 1000, AttenuationType.PHOTOELECTRIC_ABSORPTION);
    double compton =
        material.getAttenuation(energyEV / 1000, AttenuationType.INCOHERENT_ATTENUATION);

    if (sampler.random() * (photo + compton) <= photo) {
      // photoelectric absorption
      energyEV = 0;
      // System.out.println("absorbed after " + scatterCount + " collisions");
      xs.add(pos.getElement(0));
      ys.add(pos.getElement(1));
      zs.add(pos.getElement(2));

      return;
    } else {
      // compton scattering

      energyEV = sampler.sampleComptonScattering(energyEV, dir);

      // send new ray
      followRay(pos, dir, energyEV, imp, scatterCount + 1, totalDistance + dist);
    }
  }
 public static synchronized void updateAttenuationCoefficientsMap(
     HashMap<Material, double[]> attenuationCoefficientsMap,
     double[] energies,
     Material mat,
     AttenuationType att) {
   double[] attenuation = new double[energies.length];
   try {
     for (int j = 0; j < attenuation.length; j++) {
       attenuation[j] = mat.getAttenuation(energies[j], att);
     }
     attenuationCoefficientsMap.put(mat, attenuation);
   } catch (NoSuchElementException e) {
     System.out.println(
         "Skipping " + mat + " as attenuation data is incomplete for the configured energies.");
   } catch (NullPointerException e2) {
     System.out.println("Skipping " + mat + " as attenuation data is incomplete.");
   }
 }