Index: fact/tools/rootmacros/PulseTemplates/FCalcPulseTemplate.C
===================================================================
--- fact/tools/rootmacros/PulseTemplates/FCalcPulseTemplate.C	(revision 14064)
+++ fact/tools/rootmacros/PulseTemplates/FCalcPulseTemplate.C	(revision 14749)
@@ -37,11 +37,13 @@
 
 //----------------------------------------------------------------------------
-// rootmacros
+// Classes
 //----------------------------------------------------------------------------
 #include "rootfilehandler.h"
 #include "pixel.h"
 #include "pixelsum.h"
+#include "pulse.h"
 #include "templateextractors.h"
 #include "configfile.h"
+#include "csv.h"
 
 //----------------------------------------------------------------------------
@@ -87,10 +89,10 @@
 //----------------------------------------------------------------------------
 int FCalcPulseTemplate(
-    TString     InRootFileName        = "20120309_018.root",
-    TString     InputPath           = "analysis/FPulseTemplate/20120309_018/Overlay/",
+    TString     InRootFileName        = "20120309_017.root",
+    TString     InputPath           = "analysis/analysis/FPulseTemplate/20120309_017/Overlay/",
     TString     OutputRootFileName     = "test.root",
-    TString     OutPutPath          = "analysis/FPulseTemplate/20120309_018/",
+    TString     OutPutPath          = "analysis/FPulseTemplate/20120309_017/",
     int         firstpixel          = 0,
-    int         npixel              = -1,
+    int         npixel              = 1,
     int         pixelSetSize        = 200,
     int         maxPulseOrder       = 1,
@@ -102,5 +104,5 @@
     bool        debugPixel          = true,
 //    int         refresh_rate        = 500,      //refresh rate for canvases
-    int         verbosityLevel      = 2        // different verbosity levels can be implemented here
+    int         verbosityLevel      = 4        // different verbosity levels can be implemented here
         )
 {
@@ -230,5 +232,5 @@
 
             cgpPixelPulses[pulse_order]
-                     = new TCanvas(cName,cTitle, 0,pulse_order*20,1400,1400);
+                     = new TCanvas(cName,cTitle, 0,pulse_order*20,800,800);
             cgpPixelPulses[pulse_order]->Divide(4, 2);
         }
@@ -263,4 +265,17 @@
     bool        first_pass  = true;
 
+//----------------------------------------------------------------------------
+// Initialize Outputfiles
+//----------------------------------------------------------------------------
+
+    Csv PixelCsv(OutPutPath, InRootFileName.Remove(12, 5), "SPn", verbosityLevel);
+    PixelCsv.WritePointSetHeader();
+    Csv PixelModelCsv(OutPutPath, InRootFileName.Remove(12, 5), "SPModeln", verbosityLevel);
+    PixelModelCsv.WritePulseAttributesHeader();
+//    Csv AllPixelCsv(OutPutPath, InRootFileName.Remove(12, 5), -1, verbosityLevel);
+//    Csv AllPixelModelCsv(OutPutPath, InRootFileName.Remove(12, 5), "AllPixelModel", verbosityLevel);
+
+    TCanvas pulses("Templates","Templates", 0,0,1200,600);
+    pulses.Divide(3,1);
 //-------------------------------------
 // Loop over Pixel Sets
@@ -411,4 +426,16 @@
                             );
 
+                PixelCsv.WritePointSet(
+                            pixel[pixelID],
+                            "Maximum",
+                            pulse_order
+                            );
+
+                PixelCsv.WritePointSet(
+                            pixel[pixelID],
+                            "Edge",
+                            pulse_order
+                            );
+
                 if ( saveResults )
                 {
@@ -436,7 +463,36 @@
                                 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]->ShiftHistoInY(
 //                                pixel[pixelID]->hPixelEdgeMax[pulse_order],
-//                                2
+//                                0.25
 //                                );
 
@@ -458,17 +514,50 @@
                     //-------------------------------------
 
-                    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;
+
+//                    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);
+
+//                    pixel[pixelID]->hEdgeProfile[pulse_order]->SetLineColor(kBlack);
+//                    Pulse pulseMean("PulseMean", pixel[pixelID]->hEdgeProfile[pulse_order], 0);
+
+
+
+
+//                    double fit_parameters5[3];
+//                    fit_parameters5[0] = fit_parameters[0];
+//                    fit_parameters5[0] = fit_parameters[0];
+//                    FitPulse(
+//                                "hugo3",
+//                                pixel[pixelID]->hPixelEdgeMax[pulse_order],
+//                                pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetMaximumBin()-12,
+//                                pixel[pixelID]->hPixelEdgeMax[pulse_order]->GetMaximumBin(),
+//                                fit_parameters5
+//                            );
+//                    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);
@@ -489,84 +578,84 @@
 //                    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();
+//                    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)) );
+//                    }
+
+
+//                    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();
 
 
@@ -608,13 +697,13 @@
 
                 //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();
+//                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)
