Index: fact/tools/rootmacros/PulseTemplates/FPulseOverlay.C
===================================================================
--- fact/tools/rootmacros/PulseTemplates/FPulseOverlay.C	(revision 13472)
+++ fact/tools/rootmacros/PulseTemplates/FPulseOverlay.C	(revision 13485)
@@ -143,20 +143,22 @@
 
     // Temporary Objects
-    TH1F*       debugHistos = NULL;
+    TH1F*       debugHistos     = NULL;
     //TH2F*       hOverlayTemp = NULL;
     //TH1D*       hProjPeak = NULL;
-    TH1F*       hTesthisto = NULL;
-    TH2F*       hTesthisto2 = NULL;
+    TH1F*       hTesthisto      = NULL;
+    TH2F*       hTesthisto2     = NULL;
 
     //Histogram Parameters
-    Int_t       gPixelOverlayXaxisLeft = 0;
+    Int_t       gPixelOverlayXaxisLeft  = 0;
     Int_t       gPixelOverlayXaxisRight = 0;
 
     //Root-File Objects
 //    TObjArray*  hList[sampleSetSize] = {NULL};
-    TObjArray*  hRootList = NULL;
-
-    TCanvas**   cgpPixelPulses  = NULL;
-    TCanvas *   cFiltered       = NULL;
+    TObjArray*  hRootList           = NULL;
+
+    TCanvas**   cgpPixelPulses      = NULL;
+    TCanvas**   cgpDistributions    = NULL;
+    TCanvas*    cFiltered           = NULL;
+    TCanvas*    cgpTestHistos       = NULL;
 
 //----------------------------------------------------------------------------
@@ -165,5 +167,4 @@
 
 void BookDebugHistos(int );
-//void BookPixelHistos(int, int, int, int );
 void BookTestHistos( int );
 
@@ -172,6 +173,6 @@
 
 void FillHistograms(Pixel*, vector<Region>*, int, int, int, int);
-//void DrawPulseHistograms(int);
 void DrawTestHistograms( int);
+void ProduceDebugHistos( vector<Region> *pZXings);
 
 void UpdateCanvases( int, int);
@@ -209,25 +210,19 @@
 {
 //----------------------------------------------------------------------------
-// set global variables
+// Initialize Pixel
+//----------------------------------------------------------------------------
+    Pixel** pixel = new Pixel*[sampleSetSize];
+
+//----------------------------------------------------------------------------
+//	Save-Root-File Settings
+//----------------------------------------------------------------------------
+    CreateRootFile( OutRootFileName, verbosityLevel );
+
+//----------------------------------------------------------------------------
+//	root global Settings
 //----------------------------------------------------------------------------
 
     gGainMean = GainMean;
     gBSLMean = BSLMean;
-
-//----------------------------------------------------------------------------
-// Initialize Pixel
-//----------------------------------------------------------------------------
-    Pixel** pPixelSet = new Pixel*[sampleSetSize];
-
-//----------------------------------------------------------------------------
-//	Save-Root-File Settings
-//----------------------------------------------------------------------------
-    CreateRootFile( OutRootFileName, verbosityLevel );
-
-
-//----------------------------------------------------------------------------
-//	root global Settings
-//----------------------------------------------------------------------------
-
     gPixelOverlayXaxisLeft = OverlayWindowLeft;
     gPixelOverlayXaxisRight = OverlayWindowRight;
@@ -237,4 +232,8 @@
 //    gPad->SetGrid();
 
+//----------------------------------------------------------------------------
+//	root Canvas Settings
+//----------------------------------------------------------------------------
+    //Canvas Pad numbering
     int pulsesCanvasFrameNrs[4] = {
         1,  // Top left
@@ -244,14 +243,24 @@
     };
 
-    //Canvases
-    cgpPixelPulses = new TCanvas*[maxPulseOrder];
-    TCanvas*    cgpTestHistos                   = NULL;
+    //Canvas Pad numbering
+    int distributionCanvasFrameNrs[4] = {
+        1,  // Top left
+        2,  // Top right
+        3,  // bottom left
+        4   // bootom right
+    };
 
     if (ProduceGraphic)
     {
+
+        //Canvases
+        cgpPixelPulses      = new TCanvas*[maxPulseOrder];
+        cgpDistributions    = new TCanvas*[maxPulseOrder];
+
         //TCanvas*    gpcDevelopment = NULL;
         TString cName   = "";
         TString cTitle  = "";
 
+        //naming of pulse canvases
         for (
              int pulse_order = maxPulseOrder - 1;
@@ -268,9 +277,9 @@
         }
 
+
         // Create (pointer to) Canvases, which are used in every run,
         // also in 'non-debug' runs
                 // Canvases only need if spike Debug, but I want to deklare
                 // the pointers anyway ...
-
         if (spikeDebug)
         {
@@ -279,7 +288,9 @@
         }
 
+        //additional Test histograms
         cgpTestHistos = new TCanvas( "cgpTestHistos", "Test Histograms", 801, 0, 800, 800 );
         cgpTestHistos->Divide(2,0);
     }
+
 //-----------------------------------------------------------------------------
 // Filter-Settings
@@ -357,7 +368,16 @@
     }
 
