Index: fact/tools/rootmacros/PulseTemplates/FCalcPulseTemplate.C
===================================================================
--- fact/tools/rootmacros/PulseTemplates/FCalcPulseTemplate.C	(revision 14749)
+++ fact/tools/rootmacros/PulseTemplates/FCalcPulseTemplate.C	(revision 14753)
@@ -19,4 +19,6 @@
 #include <TMath.h>
 #include <TFile.h>
+#include <TGraph.h>
+#include <TGraphErrors.h>
 #include <TStyle.h>
 #include <TString.h>
@@ -28,4 +30,8 @@
 #include <iostream>
 #include <fstream>
+
+#include <vector>
+#include <utility>
+
 using namespace std;
 
@@ -70,4 +76,5 @@
 TCanvas**   cgpAllPixelPulses   = NULL;
 TCanvas**   cgpDistributions    = NULL;
+TCanvas*    cgpGraphs           = NULL;
 //TCanvas*    cgpTestHistos       = NULL;
 
@@ -94,15 +101,15 @@
     TString     OutPutPath          = "analysis/FPulseTemplate/20120309_017/",
     int         firstpixel          = 0,
-    int         npixel              = 1,
+    int         npixel              = 10,
     int         pixelSetSize        = 200,
-    int         maxPulseOrder       = 1,
-    TString     histoOptions        = "SRM",
+    int         maxPulseOrder       = 4,
+//    TString     histoOptions        = "SRM",
     bool        ProduceGraphic      = true,
     bool        stats               = true,
     bool        saveResults         = false,
 //    bool        fitdata             = false,
-    bool        debugPixel          = true,
+    bool        debugPixel          = false,
 //    int         refresh_rate        = 500,      //refresh rate for canvases
-    int         verbosityLevel      = 4        // different verbosity levels can be implemented here
+    int         verbosityLevel      = 1        // different verbosity levels can be implemented here
         )
 {
@@ -203,4 +210,5 @@
         cgpPixelPulses              = new TCanvas*[maxPulseOrder];
         cgpDistributions            = new TCanvas*[maxPulseOrder];
+        cgpGraphs                   = new TCanvas("graphs", "Dependences to pe" );
 
         //TCanvas*    gpcDevelopment = NULL;
@@ -235,4 +243,6 @@
             cgpPixelPulses[pulse_order]->Divide(4, 2);
         }
+
+        cgpGraphs->Divide(1,3);
     }
 
@@ -241,8 +251,80 @@
 //-----------------------------------------------------------------------------
 
-
 //-----------------------------------------------------------------------------
-// prepare datafile
+// Distrribution Histograms
 //-----------------------------------------------------------------------------
