Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 2580)
+++ trunk/MagicSoft/Mars/Changelog	(revision 2581)
@@ -4,4 +4,25 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2003/12/01: Markus Gaug
+ 
+   * macros/calibration.C:
+     - adjusted some parameters
+
+   * manalysis/MCalibrationCalc.cc:
+     - cosmics are now skipped from the fit
+
+   * manalysis/MCalibrationPix:
+     - cosmics are now skipped from the fit
+
+   * mhist/MHCalibrationPixel:
+     - cosmics are now skipped from the fit
+
+   * manalysis/Makefile
+     - MPedCalcPedRun was not in Makefile
+
+   * manalysis/AnalysisLinkDef
+     - MPedCalcPedRun was not in AnalysisLinkDef
+
 
  2003/12/01: Thomas Bretz
Index: trunk/MagicSoft/Mars/macros/calibration.C
===================================================================
--- trunk/MagicSoft/Mars/macros/calibration.C	(revision 2580)
+++ trunk/MagicSoft/Mars/macros/calibration.C	(revision 2581)
@@ -23,6 +23,6 @@
 \* ======================================================================== */
 
-void calibration(TString pedname="./20031102_02399_P_Unavailable_E.root", 
-                 TString calname="./20031102_02400_D_Flip500Hz_E.root")
+void calibration(TString pedname="../../Mars-0.8.2/20031102_02399_P_Unavailable_E.root", 
+                 TString calname="../../Mars-0.8.2/20031102_02400_D_Flip500Hz_E.root")
 {
 
@@ -45,7 +45,6 @@
 
     MGeomApply     geomapl;
+    MPedCalcPedRun pedcalc;
     MGeomCamMagic  geomcam;
-    MGeomCam       geom;
-    MPedCalcPedRun pedcalc;
     MPedestalCam   pedcam;
 
@@ -57,5 +56,5 @@
 
     MHCamEvent hist("Pedestal");
-    hist.SetType(1);
+    hist.SetType(0);
     plist.AddToList(&hist);
     MFillH fill(&hist, "MPedestalCam");
@@ -63,11 +62,8 @@
     tlist.AddToList(&fill);
 
-    //
-    // Update frequency by default = 1Hz
-    //
-    MStatusDisplay *d = new MStatusDisplay;
+    MStatusDisplay *d1 = new MStatusDisplay;
  
     // Set update time to 3s
-    d->SetUpdateTime(3000);
+    d1->SetUpdateTime(3000);
  
     //
@@ -76,5 +72,5 @@
     MEvtLoop evtloop;
     evtloop.SetParList(&plist);
-    evtloop.SetDisplay(d);  	
+    evtloop.SetDisplay(d1);  	
 
     //
@@ -86,4 +82,7 @@
     tlist.PrintStatistics();
 
+    MPedestalCam *ped = plist.FindObject("MPedestalCam");
+    ped.Print();
+
     //
     // Create a empty Parameter List and an empty Task List
@@ -94,7 +93,8 @@
     plist2.AddToList(&tlist2);
 
+
     plist2.AddToList((MPedestalCam*)plist.FindObject("MPedestalCam"));
 
-    MGeomApply geomapl2;
+//    MGeomApply geomapl2;
     tlist2.AddToList(&geomapl);
 
@@ -108,5 +108,5 @@
 
     MCalibrationCalc calcalc;
-    calcalc.SetSkipTFits();
+//    calcalc.SetSkipTFits();
 
     plist2.AddToList(&geomcam);
@@ -114,5 +114,5 @@
     //
     // As long, as we don't have digital modules, 
-    // we have to set the color by hand
+    // we have to set the color of the pulser LED by hand
     //
     calcalc.SetPulserColor(MCalibrationCalc::kEBlue);
@@ -141,6 +141,4 @@
     tlist2.PrintStatistics();
 
- //   plist2.FindObject("MCalibrationCam")->Print();
-
     //
     // just one example how to get the plots of individual pixels
@@ -148,6 +146,12 @@
     MCalibrationCam *cam = plist2.FindObject("MCalibrationCam");
     MCalibrationPix *pix = cam->GetCalibrationPix(523);
-    //pix->Draw();
-
+    pix->Draw();
+
+    //
+    // Here we are confronted to a serious bug in ROOT:
+    // If we do not apply the next command, gPad will get
+    // screwed up completely: (Thanks to tbretz for finding out
+    // the reason during several hours!!!)
+    //
     gROOT->GetListOfCanvases()->Delete();
 
@@ -193,8 +197,10 @@
     disp12.SetYTitle("\\Delta_{Q} [FADC counts]");
 
-//    TText text(0.1, 0.5, &fname[fname.Last('/')+1]);
-//    text.SetTextSize(0.015);
-//    text.DrawClone();
-    TCanvas *c1 = &d->AddTab("Cal1");//MH::MakeDefCanvas("Calibration1","Plots of FADC Charges",700,900);
+    MStatusDisplay *d2 = new MStatusDisplay;
+ 
+    // Set update time to 1s
+    d2->SetUpdateTime(1000);
+
+    TCanvas *c1 = &d2->AddTab("Fitted Charges"); 
     c1->Divide(5, 2);
 
@@ -241,5 +247,5 @@
     obj->Draw();
 
-    TCanvas *c2 = &d->AddTab("Cal2");//MH::MakeDefCanvas("Calibration2","Plots of Arrival Times",700,900);
+    TCanvas *c2 = &d2->AddTab("Fitted Times");
     c2->Divide(3, 2);
 
@@ -265,5 +271,5 @@
     obj->Draw();
 
-    TCanvas *c3 = &d->AddTab("Cal3");//MH::MakeDefCanvas("Calibration3","Plots of Pedestals",700,900);
+    TCanvas *c3 = &d2->AddTab("Pedestals");
     c3->Divide(2, 2);
 
@@ -282,5 +288,5 @@
     obj->Draw();
 
-    TCanvas *c4 = &d->AddTab("Cal4");//MH::MakeDefCanvas("Calibration4","Plots of Reduced Charges",700,900);
+    TCanvas *c4 = &d2->AddTab("Reduced Charges");
     c4->Divide(2, 2);
 
@@ -299,6 +305,5 @@
     obj->Draw();
 
-//    c->SaveAs(fname(0, fname.Last('.')+1) + "ps");
-    //c->SaveAs(fname(0, fname.Last('.')+1) + "root");
+#endif
 
 }
