Index: fact/tools/rootmacros/FPulseTemplate.C
===================================================================
--- fact/tools/rootmacros/FPulseTemplate.C	(revision 13378)
+++ fact/tools/rootmacros/FPulseTemplate.C	(revision 13422)
@@ -35,4 +35,7 @@
 #include <stdint.h>
 #include <cstdio>
+#include <iostream>
+#include <fstream>
+using namespace std;
 
 #define NPIX  1440
@@ -94,6 +97,6 @@
 
 typedef struct{
-  float maxAmpl;
-  float countOfMax;
+  double maxAmpl;
+  int countOfMax;
   } OverlayMaximum;
 
@@ -146,16 +149,23 @@
 TH1D*       hProjPeak = NULL;
 TH1F*       hTesthisto = NULL;
+TH2F*       hTesthisto2 = NULL;
 
 //Pixelwise Histograms
 TH2F*       hPixelOverlay[MAX_PULS_ORDER]= {NULL};//histogrammm for overlay of detected Peaks
+TH2F*       hPixelEdgeOverlay[MAX_PULS_ORDER] = {NULL};
 TProfile*   hPixelProfile[MAX_PULS_ORDER] = {NULL};//histogrammm for Profile of detected Peaks
+TProfile*   hPixelProfile2[MAX_PULS_ORDER] = {NULL};//histogrammm for Profile of detected Peaks
 TH1F*       hPixelMax[MAX_PULS_ORDER] = {NULL};
-TH2F*       hPixelEdgeOverlay[MAX_PULS_ORDER] = {NULL};
+TH1F*       hPixelMedian[MAX_PULS_ORDER] = {NULL};
+TH1F*       hPixelMean[MAX_PULS_ORDER] = {NULL};
 
 //All Pixel Histograms
 TH2F*       hAllPixelOverlay[MAX_PULS_ORDER] = {NULL};
 TProfile*   hAllPixelProfile[MAX_PULS_ORDER] = {NULL};
+TProfile*   hAllPixelProfile2[MAX_PULS_ORDER] = {NULL};
 TH1F*       hAllPixelMax[MAX_PULS_ORDER]     = {NULL};
-TH1F*       hAllPixelMaxGaus[MAX_PULS_ORDER] = {NULL};
+TH1F*       hAllPixelMedian[MAX_PULS_ORDER] = {NULL};
+TH1F*       hAllPixelMean[MAX_PULS_ORDER] = {NULL};
+TH2F*       hAllPixelEdgeOverlay[MAX_PULS_ORDER] = {NULL};
 
 //Histogram Parameters
@@ -183,10 +193,14 @@
 void DeletePixelHistos(int );
 void SaveHistograms( const char*, const char*, TObjArray*, int );
-void FillHistograms(vector<Region>*, int, int);
+void FillHistograms(vector<Region>*, int, int, int);
 void PlotPulseShapeOfMaxPropability(unsigned int, int, int);
+
 void DrawPulseHistograms(int, int);
 void FitMaxPropabilityPuls( TH1F* , int );
 void DrawMaximumHistograms( int, int );
 void DrawTestHistograms( int);
+Double_t MedianOfH1( TH1 *h1 );
+void PlotMedianEachSliceOfPulse(unsigned int, int, int);
+
 
 void UpdateCanvases( int, int);
@@ -198,4 +212,6 @@
 TString CreateSubDirName(int); //creates a String containing the path to the subdirectory in root file
 TString CreateSubDirName(const char* );
+void WritePixelTemplateToCsv( TString, const char*, const char*, int, int);
+void WriteAllPixelTemplateToCsv( TString, const char*, const char*, int);
 //void SetHistogrammSettings( const char*, int, int , int);
 //void CreatePulseCanvas( TCanvas* , unsigned int, const char* , const char*, const char*, int);
@@ -212,17 +228,18 @@
   const char*   drsfilename         = "../data/2011/11/09/20111109_003.drs.fits.gz",
   const char*   OutRootFileName     = "../analysis/FPulseTemplate/20111109_006/20111109_006.pulses.root",
-  bool          ProduceGraphic      = true,
-  int           refresh_rate        = 500,   //refresh rate for canvases
+  const char*   OutPutPath          = "../analysis/FPulseTemplate/20111109_006/Templates/",
+  bool          ProduceGraphic      = false,
+  int           refresh_rate        = 500,      //refresh rate for canvases
   bool          spikeDebug          = false,