+    TH1D hBsl[maxPulseOrder];
+    TH1D hAsymptote[maxPulseOrder];
+    TH1D hT0[maxPulseOrder];
+    TH1D hTau1[maxPulseOrder];
+    TH1D hTau2[maxPulseOrder];
+    TH1D hIntegral[maxPulseOrder];
+    TH1D hAmplitude[maxPulseOrder];
+
+    //Lopp over pulse order configure distribution histograms
+    for (int i = 0; i<maxPulseOrder; i++)
+    {
+        //BSL
+        TString title;
+        title = "hBSL";
+        title.Append(i);
+        hBsl[i].SetNameTitle(title, title);
+        hBsl[i].SetBins(400, -2-0.005, 2-0.005);
+
+        //Asymptote
+        title = "hAsymptote";
+        title.Append(i);
+        hAsymptote[i].SetNameTitle(title, title);
+        hAsymptote[i].SetBins(60, -10.5, 49.5);
+
+        //T0
+        title = "hT0";
+        title.Append(i);
+        hT0[i].SetNameTitle(title, title);
+        hT0[i].SetBins(80, 39.5, 119.5);
+
+        //Tau1
+        title = "hTau1";
+        title.Append(i);
+        hTau1[i].SetNameTitle(title, title);
+        hTau1[i].SetBins(10, -10.5, 9.5);
+
+        //Tau2
+        title = "hTau2";
+        title.Append(i);
+        hTau2[i].SetNameTitle(title, title);
+        hTau2[i].SetBins(40, 19.5, 59.5);
+
+        //Integral
+        title = "hIntegral";
+        title.Append(i);
+        hIntegral[i].SetNameTitle(title, title);
+        hIntegral[i].SetBins(400, (i+1)*99.5, (i+1)*499.5);
+
+        //Amplitude
+        title = "hAmplitude";
+        title.Append(i);
+        hAmplitude[i].SetNameTitle(title, title);
+        hAmplitude[i].SetBins(400, (i+1)*4.95, (i+1)*24.95);
+    }
+
+//-----------------------------------------------------------------------------
+// Graphs
+//-----------------------------------------------------------------------------
+    TGraphErrors grPheVsAmpl;
+    grPheVsAmpl.SetNameTitle("grPheVsAmpl", "Distribution of Pulse Amplitude vs. Photon equivalent");
+    grPheVsAmpl.SetMarkerStyle(21);
+
+    TGraphErrors grPheVsIntegral;
+    grPheVsIntegral.SetNameTitle("grPheVsIntegral", "Distribution of Pulse Integral vs. Photon equivalent");
+    grPheVsIntegral.SetMarkerStyle(21);
+
+    TGraphErrors grPheVsTau2;
+    grPheVsTau2.SetNameTitle("grPheVsTau2", "Distribution of Tau2 vs. Photon equivalent");
+    grPheVsTau2.SetMarkerStyle(21);
+//-----------------------------------------------------------------------------
+// Variables
+//-----------------------------------------------------------------------------
+
 
 //----------------------------------------------------------------------------
@@ -269,7 +351,7 @@
 //----------------------------------------------------------------------------
 
-    Csv PixelCsv(OutPutPath, InRootFileName.Remove(12, 5), "SPn", verbosityLevel);
+    Csv PixelCsv(OutPutPath, InRootFileName.Remove(12, 5), "points", verbosityLevel);
     PixelCsv.WritePointSetHeader();
-    Csv PixelModelCsv(OutPutPath, InRootFileName.Remove(12, 5), "SPModeln", verbosityLevel);
+    Csv PixelModelCsv(OutPutPath, InRootFileName.Remove(12, 5), "fitResults", verbosityLevel);
     PixelModelCsv.WritePulseAttributesHeader();
 //    Csv AllPixelCsv(OutPutPath, InRootFileName.Remove(12, 5), -1, verbosityLevel);
@@ -340,8 +422,8 @@
                        stats,
                        "L",
-                       70,   ///TODO: get it from the root file
-                       230,  ///TODO: get it from the root file
-                       -1,   ///TODO: get it from the root file
-                       9,    ///TODO: get it from the root file
+                       70,   //pixelOverlayXaxisLeft /TODO: get it from the root file
+                       230,  //pixelOverlayXaxisRight /TODO: get it from the root file
+                       -1,   //bslMean   /TODO: get it from the root file
+                       9,    //gain mean /TODO: get it from the root file
                        inputRootFile,
                        outputRootFile
@@ -426,4 +508,15 @@
                             );
 