Index: trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h	(revision 2580)
+++ trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h	(revision 2581)
@@ -20,4 +20,5 @@
 #pragma link C++ class MPedestalPix+;
 #pragma link C++ class MPedestalCam+;
+#pragma link C++ class MPedCalcPedRun+;
 #pragma link C++ class MMcPedestalCopy+;
 #pragma link C++ class MMcPedestalNSBAdd+;
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc	(revision 2580)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc	(revision 2581)
@@ -136,6 +136,7 @@
 {
 
-    fOverFlow = 0;
-    fNrEvents = 0;
+    fHistOverFlow = 0;
+    fEvents       = 0;
+    fCosmics      = 0;
 
     fRawEvt = (MRawEvtData*)pList->FindObject("MRawEvtData");
@@ -235,5 +236,7 @@
 {
 
-    fNrEvents++;
+    fEvents++;
+
+    Bool_t cosmic = kFALSE;
 
     MCalibrationBlindPix &blindpixel = *(fCalibrations->GetBlindPixel());
@@ -267,30 +270,23 @@
             mid = pixel.GetIdxMaxLoGainSample();
 
-            sum = (max == gkSaturationLimit          // overflow of LoGain ??? -> GimmeABreak!!!
-                   ? fOverFlow++, gkLoGainOverFlow   // OUCH (Florian was maybe right)
-                   : sum*gkConversionHiLo    );      // OUFF (Florian was wrong) !! 
+            sum = (max == gkSaturationLimit              // overflow of LoGain ??? -> GimmeABreak!!!
+                   ? fHistOverFlow++, gkLoGainOverFlow   // OUCH (Florian was maybe right)
+                   : sum*gkConversionHiLo    );          // OUFF (Florian was wrong) !! 
 
             //            *fLog << warn << "Warning: Saturation of HiGain reached in slice " << (int)mid << " !!! " << endl;
             //            *fLog << warn << "Max = " << max << endl;
 
-            if (fOverFlow) 
+            if (fHistOverFlow) 
               *fLog << err << dbginf << "Warning: Saturation of LoGain reached! " 
                     << err << dbginf << "sum = " << sum << endl;
 
           }
-        
-        //
-        // sanity check (old MC files sometimes have pixids>577)
-        //
-        if (fPedestals && !fPedestals->CheckBounds(pixid))
-          {
-            *fLog << inf << "Pixel ID larger than camera... skipping event." << endl;
-            return kCONTINUE;
-              }
-        
+	
+
         MPedestalPix    &ped = (*fPedestals)[pixid];
         MCalibrationPix &pix = (*fCalibrations)[pixid];
         
-        Float_t pedes = ped.GetPedestal();
+        Float_t pedes  = ped.GetPedestal();
+        Float_t pedrms = ped.GetPedestalRms();
         
         //
@@ -298,5 +294,17 @@
         // Assume pedestals per time slice ==> multiply with number of slices
         //
-        pedes *= (sat ? fNumLoGainSamples : fNumHiGainSamples );
+        pedes  *= (sat ? fNumLoGainSamples : fNumHiGainSamples );
+        pedrms *= (sat ? fNumLoGainSamples : fNumHiGainSamples );
+
+	// 
+	// This is a very primitive check for the number of cosmics
+	// The cut will be applied in the fit, but for the blind pixel,
+	// we need to remove this event
+	//
+	// FIXME: In the future need a much more sophisticated one!!!
+	//
+
+	if ((float)sum < pedes+5.*pedrms)
+           cosmic = kTRUE;
 
         Float_t rsum      = (float)sum - pedes;
@@ -306,13 +314,20 @@
             
           case gkCalibrationBlindPixelId:
-            if (!blindpixel.FillQ(sum)) 
-              *fLog << warn << 
-                "Overflow or Underflow occurred filling Blind Pixel means = " << sum << endl;
+	    // 
+	    // FIXME: This works only when the blind pixel ID is much larger than 
+	    //        the rest of the pixels (which is the case right now)
+	    //
+//	    if (!cosmic)
+	       if (!blindpixel.FillQ(sum)) 
+                  *fLog << warn << 
+                  "Overflow or Underflow occurred filling Blind Pixel sum = " << sum << endl;
+
             if (!blindpixel.FillT((int)mid)) 
               *fLog << warn << 
                 "Overflow or Underflow occurred filling Blind Pixel time = " << (int)mid << endl;
-            if (!blindpixel.FillRQvsT(rsum,fNrEvents))
+
+            if (!blindpixel.FillRQvsT(rsum,fEvents))
               *fLog << warn << 
-                "Overflow or Underflow occurred filling Blind Pixel eventnr = " << fNrEvents << endl;
+                "Overflow or Underflow occurred filling Blind Pixel eventnr = " << fEvents << endl;
 
           case gkCalibrationPINDiodeId:
@@ -323,7 +338,7 @@
               *fLog << warn << 
                 "Overflow or Underflow occurred filling HT: time = " << (int)mid << endl;
-            if (!pindiode.FillRQvsT(rsum,fNrEvents))
+            if (!pindiode.FillRQvsT(rsum,fEvents))
               *fLog << warn << 
-                "Overflow or Underflow occurred filling HQvsN: eventnr = " << fNrEvents << endl;
+                "Overflow or Underflow occurred filling HQvsN: eventnr = " << fEvents << endl;
 
           default:
@@ -336,7 +351,7 @@
             // Fill the reduced charge into the control histo for better visibility
             //
-            if (!pix.FillRQvsT(rsum,fNrEvents))
+            if (!pix.FillRQvsT(rsum,fEvents))
               *fLog << warn << "Could not fill red. Q vs. EvtNr of pixel: " << pixid 
-                    << " signal = " << rsum  << " event Nr: " << fNrEvents << endl;
+                    << " signal = " << rsum  << " event Nr: " << fEvents << endl;
 
 
@@ -348,4 +363,7 @@
 
       } /* while (pixel.Next()) */