-  bool          debugPixel          = true,
+  bool          debugPixel          = false,
   bool          fitdata             = false,
-  int           verbosityLevel      = 1, // different verbosity levels can be implemented here
-  int           firstevent          = 272,
-  int           nevents             = 500,
-  int           firstpixel          = 1100,
-  int           npixel              = 1,
-  int           AmplWindowWidth     = 14,  //Width of Window for selection of pulses to histograms
+  int           verbosityLevel      = 0,        // different verbosity levels can be implemented here
+  int           firstevent          = 0,
+  int           nevents             = -1,
+  int           firstpixel          = 0,
+  int           npixel              = -1,
+  int           AmplWindowWidth     = 14,       //Width of Window for selection of pulses to histograms
   float         GainMean           = 8,
-  float         BSLMean            = -1, //4 Histogramms will be drawn, decide how far pulsehights differ from eachother
+  float         BSLMean            = -1,        //4 Histogramms will be drawn, decide how far pulsehights differ from eachother
   int           avg1                = 8,
   int           avg2                = 8,
@@ -360,5 +377,5 @@
     for ( int pixel = firstpixel; pixel < firstpixel + npixel; pixel++ )
     {
-        if (verbosityLevel > 0)
+        if (verbosityLevel >= 0)
         {
             cout << "------------------------------------------------" << endl
@@ -440,5 +457,5 @@
             removeRegionWithMaxOnEdge( *pZXings, 2);
             removeRegionOnFallingEdge( *pZXings, 100);
-            findTimeOfHalfMaxLeft(*pZXings, Vslide, gBSLMean, 5, 10, verbosityLevel );
+            findTimeOfHalfMaxLeft(*pZXings, Vcorr, gBSLMean, 5, 10, verbosityLevel );
             if (verbosityLevel > 2) cout << "...done" << endl;
 
@@ -446,5 +463,5 @@
 // Fill Overlay Histos
 //-----------------------------------------------------------------------------
-        FillHistograms(pZXings, AmplWindowWidth, verbosityLevel );
+        FillHistograms(pZXings, AmplWindowWidth, ev, verbosityLevel );
 
 //-----------------------------------------------------------------------------
@@ -557,4 +574,7 @@
 //-------------------------------------
 
+
+
+
 //-------------------------------------
 // Histogramms of Maximas in Overlay Spectra
@@ -567,4 +587,17 @@
                  );
 
+         PlotMedianEachSliceOfPulse(
+                     MAX_PULS_ORDER,
+                     fitdata,
+                     verbosityLevel
+                 );
+
+         WritePixelTemplateToCsv(
+                     OutPutPath,
+                     "PulseTemplate_PointSet",
+                     "Maximum",
+                     pixel,
+                     verbosityLevel
+                     );
 
        if (verbosityLevel > 2) cout << "...done" << endl;
@@ -599,4 +632,5 @@
        {
            //Process gui events asynchronously during input
+           cout << endl;
            TTimer timer("gSystem->ProcessEvents();", 50, kFALSE);
            timer.TurnOn();
@@ -647,4 +681,11 @@
     }
 
+    WriteAllPixelTemplateToCsv(
+                OutPutPath,
+                "PulseTemplate_PointSet",
+                "Maximum",
+                verbosityLevel
+                );
+
     DeletePixelCanvases( verbosityLevel );
     return( 0 );
@@ -662,5 +703,6 @@
 
 
-void BookPixelHistos( int verbosityLevel)
+void
+BookPixelHistos( int verbosityLevel)
 {
     if (verbosityLevel > 2) cout << endl << "...book pixel histograms" << endl;
@@ -686,5 +728,5 @@
                     512,
                     -55.5,
-                    300.5
+                    200.5
                     );
         hPixelOverlay[order]->SetAxisRange(
@@ -722,5 +764,57 @@
         hList->Add( hPixelMax[order] );
 
-    //------------------------------------------------------------------------
+//------------------------------------------------------------------------
+    //        SetHistogramAttributes(
+    //                    "PeakMaximum",
+    //                    order,
+    //                    gMaximumAttrib,
+    //                    MaxAmplOfFirstPulse
+    //                    );
+            histoname = "hPixelMedian";
+            histoname += order;
+            if (verbosityLevel > 3) cout << "...booking " << histoname << endl;
+            hPixelMedian[order] = new TH1F(
+                        histoname,
+                        "Median of each slice in overlay plot",
+                        gPixelOverlayXaxisLeft + gPixelOverlayXaxisRight ,
+                        (-1*gPixelOverlayXaxisLeft)-0.5,
+                        gPixelOverlayXaxisRight-0.5
+                        );
+            hPixelMedian[order]->SetAxisRange(
+                        gBSLMean - 5,
+                        (gGainMean*(order+1)) + 10,
+                        "Y");
+            hPixelMedian[order]->SetLineColor(kRed);
+            hPixelMedian[order]->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
+            hPixelMedian[order]->GetYaxis()->SetTitle( "Amplitude [mV]" );
+            hList->Add( hPixelMedian[order] );
+
+//------------------------------------------------------------------------
+    //        SetHistogramAttributes(
+    //                    "PeakMaximum",
+    //                    order,
+    //                    gMaximumAttrib,
+    //                    MaxAmplOfFirstPulse
+    //                    );
+            histoname = "hPixelMean";
+            histoname += order;
+            if (verbosityLevel > 3) cout << "...booking " << histoname << endl;
+            hPixelMean[order] = new TH1F(
+                        histoname,
+                        "Mean of each slice in overlay plot",
+                        gPixelOverlayXaxisLeft + gPixelOverlayXaxisRight ,
+                        (-1*gPixelOverlayXaxisLeft)-0.5,
+                        gPixelOverlayXaxisRight-0.5
+                        );
+            hPixelMean[order]->SetAxisRange(
+                        gBSLMean - 5,
+                        (gGainMean*(order+1)) + 10,
+                        "Y");
+            hPixelMean[order]->SetLineColor(kBlue);
+            hPixelMean[order]->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
+            hPixelMean[order]->GetYaxis()->SetTitle( "Amplitude [mV]" );
+            hList->Add( hPixelMean[order] );
+
+//------------------------------------------------------------------------
 //        SetHistogramAttributes(
 //                    "PeakMaxGaus",
@@ -740,5 +834,5 @@
                     512,
                     -55.5,
-                    300.5
+                    200.5
                     );
 
@@ -771,5 +865,5 @@
                     300.5,                                          //yup
                     "s");                                           //option