-    // Book the histograms
+//-----------------------------------------------------------------------------
+// Book the histograms
+//-----------------------------------------------------------------------------
+
     BookDebugHistos(verbosityLevel );
     BookTestHistos( verbosityLevel );
+
+
+//-----------------------------------------------------------------------------
+// Main Cycle
+//-----------------------------------------------------------------------------
+
 //-------------------------------------
 // Loop over Pixel Sets
@@ -376,13 +396,7 @@
                  << firstPixelOfSample+sampleSetSize-1 << endl;
         }
-//        BookPixelHistos(
-//                    sampleSetSize,
-//                    firstPixelOfSample,
-//                    maxPulseOrder,
-//                    verbosityLevel
-//                    );
 
 //--------------------------------------------------------------------
-// Loops over Every Event of Pixel
+// Loop over every Event of Pixel Set
 //--------------------------------------------------------------------
         for ( int ev = firstevent; ev < firstevent + nevents; ev++)
@@ -401,25 +415,31 @@
 // Loops over every Pixel of a Set of Pixels
 //--------------------------------------------------------------------
-            for ( int pixel = firstPixelOfSample;
-                  pixel < sampleSetSize || pixel < firstpixel + npixel;
-                pixel++ )
+            for ( int pixelID = firstPixelOfSample;
+                  pixelID < sampleSetSize || pixelID < firstpixel + npixel;
+                pixelID++ )
             {
                 if (verbosityLevel > 0)
                 {
                  cout << "-------------------------------------" << endl
-                      << "...processing Pixel: " << pixel
+                      << "...processing Pixel: " << pixelID
                       << "/" << firstpixel + npixel << endl;
                 }
 
-                pPixelSet[pixel] = new Pixel(
-                            pixel,
-                            maxPulseOrder,
-                            verbosityLevel,
-                            gPixelOverlayXaxisLeft,
-                            gPixelOverlayXaxisRight,
-                            gBSLMean,
-                            gGainMean,
-                            histoOptions
-                            );
+//-------------------------------------
+// Create individual Pixel
+//-------------------------------------
+                if (ev == firstevent)
+                {
+                    pixel[pixelID] = new Pixel(
+                                pixelID,
+                                maxPulseOrder,
+                                verbosityLevel,
+                                gPixelOverlayXaxisLeft,
+                                gPixelOverlayXaxisRight,
+                                gBSLMean,
+                                gGainMean,
+                                histoOptions
+                                );
+                }
 
 //-------------------------------------
@@ -429,5 +449,5 @@
                 applyDrsCalibration(
                             Ameas,
-                            pixel,
+                            pixelID,
                             12,
                             12,
@@ -487,5 +507,5 @@
 //-----------------------------------------------------------------------------
                 FillHistograms(
-                            pPixelSet[pixel],
+                            pixel[pixelID],
                             pZXings,
                             AmplWindowWidth,
@@ -500,72 +520,5 @@
                 if ( spikeDebug )
                 {
-                    // TODO do this correct. The vectors should be the rigt ones... this is just luck
-                    debugHistos[Ameas_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
-                    debugHistos[Vcorr_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
-                    debugHistos[Vslide_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
-                    debugHistos[Vcfd_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
-                    debugHistos[Vcfd2_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
-
-                    for ( unsigned int sl = 0; sl < gRegionOfInterest; sl++)
-                    {
-                       debugHistos[Ameas_].SetBinContent(sl, Ameas[sl]);
-                       debugHistos[Vcorr_].SetBinContent(sl, Vcorr[sl]);
-                       debugHistos[Vslide_].SetBinContent( sl, Vslide[sl] );
-                       debugHistos[Vcfd_].SetBinContent( sl, Vcfd[sl] );
-                       debugHistos[Vcfd2_].SetBinContent( sl, Vcfd2[sl] );
-                    }
-
-
-                    cFiltered->cd(1);
-                    gPad->SetGrid();
-                    debugHistos[Ameas_].Draw();
-
-                    cFiltered->cd(2);
-                    gPad->SetGrid();
-                    debugHistos[Vcorr_].Draw();
-
-                    cFiltered->cd(3);
-                    gPad->SetGrid();
-                    debugHistos[Vslide_].Draw();
-
-                    TBox *OneBox;
-                    vector<TBox*> MyBoxes;
-                    for (unsigned int i=0; i<pZXings->size(); i++){
-                            OneBox = new TBox(
-                                    pZXings->at(i).maxPos -10 ,
-                                    pZXings->at(i).maxVal -0.5,
-                                    pZXings->at(i).maxPos +10 ,
-                                    pZXings->at(i).maxVal +0.5);
-                            OneBox->SetLineColor(kBlue);
-                            OneBox->SetLineWidth(1);
-                            OneBox->SetFillStyle(0);
-                            OneBox->SetFillColor(kRed);
-                            MyBoxes.push_back(OneBox);
-                            OneBox->Draw();
-                    }
-
-        //            cFiltered->cd(3);
-        //            gPad->SetGrid();
-        //            debugHistos[Vcfd2_].Draw();
-        //            TLine *zeroline = new TLine(0, 0, 1024, 0);
-        //            zeroline->SetLineColor(kBlue);
-        //            zeroline->Draw();
-
-                    cFiltered->Update();
-
-
-                    //Process gui events asynchronously during input
-                    TTimer timer("gSystem->ProcessEvents();", 50, kFALSE);
-                    timer.TurnOn();
-                    TString input = Getline("Type 'q' to exit, <return> to go on: ");
-                    timer.TurnOff();
-                    if (input=="q\n") {
-                            breakout=true;
-                    }
-
-                    //TODO!!!!!!!!!
-                    // do some Garbage collection ...
-                    // all the Objects on the heap should be deleted here.
-
+                    ProduceDebugHistos( pZXings );
                 }// end of if(spikeDebug)
 
@@ -586,5 +539,5 @@
                                 )
                             {
-                                pPixelSet[pixel]->DrawHistograms(
+                                pixel[pixelID]->DrawHistograms(
                                             cgpPixelPulses[order],
                                             pulsesCanvasFrameNrs
@@ -626,10 +579,10 @@
 //-------------------------------------
 
-        for ( int pixel = firstPixelOfSample;
-              pixel < sampleSetSize || pixel < firstpixel + npixel;
-            pixel++ )
+        for ( int pixelID = firstPixelOfSample;
+              pixelID < sampleSetSize || pixelID < firstpixel + npixel;
+            pixelID++ )
         {
             //here is what happends at the end of each loop over all Events
-//            pPixelSet[pixel]->DrawHistograms(
+//            pixel[pixelID]->DrawHistograms(
 //                        cgpPixelPulses,
 //                        pulsesCanvasFrameNrs
@@ -638,6 +591,6 @@
             SaveHistograms(     //save histograms of generell results into output root file
                         OutRootFileName,
-                        CreateSubDirName(pPixelSet[pixel]->mChid),
-                        pPixelSet[pixel]->hList,
+                        CreateSubDirName(pixel[pixelID]->mChid),
+                        pixel[pixelID]->hList,
                         verbosityLevel
                         );
@@ -652,5 +605,5 @@
 
             //Save Histograms of Pixels into Output rootfile
-            pPixelSet[pixel]->SavePixelHistograms( OutRootFileName );
+            pixel[pixelID]->SavePixelHistograms( OutRootFileName );
 
 
@@ -668,5 +621,5 @@
             }
 
-            delete pPixelSet[pixel];
+            delete pixel[pixelID];
 
             if (verbosityLevel > 2)
@@ -759,5 +712,6 @@
 
 
-void BookDebugHistos( int verbosityLevel )
+void
+BookDebugHistos( int verbosityLevel )
 {
     if (verbosityLevel > 2) cout << endl << "...book histograms" << endl;
@@ -788,5 +742,6 @@
 
 
-void FillHistograms(
+void
+FillHistograms(
         Pixel* CurrentPixel,
         vector<Region>* pZXings,
@@ -877,5 +832,6 @@
 //----------------------------------------------------------------------------
 
-void DrawTestHistograms(
+void
+DrawTestHistograms(
         int verbosityLevel
         )
@@ -891,16 +847,17 @@
 //----------------------------------------------------------------------------
 
-void UpdateCanvases(
+void
+UpdateCanvases(
         int verbosityLevel,
         int max_pulse_order
         )
 {
-    if (verbosityLevel > 3) cout << endl << "...updating canvases TODO CANVAS" ;
+    if (verbosityLevel > 3) cout << endl << "...updating canvases" ;
     for (int pulse_order = 0; pulse_order < max_pulse_order; pulse_order++)
     {
-//        cgpPixelPulses[pulse_order]->Modified();
-//        cgpPixelPulses[pulse_order]->Update();
-//        cgpTestHistos->Modified();
-//        cgpTestHistos->Update();
+        cgpPixelPulses[pulse_order]->Modified();
+        cgpPixelPulses[pulse_order]->Update();
+        cgpTestHistos->Modified();
+        cgpTestHistos->Update();
     }
 }
@@ -914,9 +871,10 @@
         )
 {
-    if (verbosityLevel > 2) cout << endl << "...delete pixel Canvas TODO CANVAS" ;
-//    for (int pulse_order = 0; pulse_order < maxPulseOrder; pulse_order ++)
-//    {
-//        delete cgpPixelPulses[pulse_order];
-//    }
+    if (verbosityLevel > 2) cout << endl << "...delete pixel Canvases" ;
+    for (int pulse_order = 0; pulse_order < maxPulseOrder; pulse_order ++)
+    {
+        delete cgpPixelPulses[pulse_order];
+    }
+    delete[] cgpPixelPulses;
 }
 
@@ -924,3 +882,76 @@
 //----------------------------------------------------------------------------
 
-
+void
+ProduceDebugHistos(
+        vector<Region>* pZXings
+        )
+{
+    // TODO do this correct. The vectors should be the rigt ones... this is just luck
+    debugHistos[Ameas_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
+    debugHistos[Vcorr_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
+    debugHistos[Vslide_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
+    debugHistos[Vcfd_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
+    debugHistos[Vcfd2_].GetXaxis()->Set(Ameas.size() , -0.5 , Ameas.size()-0.5);
+
+    for ( unsigned int sl = 0; sl < gRegionOfInterest; sl++)
+    {
+       debugHistos[Ameas_].SetBinContent(sl, Ameas[sl]);
+       debugHistos[Vcorr_].SetBinContent(sl, Vcorr[sl]);
+       debugHistos[Vslide_].SetBinContent( sl, Vslide[sl] );
+       debugHistos[Vcfd_].SetBinContent( sl, Vcfd[sl] );
+       debugHistos[Vcfd2_].SetBinContent( sl, Vcfd2[sl] );
+    }
+
+
+    cFiltered->cd(1);
+    gPad->SetGrid();
+    debugHistos[Ameas_].Draw();
+
+    cFiltered->cd(2);
+    gPad->SetGrid();
+    debugHistos[Vcorr_].Draw();
+
+    cFiltered->cd(3);
+    gPad->SetGrid();
+    debugHistos[Vslide_].Draw();
+
+    TBox *OneBox;
+    vector<TBox*> MyBoxes;
+    for (unsigned int i=0; i<pZXings->size(); i++){
+            OneBox = new TBox(
+                    pZXings->at(i).maxPos -10 ,
+                    pZXings->at(i).maxVal -0.5,
+                    pZXings->at(i).maxPos +10 ,
+                    pZXings->at(i).maxVal +0.5);
+            OneBox->SetLineColor(kBlue);
+            OneBox->SetLineWidth(1);
+            OneBox->SetFillStyle(0);
+            OneBox->SetFillColor(kRed);
+            MyBoxes.push_back(OneBox);
+            OneBox->Draw();
+    }
+
+//            cFiltered->cd(3);
+//            gPad->SetGrid();
+//            debugHistos[Vcfd2_].Draw();
+//            TLine *zeroline = new TLine(0, 0, 1024, 0);
+//            zeroline->SetLineColor(kBlue);
+//            zeroline->Draw();
+
+    cFiltered->Update();
+
+
+    //Process gui events asynchronously during input
+    TTimer timer("gSystem->ProcessEvents();", 50, kFALSE);
+    timer.TurnOn();
+    TString input = Getline("Type 'q' to exit, <return> to go on: ");
+    timer.TurnOff();
+    if (input=="q\n") {
+            breakout=true;
+    }
+
+    //TODO!!!!!!!!!
+    // do some Garbage collection ...
+    // all the Objects on the heap should be deleted here.
+
+}// end of if(spikeDebug)
