Index: trunk/Mars/Changelog
===================================================================
--- trunk/Mars/Changelog	(revision 9910)
+++ trunk/Mars/Changelog	(revision 9913)
@@ -18,4 +18,12 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2010/08/31 Thomas Bretz
+
+   * msimcamera/MSimRandomPhotons.[h,cc]:
+     - improved the range checks. All ranges are now aligned on the
+       range of simulated wavlengths
+
+
 
  2010/08/30 Thomas Bretz
Index: trunk/Mars/msimcamera/MSimRandomPhotons.cc
===================================================================
--- trunk/Mars/msimcamera/MSimRandomPhotons.cc	(revision 9910)
+++ trunk/Mars/msimcamera/MSimRandomPhotons.cc	(revision 9913)
@@ -150,4 +150,29 @@
 }
 
+Bool_t MSimRandomPhotons::CheckWavelengthRange(const MParSpline &sp, const char *txt) const
+{
+    const Float_t min = sp.GetXmin();
+    const Float_t max = sp.GetXmax();
+
+    if (min>fRunHeader->GetWavelengthMin())
+    {
+        *fLog << err << "ERROR - Minimum wavelength (" << min << "nm)";
+        *fLog << " defined for " << txt;
+        *fLog << " exceeds minimum wavelength simulated (";
+        *fLog << fRunHeader->GetWavelengthMin() << "nm)." << endl;
+        return kFALSE;
+    }
+    if (max<fRunHeader->GetWavelengthMax())
+    {
+        *fLog << err << "ERROR - Maximum wavelength (" << max << "nm)";
+        *fLog << " defined for " << txt;
+        *fLog << " undershoots maximum wavelength simulated (";
+        *fLog << fRunHeader->GetWavelengthMax() << "nm)." << endl;
+        return kFALSE;
+    }
+
+    return kTRUE;
+}
+
 // --------------------------------------------------------------------------
 //
@@ -196,5 +221,5 @@
 
     fRunHeader = (MCorsikaRunHeader*)pList->FindObject("MCorsikaRunHeader");