+
+    if (cosmic)
+	fCosmics++;
 
     fCalibrations->SetReadyToSave();
@@ -398,4 +416,8 @@
           MCalibrationPix &pix = (*fCalibrations)[pixid];
 
+          const Float_t ped    = (*fPedestals)[pixid].GetPedestal() * fNumHiGainSamples;
+          const Float_t prms   = (*fPedestals)[pixid].GetPedestalRms() * fNumHiGainSamples;
+
+          pix.SetPedestal(ped,prms);
 
           if (TESTBIT(fFlags,kUseTFits))
@@ -405,7 +427,4 @@
              continue;
 
-          const Float_t ped    = (*fPedestals)[pixid].GetPedestal();
-          const Float_t prms   = (*fPedestals)[pixid].GetPedestalRms();
-          pix.SetPedestal(ped,prms);
         }
     }
@@ -413,12 +432,10 @@
   fCalibrations->SetReadyToSave();
   
-  if (GetNumExecutions()==0 || fOverFlow==0)
+  if (GetNumExecutions()==0)
     return kTRUE;
   
-  *fLog << inf << endl;
-  *fLog << GetDescriptor() << " execution statistics:" << endl;
-  *fLog << dec << setfill(' ');
-  *fLog << " " << setw(7) << fOverFlow << " (" << setw(3) << (int)(fOverFlow*100/GetNumExecutions()) << "%) Evts skipped due to: lo gain saturated." << endl;
-  
+  *fLog << endl;
+  *fLog << dec << setfill(' ') << fCosmics << " Events presumably cosmics" << endl;
+
   return kTRUE;
 }
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.h	(revision 2580)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.h	(revision 2581)
@@ -35,6 +35,7 @@
   MTime                    *fEvtTime;      // Time of the event
 