+
+//                ShiftStartOfHistoInXTo(
+//                            pixel[pixelID]->hEdgeOverlay[pulse_order],
+//                            0
+//                            );
+
+//                ShiftStartOfHistoInXTo(
+//                            pixel[pixelID]->hMaxOverlay[pulse_order],
+//                            0
+//                            );
+
                 PixelCsv.WritePointSet(
                             pixel[pixelID],
@@ -459,33 +552,33 @@
                 if (ProduceGraphic && debugPixel)
                 {
-                    pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetXaxis()->SetLimits(
-                                0,
-                                300
-                                );
-                    pixel[pixelID]->hPixelEdgeMean[pulse_order]->GetXaxis()->SetLimits(
-                                0,
-                                300
-                                );
-                    pixel[pixelID]->hPixelEdgeMedian[pulse_order]->GetXaxis()->SetLimits(
-                                0,
-                                300
-                                );
-                    pixel[pixelID]->hPixelMax[pulse_order]->GetXaxis()->SetLimits(
-                                0,
-                                300
-                                );
-                    pixel[pixelID]->hPixelMean[pulse_order]->GetXaxis()->SetLimits(
-                                0,
-                                300
-                                );
-                    pixel[pixelID]->hPixelMedian[pulse_order]->GetXaxis()->SetLimits(
-                                0,
-                                300
-                                );
-
-                    pixel[pixelID]->hEdgeProfile[pulse_order]->GetXaxis()->SetLimits(
-                                0,
-                                300
-                                );
+//                    pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetXaxis()->SetLimits(
+//                                0,
+//                                300
+//                                );
+//                    pixel[pixelID]->hPixelEdgeMean[pulse_order]->GetXaxis()->SetLimits(
+//                                0,
+//                                300
+//                                );
+//                    pixel[pixelID]->hPixelEdgeMedian[pulse_order]->GetXaxis()->SetLimits(
+//                                0,
+//                                300
+//                                );
+//                    pixel[pixelID]->hPixelMax[pulse_order]->GetXaxis()->SetLimits(
+//                                0,
+//                                300
+//                                );
+//                    pixel[pixelID]->hPixelMean[pulse_order]->GetXaxis()->SetLimits(
+//                                0,
+//                                300
+//                                );
+//                    pixel[pixelID]->hPixelMedian[pulse_order]->GetXaxis()->SetLimits(
+//                                0,
+//                                300
+//                                );
+
+//                    pixel[pixelID]->hEdgeProfile[pulse_order]->GetXaxis()->SetLimits(
+//                                0,
+//                                300
+//                                );
 
 
@@ -507,4 +600,7 @@
                                 PixelCanvasFrameNrs
                                 );
+
+//                    cgpPixelPulses[pulse_order]->cd(6);
+
 
                     //-------------------------------------
@@ -513,29 +609,4 @@
                     //-------------------------------------
                     //-------------------------------------
-
-
-//                    cgpPixelPulses[pulse_order]->cd(6);
-
-                    pulses.cd(1);
-                    Pulse pulse("EdgeMaxPulse", pixel[pixelID]->hPixelEdgeMax[pulse_order], 1);
-//                    pixel[pixelID]->hPixelEdgeMax[pulse_order]->DrawCopy();
-                    pulses.cd(2);
-                    Pulse pulse1("EdgeMedianPulse", pixel[pixelID]->hPixelEdgeMedian[pulse_order], 1);
-                    pulses.cd(3);
-                    Pulse pulse2("EdgeMeanPulse", pixel[pixelID]->hPixelEdgeMean[pulse_order], 1);
-
-                    PixelModelCsv.WritePulseAttributes(
-                                pixel[pixelID],
-                                &pulse,
-                                "2",
-                                pulse_order
-                                );
-
-                    PixelModelCsv.WritePulseAttributes(
-                                pixel[pixelID],
-                                &pulse1,
-                                "2",
-                                pulse_order
-                                );
 
                     cgpPixelPulses[pulse_order]->cd(8);
@@ -665,7 +736,104 @@
                     //-------------------------------------
                     //-------------------------------------
-
-
                 }