-        hPixelMax[order]->SetAxisRange(
+        hPixelProfile[order]->SetAxisRange(
                     gBSLMean - 5,
                     (gGainMean*(order+1)) + 10,
@@ -779,4 +873,38 @@
         //hPixelProfile->SetBit(TH2F::kCanRebin);
         hList->Add( hPixelProfile[order] );
+
+
+    //------------------------------------------------------------------------
+    //        SetHistogramAttributes(
+    //                    "PeakProfile",
+    //                    order,
+    //                    gProfileAttrib,
+    //                    MaxAmplOfFirstPulse
+    //                    );
+            histoname = "hPixelProfile2";
+            histoname += order;
+            if (verbosityLevel > 3) cout << "...booking " << histoname << endl;
+            hPixelProfile2[order] = new TProfile(
+                        histoname,
+                        "Mean value of each slice in overlay plot (Tprofile)",
+                        gPixelOverlayXaxisLeft + gPixelOverlayXaxisRight ,//nbinsx
+                        (-1*gPixelOverlayXaxisLeft)-0.5,                 //xlow
+                        gPixelOverlayXaxisRight-0.5 ,                    //xup
+        //                512,                                            //nbinsy
+                        -55.5,                                          //ylow
+                        300.5,                                          //yup
+                        "s");                                           //option
+            hPixelProfile2[order]->SetLineColor(kRed);
+            hPixelProfile2[order]->SetAxisRange(
+                        gBSLMean - 5,
+                        (gGainMean*(order+1)) + 10,
+                        "Y");
+            hPixelProfile2[order]->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
+            hPixelProfile2[order]->GetYaxis()->SetTitle( "Amplitude [mV]" );
+            //hPixelProfile->SetBit(TH2F::kCanRebin);
+
+
+
+
     }
     if (verbosityLevel > 2) cout << "...done" << endl;
@@ -795,8 +923,14 @@
         if (verbosityLevel > 3) cout << endl << "...deleting hPixelMax" << order;
         delete hPixelMax[order];
+        if (verbosityLevel > 3) cout << endl << "...deleting hPixelMedian" << order;
+        delete hPixelMedian[order];
+        if (verbosityLevel > 3) cout << endl << "...deleting hPixelMean" << order;
+        delete hPixelMean[order];
         if (verbosityLevel > 3) cout << endl << "...deleting hPixelEdgeOverlay" << order;
         delete hPixelEdgeOverlay[order];
         if (verbosityLevel > 3) cout << endl << "...deleting hPixelProfile" << order;
         delete hPixelProfile[order];
+        if (verbosityLevel > 3) cout << endl << "...deleting hPixelProfile2" << order;
+        delete hPixelProfile2[order];
     }
     if (verbosityLevel > 3) cout << endl << "...deleting hList";