-  Int_t fNrEvents;                         // Number of events  
-  Int_t fOverFlow;                         // Number of events with saturated Low Gain
+  Int_t fEvents;                           // Number of events  
+  Int_t fHistOverFlow;                     // Number of events with saturated Low Gain
+  Int_t fCosmics;                          // Number of events due to supposed cosmics
 
   Byte_t fNumHiGainSamples; 
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationPix.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationPix.cc	(revision 2580)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationPix.cc	(revision 2581)
@@ -83,4 +83,9 @@
     return kTRUE;
 
+  if (fPed && fPedRms)
+    fHist->SetLowerFitRange(fPed + 3.5*fPedRms);
+  else
+    *fLog << warn << "Cannot set lower fit range to suppress cosmics: Pedestals not available" << endl;
+
   if(!fHist->FitQ())
     {
Index: trunk/MagicSoft/Mars/manalysis/Makefile
===================================================================
--- trunk/MagicSoft/Mars/manalysis/Makefile	(revision 2580)
+++ trunk/MagicSoft/Mars/manalysis/Makefile	(revision 2581)
@@ -33,4 +33,5 @@
 SRCFILES = MPedestalPix.cc \
            MPedestalCam.cc \
+           MPedCalcPedRun.cc \
            MMcPedestalCopy.cc \
            MMcPedestalNSBAdd.cc \
Index: trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.cc	(revision 2580)
+++ trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.cc	(revision 2581)
@@ -55,4 +55,5 @@
 
 using namespace std;
+
 // --------------------------------------------------------------------------
 //
@@ -60,6 +61,11 @@
 //
 MHCalibrationPixel::MHCalibrationPixel(Int_t pix, const char *name, const char *title)
-    : fFitOK(kFALSE), fPixId(pix), fTGausFit(NULL), fQGausFit(NULL), fFitLegend(NULL)
-{
+      : fPixId(pix),
+        fQGausFit(NULL), 
+	fTGausFit(NULL), 
+	fFitLegend(NULL),
+	fLowerFitRange(0.),
+        fFitOK(kFALSE)
+{ 
 
     fName  = name  ? name  : "MHCalibrationPixel";
@@ -328,5 +334,5 @@
 }
 
-Bool_t MHCalibrationPixel::FitQ(Axis_t rmin, Axis_t rmax, Option_t *option)
+Bool_t MHCalibrationPixel::FitQ(Option_t *option)
 {
 
@@ -337,6 +343,5 @@
   // Get the fitting ranges
   //
-  rmin = (rmin != 0.) ? rmin : fQfirst;
-  rmax = (rmax != 0.) ? rmax : fQlast;
+  Axis_t rmin = (fLowerFitRange != 0.) ? fLowerFitRange : fQfirst;
 
   //
@@ -349,5 +354,5 @@
   const Double_t si_guess = mu_guess/500.;
 
-  fQGausFit = new TF1("QGausFit","gaus",rmin,rmax);
+  fQGausFit = new TF1("QGausFit","gaus",rmin,fQlast);
 
   if (!fQGausFit) 
@@ -360,6 +365,6 @@
   fQGausFit->SetParNames("Area","#mu","#sigma");
   fQGausFit->SetParLimits(0,0.,entries);
-  fQGausFit->SetParLimits(1,rmin,rmax);
-  fQGausFit->SetParLimits(2,0.,rmax-rmin);
+  fQGausFit->SetParLimits(1,rmin,fQlast);
+  fQGausFit->SetParLimits(2,0.,fQlast-rmin);
 
   fHQ->Fit("QGausFit",option);
Index: trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.h	(revision 2580)
+++ trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.h	(revision 2581)
@@ -41,4 +41,5 @@
   TPaveText *fFitLegend;  
   
+  Axis_t  fLowerFitRange;
   Axis_t  fQfirst;
   Axis_t  fQlast;
@@ -104,5 +105,5 @@
   const TH1I *GetHQvsN() const { return fHQvsN; }
   
-  Bool_t FitQ(Axis_t rmin=0, Axis_t rmax=0, Option_t *option="RQ0+");  
+  Bool_t FitQ(Option_t *option="RQ0+");  
   Bool_t FitT(Axis_t rmin=0, Axis_t rmax=0, Option_t *option="RQ0+");    
 
@@ -110,4 +111,6 @@
   virtual void CutAllEdges();
   virtual void Reset();
+
+  void SetLowerFitRange(Axis_t min)  { fLowerFitRange = min; }
 
   void PrintQFitResult();