+
+                // =======================================================
+                //  Fit of pulse function to template
+
+                Pulse* pulseFits[6];
+
+                pulses.cd(1);
+                Pulse maxMaxPulse("maxMaxPulse", pixel[pixelID]->hPixelMax[pulse_order], "Q", 1);
+                pulseFits[0] = &maxMaxPulse;
+
+                pulses.cd(2);
+                Pulse maxMedianPulse("maxMedianPulse", pixel[pixelID]->hPixelMedian[pulse_order], "Q",1);
+                pulseFits[1] = &maxMedianPulse;
+
+                pulses.cd(3);
+                Pulse maxMeanPulse("maxMeanPulse", pixel[pixelID]->hPixelMean[pulse_order], "Q",1);
+                pulseFits[2] = &maxMeanPulse;
+
+                Pulse edgeMaxPulse("edgeMaxPulse", pixel[pixelID]->hPixelEdgeMax[pulse_order], "Q",1);
+                pulseFits[3] = &edgeMaxPulse;
+
+                Pulse edgeMedianPulse("edgeMedianPulse", pixel[pixelID]->hPixelEdgeMedian[pulse_order], "Q",1);
+                pulseFits[4] = &edgeMedianPulse;
+
+                Pulse edgeMeanPulse("edgeMeanPulse", pixel[pixelID]->hPixelEdgeMean[pulse_order], "Q",1);
+                pulseFits[5] = &edgeMeanPulse;
+
+
+                //--------------------------------------------------------
+                //Write parameters from pulse fit to CSV
+
+                for (int i = 0; i < 6; i++)
+                {
+                    PixelModelCsv.WritePulseAttributes(
+                                pixel[pixelID],
+                                pulseFits[i],
+                                "Edge_pix",
+                                pulse_order
+                                );
+                }
+                //-------------------------------------
+                // Fill Distribution Histogramms
+                //-------------------------------------
+
+                hBsl[pulse_order].Fill(         edgeMedianPulse.GetBsl()    );
+                hAsymptote[pulse_order].Fill(   edgeMedianPulse.GetHeight() );
+                hT0[pulse_order].Fill(          edgeMedianPulse.GetT0()     );
+                hTau1[pulse_order].Fill(        edgeMedianPulse.GetTau1()   );
+                hTau2[pulse_order].Fill(        edgeMedianPulse.GetTau2()   );
+                hIntegral[pulse_order].Fill(    edgeMedianPulse.GetIntegral());
+                hAmplitude[pulse_order].Fill(   edgeMedianPulse.GetAmplitude());
+
+                //-------------------------------------
+                // Fill Distribution Containers
+                //-------------------------------------
+
+                grPheVsAmpl.Set(grPheVsAmpl.GetN()+1);
+                grPheVsIntegral.Set(grPheVsIntegral.GetN()+1);
+
+
+                grPheVsAmpl.SetPoint(
+                            grPheVsAmpl.GetN()-1,
+                            edgeMedianPulse.GetPhE(),
+                            edgeMedianPulse.GetAmplitude()
+                            );
+
+                grPheVsAmpl.SetPointError(
+                            grPheVsAmpl.GetN()-1,
+                            edgeMedianPulse.GetPhEErr(),
+                            edgeMedianPulse.GetAmplitudeErr()
+                            );
+
+                grPheVsIntegral.SetPoint(
+                            grPheVsIntegral.GetN()-1,
+                            edgeMedianPulse.GetPhE(),
+                            edgeMedianPulse.GetIntegral()
+                            );
+
+                grPheVsAmpl.SetPointError(
+                            grPheVsIntegral.GetN()-1,
+                            edgeMedianPulse.GetPhEErr(),
+                            edgeMedianPulse.GetIntegralErr()
+                            );
+
+                grPheVsTau2.SetPoint(
+                            grPheVsIntegral.GetN()-1,
+                            edgeMedianPulse.GetPhE(),
+                            edgeMedianPulse.GetTau2()
+                            );
+
+                grPheVsTau2.SetPointError(
+                            grPheVsIntegral.GetN()-1,
+                            edgeMedianPulse.GetPhE(),
+                            edgeMedianPulse.GetTau2Err()
+                            );
+
+//                cgpDistributions[pulse_order].cd(1);
+
+
 //                FitMaxPropabilityPuls(
 //                            pixel[pixelID]->hPixelEdgeMean[pulse_order],
@@ -854,5 +1022,110 @@
                         );
         }