@@ -814,10 +948,24 @@
                 "hTesthisto",
                 "Deviation of rising edge and maximum",
-                200,
-                -300,
-                300
+                600,
+                -10.1,
+                10.1
                 );
     hTesthisto->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
     hTesthisto->GetYaxis()->SetTitle( "counts" );
+
+    hTesthisto2 = new TH2F (
+                "hTesthisto2",
+                "Deviation of rising edge and maximum by event #",
+                gNEvents,
+                250,
+                800,
+                600,
+                -10.1,
+                10.1
+                );
+//    hTesthisto2->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
+//    hTesthisto2->GetYaxis()->SetTitle( "counts" );
+//    hTesthisto2->SetMarkerStyle( 2 );
 
     if (verbosityLevel > 2) cout << "...done" << endl;
@@ -910,30 +1058,86 @@
         hAllPixelList->Add( hAllPixelMax[order] );
 
-    //------------------------------------------------------------------------
+//------------------------------------------------------------------------
 //        SetHistogramAttributes(
-//                    "AllPixelPeakMaxGaus",
+//                    "PeakMaximum",
+//                    order,
+//                    gMaximumAttrib,
+//                    MaxAmplOfFirstPulse
+//                    );
+        histoname = "hAllPixelMedian";
+        histoname += order;
+        if (verbosityLevel > 3) cout << "...booking " << histoname << endl;
+        hAllPixelMedian[order] = new TH1F(
+                    histoname,
+                    "Median of each slice in overlay plot for All Pixels",
+                    gPixelOverlayXaxisLeft + gPixelOverlayXaxisRight ,
+                    (-1*gPixelOverlayXaxisLeft)-0.5,
+                    gPixelOverlayXaxisRight-0.5
+                    );
+        hAllPixelMedian[order]->SetAxisRange(
+                    gBSLMean - 5,
+                    (gGainMean*(order+1)) + 10,
+                    "Y");
+        hAllPixelMedian[order]->SetLineColor(kRed);
+        hAllPixelMedian[order]->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
+        hAllPixelMedian[order]->GetYaxis()->SetTitle( "Amplitude [mV]" );
+        hAllPixelList->Add( hAllPixelMedian[order] );
+        if (verbosityLevel > 3) cout << "...BREAKPOINT in " << histoname << endl;
+//------------------------------------------------------------------------
+//        SetHistogramAttributes(
+//                    "PeakMaximum",
+//                    order,
+//                    gMaximumAttrib,
+//                    MaxAmplOfFirstPulse
+//                    );
+        histoname = "hAllPixelMean";
+        histoname += order;
+        if (verbosityLevel > 3) cout << "...booking " << histoname << endl;
+        hAllPixelMean[order] = new TH1F(
+                    histoname,
+                    "Mean of each slice in overlay plot for All Pixels",
+                    gPixelOverlayXaxisLeft + gPixelOverlayXaxisRight ,
+                    (-1*gPixelOverlayXaxisLeft)-0.5,
+                    gPixelOverlayXaxisRight-0.5
+                    );
+        hAllPixelMean[order]->SetAxisRange(
+                    gBSLMean - 5,
+                    (gGainMean*(order+1)) + 10,
+                    "Y");
+        hAllPixelMean[order]->SetLineColor(kBlue);
+        hAllPixelMean[order]->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
+        hAllPixelMean[order]->GetYaxis()->SetTitle( "Amplitude [mV]" );
+        hAllPixelList->Add( hAllPixelMean[order] );
+
+//------------------------------------------------------------------------
+//        SetHistogramAttributes(
+//                    "PeakMaxGaus",
 //                    order,
 //                    gMaxGausAttrib,
 //                    MaxAmplOfFirstPulse
 //                    );
