Index: fact/tools/rootmacros/PulseTemplates/FCalcPulseTemplate.C
===================================================================
--- fact/tools/rootmacros/PulseTemplates/FCalcPulseTemplate.C	(revision 13851)
+++ fact/tools/rootmacros/PulseTemplates/FCalcPulseTemplate.C	(revision 14064)
@@ -21,4 +21,5 @@
 #include <TStyle.h>
 #include <TString.h>
+#include <TF1.h>
 
 #include <stdio.h>
@@ -79,4 +80,5 @@
         bool testmode
         );
+
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
@@ -92,11 +94,11 @@
     int         npixel              = -1,
     int         pixelSetSize        = 200,
-    int         maxPulseOrder       = 3,
+    int         maxPulseOrder       = 1,
     TString     histoOptions        = "SRM",
     bool        ProduceGraphic      = true,
     bool        stats               = true,
-    bool        saveResults         = true,
+    bool        saveResults         = false,
 //    bool        fitdata             = false,
-    bool        debugPixel          = false,
+    bool        debugPixel          = true,
 //    int         refresh_rate        = 500,      //refresh rate for canvases
     int         verbosityLevel      = 2        // different verbosity levels can be implemented here
@@ -173,18 +175,23 @@
 
     //Canvas Pad numbering
-    int PixelCanvasFrameNrs[4]      = {
+    int PixelCanvasFrameNrs[8]      =
+    {
         1,  // Top left
-        2,  // Top right
-        3,  // bottom left
-        4   // bootom right
+        2,  // Top mid left
+        3,  // Top mid right
+        4,  // Top right
+        5,  // bootom left
+        6,  // bootom mid left
+        7,  // bottom mid right
+        8   // bootom right
     };
 
-    //Canvas Pad numbering
-    int AllpixelCanvasFrameNrs[4]   = {
-        1,  // Top left
-        2,  // Top right
-        3,  // bottom left
-        4   // bootom right
-    };
+//    //Canvas Pad numbering
+//    int DistributionCanvasFrameNrs[4]   = {
+//        1,  // Top left
+//        2,  // Top right
+//        3,  // bottom left
+//        4   // bootom right
+//    };
 
     if (ProduceGraphic)
@@ -214,5 +221,5 @@
             cgpDistributions[pulse_order]
                     = new TCanvas(cName,cTitle, 720,pulse_order*20,720,720);
-            cgpDistributions[pulse_order]->Divide(2, 2);
+//            cgpDistributions[pulse_order]->Divide(2, 2);
 
             cName   ="cgpPixelPulses";
@@ -223,6 +230,6 @@
 
             cgpPixelPulses[pulse_order]
-                     = new TCanvas(cName,cTitle, 0,pulse_order*20,720,720);
-            cgpPixelPulses[pulse_order]->Divide(2, 2);
+                     = new TCanvas(cName,cTitle, 0,pulse_order*20,1400,1400);
+            cgpPixelPulses[pulse_order]->Divide(4, 2);
         }
     }
@@ -342,5 +349,5 @@
                             verbosityLevel,
                             stats,
-                            "",
+                            "C",
                             pixel[pixelID]->mPixelOverlayXaxisLeft,
                             pixel[pixelID]->mPixelOverlayXaxisRight ,