+
+        ShiftStartOfHistoInXTo(
+                    wholeCamera->hEdgeOverlay[0],
+                    0
+                    );
+
+        ShiftStartOfHistoInXTo(
+                    wholeCamera->hMaxOverlay[0],
+                    0
+                    );
+
+        // =======================================================
+        //  Fit of pulse function to template
+
+        Pulse* allPulseFits[6];
+
+        pulses.cd(1);
+        Pulse maxCamMaxPulse("maxMaxPulse", wholeCamera->hPixelMax[pulse_order], "Q", 1);
+        allPulseFits[0] = &maxCamMaxPulse;
+
+        pulses.cd(2);
+        Pulse maxCamMedianPulse("maxMedianPulse", wholeCamera->hPixelMedian[pulse_order], "Q", 1);
+        allPulseFits[1] = &maxCamMedianPulse;
+
+        pulses.cd(3);
+        Pulse maxCamMeanPulse("maxMeanPulse", wholeCamera->hPixelMean[pulse_order],"Q", 1);
+        allPulseFits[2] = &maxCamMeanPulse;
+
+        Pulse edgeCamMaxPulse("edgeMaxPulse", wholeCamera->hPixelEdgeMax[pulse_order], "Q", 1);
+        allPulseFits[3] = &edgeCamMaxPulse;
+
+        Pulse edgeCamMedianPulse("edgeMedianPulse", wholeCamera->hPixelEdgeMedian[pulse_order],"Q", 1);
+        allPulseFits[4] = &edgeCamMedianPulse;
+
+        Pulse edgeCamMeanPulse("edgeMeanPulse", wholeCamera->hPixelEdgeMean[pulse_order], "Q", 1);
+        allPulseFits[5] = &edgeCamMeanPulse;
+
+
+        //--------------------------------------------------------
+        //Write parameters from pulse fit to CSV
+
+        for (int i = 0; i < 6; i++)
+        {
+            PixelModelCsv.WritePulseAttributes(
+                        wholeCamera,
+                        allPulseFits[i],
+                        "Edge",
+                        pulse_order
+                        );
+        }
     } //EOF: Draw All Pixel Histograms
+
+
+    //-------------------------------------
+    // Draw Parameter Distributions
+    //-------------------------------------
+
+    if (ProduceGraphic)
+    {
+        cgpGraphs->cd(1);
+
+        if (verbosityLevel > 0) cout << "...drawing grPheVsAmpl" << endl;
+        grPheVsAmpl.Draw("AP");
+
+        cgpGraphs->cd(2);
+        if (verbosityLevel > 0) cout << "...drawing grPheVsIntegral" << endl;
+        grPheVsIntegral.Draw("AP");
+
+        cgpGraphs->cd(3);
+        if (verbosityLevel > 0) cout << "...drawing grPheVsTau2" << endl;
+        grPheVsTau2.Draw("AP");
+
+        cgpGraphs->Update();
+
+        for (int i = 0; i < maxPulseOrder; i++){
+            cgpDistributions[i]->Divide(4,2);
+
+            cgpDistributions[i]->cd(1);
+            hBsl[i].Draw();
+            cgpDistributions[i]->cd(2);
+            hAsymptote[i].Draw();
+            cgpDistributions[i]->cd(3);
+            hT0[i].Draw();
+            cgpDistributions[i]->cd(4);
+            hTau1[i].Draw();
+            cgpDistributions[i]->cd(5);
+            hTau2[i].Draw();
+            cgpDistributions[i]->cd(6);
+            hIntegral[i].Draw();
+            cgpDistributions[i]->cd(7);
+            hAmplitude[i].Draw();
+
+
+
+        }
+
+
+
+
+
+    }
+
+
+
+
+
 
 
@@ -1061,5 +1334,5 @@
                     rcfile.mPixelSetSize,
                     rcfile.mMaxOrder,
-                    rcfile.mHistoOptions,
+//                    rcfile.mHistoOptions,
                     rcfile.mProduceGraphic,
                     rcfile.mPrintStats,