-        histoname = "hAllPixelMaxGaus";
+        histoname = "hAllPixelEdgeOverlay";
         histoname += order;
         if (verbosityLevel > 3) cout << "...booking " << histoname << endl;
-        hAllPixelMaxGaus[order] = new TH1F(
+        hAllPixelEdgeOverlay[order] = new TH2F(
                     histoname,
-                    "Mean value of each slice in overlay plot for All Pixels",
+                    "Overlay at rising edge of detected pulses of one pulse order for All Pixels ",
                     gPixelOverlayXaxisLeft + gPixelOverlayXaxisRight ,
                     (-1*gPixelOverlayXaxisLeft)-0.5,
-                    gPixelOverlayXaxisRight-0.5
+                    gPixelOverlayXaxisRight-0.5 ,
+                    512,
+                    -55.5,
+                    200.5
                     );
-        hAllPixelMaxGaus[order]->SetAxisRange(
+
+        hAllPixelEdgeOverlay[order]->SetAxisRange(
                     gBSLMean - 5,
                     (gGainMean*(order+1)) + 10,
                     "Y");
-        hAllPixelMaxGaus[order]->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
-        hAllPixelMaxGaus[order]->GetYaxis()->SetTitle( "Amplitude [mV]" );
-        hAllPixelList->Add( hAllPixelMaxGaus[order] );
-
-    //------------------------------------------------------------------------
+        hAllPixelEdgeOverlay[order]->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
+        hAllPixelEdgeOverlay[order]->GetYaxis()->SetTitle( "Amplitude [mV]" );
+        hAllPixelList->Add( hAllPixelEdgeOverlay[order] );
+
+//------------------------------------------------------------------------
 //        SetHistogramAttributes(
 //                    "AllPixelPeakProfile",
@@ -964,4 +1168,34 @@
         hAllPixelList->Add( hAllPixelProfile[order] );
 
+//------------------------------------------------------------------------
+//        SetHistogramAttributes(
+//                    "AllPixelPeakProfile",
+//                    order,
+//                    gProfileAttrib,
+//                    MaxAmplOfFirstPulse
+//                    );
+        histoname = "hAllPixelProfile2";
+        histoname += order;
+        if (verbosityLevel > 3) cout << "...booking " << histoname << endl;
+        hAllPixelProfile2[order] = new TProfile(
+                    histoname,
+                    "Mean value of each slice in overlay plot for All Pixels (Tprofile)",
+                    gPixelOverlayXaxisLeft + gPixelOverlayXaxisRight ,//nbinsx
+                    (-1*gPixelOverlayXaxisLeft)-0.5,                 //xlow
+                    gPixelOverlayXaxisRight-0.5 ,                    //xup
+    //                512,                                            //nbinsy
+                    -55.5,                                          //ylow
+                    300.5,                                          //yup
+                    "s");                                           //option
+        hAllPixelProfile2[order]->SetAxisRange(
+                    gBSLMean - 5,
+                    (gGainMean*(order+1)) + 10,
+                    "Y");
+        hAllPixelProfile2[order]->SetLineColor(kRed);
+        hAllPixelProfile2[order]->GetXaxis()->SetTitle( "Timeslices [a.u.]" );
+        hAllPixelProfile2[order]->GetYaxis()->SetTitle( "Amplitude [mV]" );
+        //hPixelProfile->SetBit(TH2F::kCanRebin);
+        hAllPixelList->Add( hAllPixelProfile2[order] );
+
 
 
@@ -1077,4 +1311,5 @@
         vector<Region>* pZXings,
         int AmplWindowWidth,
+        int eventNumber,
         int verbosityLevel
         )
@@ -1103,7 +1338,11 @@
 
         if (Left < 0) Left =0;
-        if (Right > (int)Vcorr.size() ) Right=Vcorr.size() ;
-
-        hTesthisto->Fill( reg->maxPos - reg->halfRisingEdgePos ) ;
+        if (Right > (int)Ameas.size() ) Right=Ameas.size() ;
+        if (EdgeLeft < 0) EdgeLeft =0;
+        if (EdgeRight > (int)Ameas.size() ) EdgeRight=Ameas.size() ;
+
+
+        hTesthisto->Fill( reg->slopeOfRisingEdge ) ;
+        hTesthisto2->Fill( eventNumber, reg->slopeOfRisingEdge ) ;
 
         if (verbosityLevel > 2) cout << endl << "\t...choosing Histogram" ;
@@ -1132,4 +1371,5 @@
             if (verbosityLevel > 2) cout << "...filling" ;
             for ( int pos = Left; pos < Right; pos++){
+//                if ();
                 hPixelOverlay[order_of_pulse]->Fill( pos - (reg->maxPos), Ameas[pos]) ;
                 hPixelProfile[order_of_pulse]->Fill( pos - (reg->maxPos), Ameas[pos]) ;
@@ -1140,4 +1380,7 @@
 //                cout << endl << "###filling edge histo###" << endl;
                 hPixelEdgeOverlay[order_of_pulse]->Fill( pos - (reg->halfRisingEdgePos), Ameas[pos]) ;
+                hPixelProfile2[order_of_pulse]->Fill( pos - (reg->halfRisingEdgePos), Ameas[pos]) ;
+                hAllPixelEdgeOverlay[order_of_pulse]->Fill( pos - (reg->halfRisingEdgePos), Ameas[pos]) ;
+                hAllPixelProfile2[order_of_pulse]->Fill( pos - (reg->halfRisingEdgePos), Ameas[pos]) ;
 //                cout << endl << "######" << endl;
             }
@@ -1161,4 +1404,6 @@
     cgpTestHistos->cd(1);
     hTesthisto->Draw();
+    cgpTestHistos->cd(2);
+    hTesthisto2->Draw("BOX");
 }
 // end of DrawTestHistograms