@@ -425,4 +432,14 @@
                 if (ProduceGraphic && debugPixel)
                 {
+                    pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetXaxis()->SetLimits(
+                                0,
+                                300
+                                );
+//                    pixel[pixelID]->ShiftHistoInY(
+//                                pixel[pixelID]->hPixelEdgeMax[pulse_order],
+//                                2
+//                                );
+
+
                     pixel[pixelID]->DrawTemplateHistograms(
                                 cgpPixelPulses,
@@ -434,5 +451,136 @@
                                 PixelCanvasFrameNrs
                                 );
+
+                    //-------------------------------------
+                    //-------------------------------------
+                    // Test Area
+                    //-------------------------------------
+                    //-------------------------------------
+
+                    cgpPixelPulses[pulse_order]->cd(6);
+
+                    double fit_parameters[3];
+                    FitFallingEdge(
+                                "hugo1",
+                                pixel[pixelID]->hPixelEdgeMax[pulse_order],
+                                pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetMaximumBin(),
+                                pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetXaxis()->GetLast(),
+                                fit_parameters
+                            );
+                    cout << "Parameters\t" << fit_parameters[0] << "\n"
+                            << fit_parameters[1] << "\n"
+                               << fit_parameters[2] << "\n" << endl;
+
+//                    TF1 *func = new TF1("func", template_function, 0, 300, 10);
+
+//                    func->SetParameters(
+//                                -0.5, // bsl
+//                                pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetXaxis()->GetFirst() + 50,
+//                                pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetMaximumBin()
+//                                ); // range
+
+
+
+//                    func->SetParNames("baseline",
+//                                      "begin of pol3", "begin of exp",
+//                                      "exp-factor", "exp-tau", "ext-t0",
+//                                      "pol3_0", "pol3_1", "pol3_2", "pol3_3"
+//                                      );
+//                    pixel[pixelID]->hPixelEdgeMax[pulse_order]->Fit(func);
+
+                    TH1F* hTest = new TH1F(
+                                "hTest",
+                                "Test",
+                                pixel[pixelID]->mPixelOverlayXaxisLeft
+                                + pixel[pixelID]->mPixelOverlayXaxisRight ,
+                                (-1*pixel[pixelID]->mPixelOverlayXaxisLeft)-0.5,
+                                pixel[pixelID]->mPixelOverlayXaxisRight-0.5
+                                );
+
+                    hTest->GetXaxis()->SetLimits(
+                                0,
+                                300
+                                );
+
+                    for (int bin = 0;
+                         bin < pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetXaxis()->GetLast();
+                         bin++
+                         )
+                    {
+                        hTest->SetBinContent( bin, (-1)*(fit_parameters[0]+TMath::Exp(fit_parameters[1]+fit_parameters[2]*(bin))) );
+                    }
+
+                    hTest->Add(pixel[pixelID]->hPixelEdgeMax[pulse_order], 1);
+
+                    cgpPixelPulses[pulse_order]->cd(7);
+                    hTest->Draw();
+
+                    for (int bin = 0;
+                         bin < pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetXaxis()->GetLast();
+                         bin++
+                         )
+                    {
+                        hTest->SetBinContent( bin, (-1)*(hTest->GetBinContent(bin)) );
+                    }
+
+//                    pixel[pixelID]->hPixelEdgeMax[pulse_order]->Fit(func);
+
+                    double fit_parameters2[3];
+                    FitFallingEdge(
+                                "hugo2",
+                                hTest,
+                                hTest->GetXaxis()->GetFirst()+68,
+                                hTest->GetXaxis()->GetFirst()+80,
+                                fit_parameters2
+                            );
+
+                     cgpPixelPulses[pulse_order]->cd(8);
+
+                     TH1F* hTest2 = new TH1F(
+                                 "hTest2",
+                                 "Test",
+                                 pixel[pixelID]->mPixelOverlayXaxisLeft
+                                 + pixel[pixelID]->mPixelOverlayXaxisRight ,
+                                 (-1*pixel[pixelID]->mPixelOverlayXaxisLeft)-0.5,
+                                 pixel[pixelID]->mPixelOverlayXaxisRight-0.5
+                                 );
+
+                     hTest2->GetXaxis()->SetLimits(
+                                 0,
+                                 300
+                                 );
+
+                    for (int bin = 0;
+                         bin < pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetXaxis()->GetLast();
+                         bin++
+                         )
+                    {
+                        hTest2->SetBinContent( bin, (-1)*(hTest->GetBinContent(bin)) );
+                    }
+
+                    double fit_parameters3[3];
+                    FitRisingEdge(
+                                "hugo3",
+                                hTest2,
+                                hTest2->GetXaxis()->GetFirst()+68,
+                                hTest2->GetXaxis()->GetFirst()+80,
+                                fit_parameters3
+                            );
+
+                    hTest2->Draw();
+
+
+                    //-------------------------------------
+                    //-------------------------------------
+                    // EOF Test Area
+                    //-------------------------------------
+                    //-------------------------------------
+
+
                 }
+//                FitMaxPropabilityPuls(
+//                            pixel[pixelID]->hPixelEdgeMean[pulse_order],
+//                            verbosityLevel
+//                            );
                 //-------------------------------------
                 // Fill Histogramms of Camera
@@ -455,4 +603,18 @@
                             );
 
+                //-------------------------------------
+                // Comparisons
+                //-------------------------------------
+
+                //chi2 test
+                float chi2 =
+                pixel[pixelID]->hPixelEdgeMean[pulse_order]->Chi2Test(
+                             pixel[pixelID]->hPixelMean[pulse_order],
+                            "UUPCHI2"
+                            );
+                cout << "p-Value :" << chi2 << endl;
+                wholeCamera->hChi2EdgetoMax[pulse_order]->Fill(chi2);
+                cgpDistributions[pulse_order]->cd();
+                wholeCamera->hChi2EdgetoMax[pulse_order]->Draw();
 
                 if (verbosityLevel > 2)
@@ -486,7 +648,8 @@
                 if (input=="q\n")
                 {
-                       break;
+                    return(0);
                 }
             }
+
             if ( saveResults )
             {
@@ -594,10 +757,10 @@
             wholeCamera->DrawTemplateHistograms(
                         cgpPixelPulses,
-                        AllpixelCanvasFrameNrs
+                        PixelCanvasFrameNrs
                         );
 
             wholeCamera->DrawEdgeTemplateHistograms(
                         cgpPixelPulses,
-                        AllpixelCanvasFrameNrs
+                        PixelCanvasFrameNrs
                         );
         }
@@ -630,8 +793,5 @@
         TString input = Getline("press <return> to exit: ");
         timer.TurnOff();
-        if (input=="q\n")
-        {
-               break;
-        }
+        cout << input ;
     }
 
@@ -827,2 +987,4 @@
     return 0;
 }
+
+