-    if (fSimulateWavelength && !fRunHeader)
+    if (!fRunHeader)
     {
         *fLog << err << "MCorsikaRunHeader not found... aborting." << endl;
@@ -214,11 +239,28 @@
     const MParSpline *s4 = (MParSpline*)pList->FindObject("ConesAngularAcceptance",    "MParSpline");
 
-    // Multiply all relevant efficiencies to get the total tarnsmission
-    MParSpline *eff = (MParSpline*)s1->Clone();
-    eff->Multiply(*s2->GetSpline());
-    eff->Multiply(*s3->GetSpline());
-
-    // Effectively transmitted wavelength band
-    const Double_t nm = eff && eff->GetSpline() ? eff->GetSpline()->Integral() : 1;
+    // Check if all splines are defined in the relevant range
+    if (!CheckWavelengthRange(*s1, "PhotonDetectionEfficiency [MParSpline]"))
+        return kFALSE;
+    if (!CheckWavelengthRange(*s2, "ConesTransmission [MParSpline]"))
+        return kFALSE;
+    if (!CheckWavelengthRange(*s3, "MirrorReflectivity [MParSpline]"))
+        return kFALSE;
+
+    const Float_t wmin = fRunHeader->GetWavelengthMin();
+    const Float_t wmax = fRunHeader->GetWavelengthMax();
+
+    const Int_t min = TMath::FloorNint(wmin);
+    const Int_t max = TMath::CeilNint(wmax);
+
+    // Multiply all relevant efficiencies to get the total transmission
+    MParSpline eff;
+    eff.SetFunction("1", max-min, min, max);
+
+    eff.Multiply(*s1->GetSpline());
+    eff.Multiply(*s2->GetSpline());
+    eff.Multiply(*s3->GetSpline());
+
+    // Effectively transmitted wavelength band in the simulated range
+    const Double_t nm = eff.GetSpline()->Integral();
 
     // Angular acceptance of the cones
@@ -246,8 +288,5 @@
 
     if (fFileNameNSB.IsNull())
-    {
-        delete eff;
         return kTRUE;
-    }
 
     // const MMcRunHeader *mcrunheader = (MMcRunHeader*)pList->FindObject("MMcRunHeader");
@@ -277,24 +316,6 @@
         return kFALSE;
 
-    const Int_t min = TMath::FloorNint(flux.GetXmin());
-    const Int_t max = TMath::CeilNint( flux.GetXmax());
-
-    if (fRunHeader)
-    {
-        if (min>fRunHeader->GetWavelengthMin())
-        {
-            *fLog << warn << "WARNING - Minimum wavelength of night sky background flux (";
-            *fLog << min << "nm) from " << fFileNameNSB;
-            *fLog << " exceeds minimum wavelength simulated ";
-            *fLog << fRunHeader->GetWavelengthMin() << "nm." << endl;
-        }
-        if (max<fRunHeader->GetWavelengthMax())
-        {
-            *fLog << warn << "WARNING - Maximum wavelength of night sky background flux (";
-            *fLog << max << "nm) from " << fFileNameNSB;
-            *fLog << " undershoots maximum wavelength simulated ";
-            *fLog << fRunHeader->GetWavelengthMax() << "nm." << endl;
-        }
-    }
+    if (!CheckWavelengthRange(flux, TString("night sky background flux from ")+fFileNameNSB))
+        return kFALSE;
 
     MParSpline nsb;
@@ -308,24 +329,30 @@
 
     // Multiply with the total transmission
-    nsb.Multiply(*eff->GetSpline());
-
-    // Check if the photon flux is zero at both ends
+    nsb.Multiply(*eff.GetSpline());
+
+    // Check if the photon flux is zero at both ends of the NSB
     if (nsb.GetSpline()->Eval(min)>1e-5)
     {
-        *fLog << err << "ERROR - Transmitted NSB spectrum at detector at " << min << "nm is not zero... abort." << endl;
-        return kFALSE;
+        *fLog << warn << "WARNING - Transmitted NSB spectrum at detector at ";
+        *fLog << wmin << "nm is not zero, but " << nsb.GetSpline()->Eval(wmin) << "... abort." << endl;
     }
     if (nsb.GetSpline()->Eval(max)>1e-5)
     {
-        *fLog << err << "ERROR - Transmitted NSB spectrum at detector at " << max << "nm is not zero... abort." << endl;
-        return kFALSE;
-    }
-
-    if (fRunHeader)
-    {
-        if (nsb.GetSpline()->Eval(fRunHeader->GetWavelengthMin())>1e-5)
-            *fLog << warn << "WARNING - Transmitted NSB spectrum at detector at " << fRunHeader->GetWavelengthMin() << "nm is not zero... abort." << endl;
-        if (nsb.GetSpline()->Eval(fRunHeader->GetWavelengthMax())>1e-5)
-            *fLog << warn << "WARNING - Transmitted NSB spectrum at detector at " << fRunHeader->GetWavelengthMax() << "nm is not zero... abort." << endl;
+        *fLog << warn << "WARNING - Transmitted NSB spectrum at detector at ";
+        *fLog << wmax << "nm is not zero, but " << nsb.GetSpline()->Eval(wmax) << "... abort." << endl;
+    }
+
+    // Check if the photon flux is zero at both ends of the simulated region
+    if (nsb.GetSpline()->Eval(fRunHeader->GetWavelengthMin())>1e-5)
+    {
+        *fLog << err << "ERROR - Transmitted NSB spectrum at detector at minimum simulated wavelength ";
+        *fLog << fRunHeader->GetWavelengthMin() << "nm is not zero... abort." << endl;
+        return kFALSE;
+    }
+    if (nsb.GetSpline()->Eval(fRunHeader->GetWavelengthMax())>1e-5)
+    {
+        *fLog << err << "ERROR - Transmitted NSB spectrum at detector at maximum simulated wavelength ";
+        *fLog << fRunHeader->GetWavelengthMax() << "nm is not zero... abort." << endl;
+        return kFALSE;
     }
 
@@ -336,7 +363,7 @@
     //const Double_t sr = s5.GetSpline()->IntegralSolidAngle(); // sr
     // Absolute reflector area
-    const Double_t Ar = r->GetA()/1e4;                      // m^2
+    const Double_t Ar = r->GetA()/1e4;                          // m^2
     // Size of the cone's entrance window
-    const Double_t A0 = (*fGeom)[0].GetA()*1e-6;                  // m^2
+    const Double_t A0 = (*fGeom)[0].GetA()*1e-6;                // m^2
 
     // Rate * m^2 * Solid Angle
@@ -360,6 +387,6 @@
 
     // Resulting rates as if Razmick's constant had been used
-    // *fLog << 1.75e6/(600-300) * f1 * eff->GetSpline()->Integral() << " MHz" << endl;
-    // *fLog << 1.75e6/(600-300) * f2 * eff->GetSpline()->Integral() << " MHz" << endl;
+    // *fLog << 1.75e6/(600-300) * f1 * eff.GetSpline()->Integral() << " MHz" << endl;
+    // *fLog << 1.75e6/(600-300) * f2 * eff.GetSpline()->Integral() << " MHz" << endl;
 
     *fLog << "Conversion factor Fnu:         " << f  << endl;
@@ -385,6 +412,4 @@
     }
 
-    delete eff;
-
     return kTRUE;
 }
Index: trunk/Mars/msimcamera/MSimRandomPhotons.h
===================================================================
--- trunk/Mars/msimcamera/MSimRandomPhotons.h	(revision 9910)
+++ trunk/Mars/msimcamera/MSimRandomPhotons.h	(revision 9913)
@@ -8,7 +8,7 @@
 class MGeomCam;
 class MParList;
+class MParSpline;
 class MPhotonEvent;
 class MPhotonStatistics;
-//class MCorsikaEvtHeader;
 class MCorsikaRunHeader;
 class MPedestalCam;
@@ -43,4 +43,7 @@
     Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print=kFALSE);
 
+    // MSimRandomPhotons
+    Bool_t CheckWavelengthRange(const MParSpline &sp, const char *txt) const;
+
 public:
     MSimRandomPhotons(const char *name=NULL, const char *title=NULL);