@@ -1175,7 +1420,9 @@
         cgpPixelPulses[pulse_order]->cd(1);
         hPixelOverlay[pulse_order]->Draw("COLZ");
+        cgpPixelPulses[pulse_order]->cd(3);
+        hPixelProfile[pulse_order]->Draw("COLZ");
+        hPixelProfile2[pulse_order]->Draw("SAME");
+
         cgpPixelPulses[pulse_order]->cd(2);
-        hPixelProfile[pulse_order]->Draw("COLZ");
-        cgpPixelPulses[pulse_order]->cd(3);
         hPixelEdgeOverlay[pulse_order]->Draw("COLZ");
 //        cgpPixelPulses[pulse_order]->cd(4);
@@ -1185,5 +1432,9 @@
         hAllPixelOverlay[pulse_order]->Draw("COLZ");
         cgpAllPixelPulses[pulse_order]->cd(2);
+        hAllPixelEdgeOverlay[pulse_order]->Draw("COLZ");
+
+        cgpAllPixelPulses[pulse_order]->cd(3);
         hAllPixelProfile[pulse_order]->Draw("COLZ");
+        hAllPixelProfile2[pulse_order]->Draw("SAME");
 
     }
@@ -1202,8 +1453,12 @@
     {
         cgpPixelPulses[pulse_order]->cd(4);
-        hPixelMax[pulse_order]->Draw("LF2");
+        hPixelMax[pulse_order]->Draw();
+        hPixelMedian[pulse_order]->Draw("SAME");
+        hPixelMean[pulse_order]->Draw("SAME");
 
         cgpAllPixelPulses[pulse_order]->cd(4);
         hAllPixelMax[pulse_order]->Draw();
+        hAllPixelMedian[pulse_order]->Draw("SAME");
+        hAllPixelMean[pulse_order]->Draw("SAME");
 //        cgpAllPixelPulses[pulse_order]->cd(3);
      //   hAllPixelMaxGaus[pulse_order]->Draw("COLZ");
@@ -1236,4 +1491,67 @@
 
 void
+PlotMedianEachSliceOfPulse(
+        unsigned int    max_pulse_order,
+        int             fitdata,
+        int             verbosityLevel
+        )
+{
+    if (verbosityLevel > 2) cout << endl
+                                 << "...calculating pulse shape of slice's Median"
+                                 << endl;
+    for (unsigned int pulse_order = 0 ; pulse_order < max_pulse_order ; pulse_order ++)
+    {
+        if (verbosityLevel > 2) cout << "\t...calculation of "
+                                     << "pulse order " << pulse_order;
+
+        Int_t nbins = hPixelOverlay[pulse_order]->GetXaxis()->GetNbins();
+
+        for (Int_t TimeSlice=1;TimeSlice<=nbins;TimeSlice++) {
+           TH1 *h1 = hPixelOverlay[pulse_order]->ProjectionY("",TimeSlice,TimeSlice);
+           float median = MedianOfH1(h1);
+           float mean = h1->GetMean();
+           if (verbosityLevel > 2) printf("Median of Slice %d, Median=%g\n",TimeSlice,median);
+           delete h1;
+           hPixelMedian[pulse_order]->SetBinContent(TimeSlice, median );
+           hPixelMean[pulse_order]->SetBinContent(TimeSlice, mean );
+           hAllPixelMedian[pulse_order]->SetBinContent(TimeSlice, median );
+           hAllPixelMean[pulse_order]->SetBinContent(TimeSlice, mean );
+        }
+
+        if (verbosityLevel > 2) cout << "\t...done" << endl;
+
+        if (fitdata)
+        {
+           FitMaxPropabilityPuls(
+                    hPixelMedian[pulse_order],
+                    verbosityLevel
+                    );
+        }
+    }
+}
+// end of PlotMedianEachSliceOfPulse
+//----------------------------------------------------------------------------
+
+Double_t MedianOfH1 (
+        TH1*            h1
+        )
+{
+   //compute the median for 1-d histogram h1
+   Int_t nbins = h1->GetXaxis()->GetNbins();
+   Double_t *x = new Double_t[nbins];
+   Double_t *y = new Double_t[nbins];
+   for (Int_t i=0;i<nbins;i++) {
+      x[i] = h1->GetXaxis()->GetBinCenter(i+1);
+      y[i] = h1->GetBinContent(i+1);
+   }
+   Double_t median = TMath::Median(nbins,x,y);
+   delete [] x;
+   delete [] y;
+   return median;
+}
+// end of PlotMedianEachSliceOfPulse
+//----------------------------------------------------------------------------
+
+void
 PlotPulseShapeOfMaxPropability(
         unsigned int    max_pulse_order,
@@ -1247,22 +1565,24 @@
     for (unsigned int pulse_order = 0 ; pulse_order < max_pulse_order ; pulse_order ++)
     {
-        if (verbosityLevel > 2) cout << "\t...calculating of "
+        if (verbosityLevel > 2) cout << "\t...calculation of "
                                      << "pulse order " << pulse_order;
         //  vector max_value_of to number of timeslices in Overlay Spectra
-        vector<OverlayMaximum> max_value_of;
-        max_value_of.resize(hPixelOverlay[pulse_order]->GetNbinsX());
-
-        for (int TimeSlice = 0;
-             TimeSlice <= hPixelOverlay[pulse_order]->GetNbinsX();
-             TimeSlice++ )
+        float max_value_of_slice;
+
+        Int_t nbins = hPixelOverlay[pulse_order]->GetXaxis()->GetNbins();
+        if (verbosityLevel > 2) cout << "...generating projections" << endl;
+        for (Int_t TimeSlice=1;TimeSlice<=nbins;TimeSlice++)
         {
-            histotitle = "hproj_py";    //generate title of histogram of which MaxVal
-            histotitle += pulse_order ; //will be calculated
-
+            if (verbosityLevel > 2) cout << "...Timeslice: " << TimeSlice;
             //put maximumvalue of every Y-projection of every timeslice into vector
-            hProjPeak =	hPixelOverlay[pulse_order]->ProjectionY(histotitle, TimeSlice, TimeSlice, "");
-            max_value_of[ TimeSlice ].maxAmpl = (hProjPeak->GetMaximumBin() * 0.5) - 3.5;
-            max_value_of[ TimeSlice ].countOfMax = hProjPeak->GetBinContent( hProjPeak->GetMaximumBin() );
-            hPixelMax[pulse_order]->SetBinContent(TimeSlice, max_value_of[ TimeSlice ].maxAmpl );
+            TH1D *h1 =	hPixelOverlay[pulse_order]->ProjectionY( "", TimeSlice,TimeSlice);
+
+            max_value_of_slice = h1->GetBinCenter( h1->GetMaximumBin() );
+
+            if (verbosityLevel > 2) cout << " with max value "
+                                         << max_value_of_slice << endl;
+            hPixelMax[pulse_order]->SetBinContent(TimeSlice, max_value_of_slice );
+            hAllPixelMax[pulse_order]->SetBinContent(TimeSlice, max_value_of_slice );
+            delete h1;
         }
 
@@ -1282,6 +1602,6 @@
 void
 FitMaxPropabilityPuls(
-        TH1F*   hMaximumTemp,
-        int     verbosityLevel
+        TH1F*       hMaximumTemp,
+        int         verbosityLevel
         )
     {
@@ -1415,2 +1735,92 @@
 //    histoAttrib[pulse_order].yTitle += "Amplitude [mV]";
 //}
+
+void WritePixelTemplateToCsv(
+        TString         path,
+        const char*     csv_file_name,
+        const char*     overlay_method,
+        int             pixel,
+        int             verbosityLevel
+        )
+{
+    path += csv_file_name;
+    path += "_";
+    path += pixel;
+    path += ".csv";
+
+    Int_t nbins = hPixelMax[0]->GetXaxis()->GetNbins();
+    if (verbosityLevel > 0)
+    {
+        cout << "writing point-set to csv file: " ;
+        cout << path << endl;
+        cout << "...opening file" << endl;
+    }
+    if (verbosityLevel > 2) cout << "...number of bins " << nbins << endl;
+    ofstream out;
+    out.open( path );
+    out << "### point-set of a single photon pulse template" << endl;
+    out << "### template determined with pulse overlay at: "
+        << overlay_method << endl;
+    out << "### Slice's Amplitude determined by calculating the " << endl
+        << "### value of maximum propability of slice -> Amplitude1 " << endl
+        << "### mean of slice -> Amplitude2 " << endl
+        << "### median of slice -> Amplitude3 " << endl
+        << "### for each slice" << endl;
+    out << "### Pixel number (CHid): " << pixel << endl
+        << endl;
+
+    out << "time [slices],Amplitude1 [mV],Amplitude2 [mV],Amplitude3 [mV]" << endl;
+
+    for (int TimeSlice=1;TimeSlice<=nbins;TimeSlice++)
+    {
+        out << TimeSlice << "," ;
+        out << hPixelMax[0]->GetBinContent(TimeSlice) << ",";
+        out << hPixelMean[0]->GetBinContent(TimeSlice) << ",";
+        out << hPixelMedian[0]->GetBinContent(TimeSlice) << endl;
+    }
+    out.close();
+    if (verbosityLevel > 0) cout << "...file closed" << endl;
+}
+
+void WriteAllPixelTemplateToCsv(
+        TString         path,
+        const char*     csv_file_name,
+        const char*     overlay_method,
+        int             verbosityLevel
+        )
+{
+    path += csv_file_name;
+    path += "_AllPixel.csv";
+
+    Int_t nbins = hAllPixelMax[0]->GetXaxis()->GetNbins();
+    if (verbosityLevel > 0)
+    {
+        cout << "writing point-set to csv file: " ;
+        cout << path << endl;
+        cout << "...opening file" << endl;
+    }
+    if (verbosityLevel > 2) cout << "...number of bins " << nbins << endl;
+    ofstream out;
+    out.open( path );
+    out << "### point-set of a single photon pulse template" << endl;
+    out << "### template determined with pulse overlay at: "
+        << overlay_method << "of all Pixels";
+    out << "### Slice's Amplitude determined by calculating the " << endl
+        << "### value of maximum propability of slice -> Amplitude1 " << endl
+        << "### mean of slice -> Amplitude2 " << endl
+        << "### median of slice -> Amplitude3 " << endl
+        << "### for each slice" << endl
+        << endl;
+
+    out << "time [slices],Amplitude1 [mV],Amplitude2 [mV],Amplitude3 [mV]" << endl;
+
+    for (int TimeSlice=1;TimeSlice<=nbins;TimeSlice++)
+    {
+        out << TimeSlice << "," ;
+        out << hAllPixelMax[0]->GetBinContent(TimeSlice) << ",";
+        out << hAllPixelMean[0]->GetBinContent(TimeSlice) << ",";
+        out << hAllPixelMedian[0]->GetBinContent(TimeSlice) << endl;
+    }
+    out.close();
+    if (verbosityLevel > 0) cout << "...file closed" << endl;
+}
