Index: fact/tools/rootmacros/PulseTemplates/FPulseOverlay_filelist.C
===================================================================
--- fact/tools/rootmacros/PulseTemplates/FPulseOverlay_filelist.C	(revision 14808)
+++ fact/tools/rootmacros/PulseTemplates/FPulseOverlay_filelist.C	(revision 14809)
@@ -179,8 +179,8 @@
 //void SaveHistograms( const char*, const char*, TObjArray*, int );
 
-void FillHistograms(Pixel*, vector<Region>*, int, int, int , int );
+void FillHistograms(Pixel*, vector<Region>*, int, int, int , bool , int verbosityLevel );
 void DrawTestHistograms( int);
 bool ProduceDebugHistos( vector<Region> *pZXings);
-bool  UseThisPulse( int, int, int, float, int, int);
+bool  UseThisPulse( int, int, int, float, float bslMean, int maxPulseOrder, int verbosityLevel);
 void UpdateCanvases( int, int, bool);
 void DeletePixelCanvases( int, int );
@@ -211,32 +211,32 @@
 //----------------------------------------------------------------------------
 int FPulseOverlay_filelist(
-        TString     inputPath           = "raw/2012/03/09/",
-        TString     sequenzFileName = "/home/jbuss/macros/isdc_rootmacros/PulseTemplates/seq",
-//        TString     dataFileSuffix      = ".fits.gz",
+        TString     inputPath           = "raw/2012/08/02/",
+        TString     sequenzFileName = "/home_nfs/isdc/jbbuss/analysis/pulseShape/lists/20120802templ_vs_0mV.csv",
 //        int         firstRunId          = 18,
-//        int         nRuns               = 50,
+        int         nRuns               = -1,
 //        TString     drsfilename         = "20120309_012.drs.fits.gz",
-        TString     OutputPath          = "analysis/FPulseTemplate/20120309_018/Overlay/",
-        TString     OutRootFileName     = "20120309_018.root",
+        TString     OutputPath          = "analysis/pulseShape/",
+        TString     OutRootFileName     = "20120802_0mV_newSelectAlg.root",
         int         firstevent          = 1,        // Hast to be between 1 and infinity NOT 0!!!!
-        int         nevents             = 100,
+        int         nevents             = -1,
         int         firstpixel          = 0,
-        int         npixel              = 2,
+        int         npixel              = 1,
         int         pixelSetSize       = 150,
-        int         maxPulseOrder       = 1,
-        int         verbosityLevel      = 6,        // different verbosity levels can be implemented here
+        int         maxPulseOrder       = 6,
+        int         verbosityLevel      = 0,        // different verbosity levels can be implemented here
         TString     histoOptions        = "SRM",
-        int         AmplWindowWidth     = 14,       //Width of Window for selection of pulses to histograms
-        float       GainMean            = 9,
-        float       BSLMean             = -1,        //4 Histogramms will be drawn, decide how far pulsehights differ from eachother
+        int         AmplWindowWidth     = 7,       //Width of Window for selection of pulses to histograms
+        float       GainMean            = 11,
+        float       BSLMean             = -2,        //4 Histogramms will be drawn, decide how far pulsehights differ from eachother
         int         avg1                = 8,
         int         avg2                = 8,
-        int         OverlayWindowLeft   = 70,
-        int         OverlayWindowRight  = 230,
-        bool        ProduceGraphic      = true,
+        int         OverlayWindowLeft   = 1024,
+        int         OverlayWindowRight  = 1024,
+        bool            select_pulses   = false,
+        bool        ProduceGraphic      = false,
         bool        spikeDebug          = false,
         bool        debugPixel          = false,
-        bool        stats               = false,
-        bool        saveResults         = false,
+        bool        stats               = true,
+        bool        saveResults         = true,
         bool        testmode            = false,
         int         refresh_rate        = 500      //refresh rate for canvases
@@ -244,8 +244,23 @@
 {
 //-----------------------------------------------------------------------------
+// configuration
+//-----------------------------------------------------------------------------
+
+    float minPulseAmplitude     = 0.3*(GainMean-BSLMean);
+    float maxPulseAmplitude     = 2*(GainMean-BSLMean)*maxPulseOrder;
+    int   timeLineEdgeWidth     = 2;
+    int   FallingEdgeWidth      = 200;
+    int   beginRisingEdge       = 5;
+    int   endRisingEdge         = 10;
+    float   risingEdgeMinSlope    = 0.5;
+
+//-----------------------------------------------------------------------------
 // prepare datafiles
 //-----------------------------------------------------------------------------
 
     inputPath       = SetHostsPaths(false, inputPath );
+
+    TString     fitsFileSuffix      = ".fits.gz";
+    TString     drsFileSuffix       = ".drs.fits.gz";
 
     // declare the data file
@@ -260,4 +275,5 @@
     vector<TString> drsFileNames;
 
+    cout << "...reading sequnezfile" << endl;
     ReadSequenzFile(
             sequenzFileName,
@@ -270,4 +286,10 @@
     //fill vector of fits files to read
     cout << "generating file list:" << endl;
+    if(nRuns < fitsFileNames.size() && nRuns != -1)
+    {
+        fitsFileNames.resize(   nRuns);
+        drsFileNames.resize(    nRuns);
+    }
+
     for (unsigned int fileNr = 0; fileNr < fitsFileNames.size(); fileNr++)
     {
@@ -288,6 +310,8 @@
         TString fits_filename = inputPath;
         fits_filename.Append(fitsFileNames.at(fileNr));
+        fits_filename.Append(fitsFileSuffix);
         TString drs_filename = inputPath;
         drs_filename.Append(drsFileNames.at(fileNr));
+        drs_filename.Append(drsFileSuffix);
         cout << "\n\t fits:\t" << fits_filename;
         cout << "\n\t drs:\t" << drs_filename << endl;
@@ -321,4 +345,6 @@
          << " drs: "
          << drsFileNames.size() << endl;
+
+    if (fitsFileNames.size() < 1) return 1;
 
 //----------------------------------------------------------------------------
@@ -462,4 +488,7 @@
 // Main Cycle
 //-----------------------------------------------------------------------------
+    cout << "------------------Main cylcle------------------------------" << endl;
+
+    if(npixel == -1) npixel = 1439;
 
     if ( pixelSetSize == -1 )
@@ -468,4 +497,5 @@
     }
     int lastPixelOfSet = 0;
+    cout << "check " << npixel << endl;
 //-------------------------------------
 // Loop over Pixel Sets
@@ -497,4 +527,5 @@
         //-------------------------------------
 
+        cout << "loop over files" << endl;
         for( unsigned int fileId = 0; fileId < fitsFileNames.size(); fileId++ )
         {
@@ -597,5 +628,4 @@
                 }
 
-                cout << "\t\t success!" << endl;
     //--------------------------------------------------------------------
     // Loops over every Pixel of a Set of Pixels
@@ -622,5 +652,5 @@
     // Create individual Pixel
     //-------------------------------------
-                    if (ev == firstevent)
+                    if (ev == firstevent && fileId == 0)
                     {
                         pixel[pixelID] = new Pixel(
@@ -680,5 +710,5 @@
 
     //-------------------------------------
-    // SeagRCh vor Zero crossings
+    // Search vor Zero crossings
     //-------------------------------------
                     if (verbosityLevel > 2) cout << endl << "...seagRChing zero crossings" ;
@@ -691,9 +721,12 @@
                     EnlargeRegion(*pZXings, 10, 10);
                     findAbsMaxInRegions(*pZXings, gVslide);
-                    removeMaximaBelow( *pZXings, 3.0);
-                    removeRegionWithMaxOnEdge( *pZXings, 2);
-                    removeRegionOnFallingEdge( *pZXings, 100);
-                    findTimeOfHalfMaxLeft(*pZXings, gVslide, gBSLMean, 5, 10, verbosityLevel );
-                    removeRegionWithToFlatSlope(*pZXings, 0.5);
+                    removeMaximaBelow( *pZXings, minPulseAmplitude);
+                    removeMaximaAbove( *pZXings, maxPulseAmplitude);
+                    removeRegionWithMaxOnEdge( *pZXings, timeLineEdgeWidth);
+                    removeRegionOnFallingEdge( *pZXings, FallingEdgeWidth);
+                    // Calculate the position (sample) of a pulses half maximum
+                    findTimeOfHalfMaxLeft(*pZXings, gVslide, gBSLMean, beginRisingEdge, endRisingEdge, verbosityLevel );
+                    //sometimes pulses where found with a very flat slope, DISMISS them
+                    removeRegionWithToFlatSlope(*pZXings, risingEdgeMinSlope);
                     if (verbosityLevel > 2) cout << "...done" << endl;
 
@@ -713,4 +746,5 @@
     //                            histoOptions,
                                 maxPulseOrder,
+                                select_pulses,
                                 verbosityLevel
                                 );
@@ -994,7 +1028,17 @@
         int             eventNumber,
         int             maxPulseOrder,
+        bool            select_pulses,
         int             verbosityLevel
         )
 {
+    //entry counters
+    int maxOverlayEntries   = 0;
+    int edgeOverlayEntries  = 0;
+    int maxProfileEntries   = 0;
+    int edgeProfileEntries  = 0;
+
+
+
+
     if (verbosityLevel > 2) cout << endl << "...filling pulse histograms" ;
     if (verbosityLevel > 3) cout << endl << "...EventNR " << eventNumber ;
@@ -1020,6 +1064,6 @@
         if (Left < 0)                           Left        =   0;
         if (EdgeLeft < 0)                       EdgeLeft    =   0;
-        if (Right > (int)gAmeas.size() )         Right       =   gAmeas.size();
-        if (EdgeRight > (int)gAmeas.size() )     EdgeRight   =   gAmeas.size();
+        if (Right > (int)gVcorr.size() )         Right       =   gVcorr.size();
+        if (EdgeRight > (int)gVcorr.size() )     EdgeRight   =   gVcorr.size();
 
 
@@ -1033,4 +1077,5 @@
                         AmplWindowWidth,
                         gGainMean,
+                        gBSLMean,
                         maxPulseOrder,
                         verbosityLevel
@@ -1042,7 +1087,18 @@
             }
         }
+        if (select_pulses){
+            order_of_pulse = 0;
+            use_this_peak = true;
+        }
         //Fill histograms
         if (use_this_peak)
         {
+            //get number of entries of used histograms
+            maxOverlayEntries   = CurrentPixel->hMaxOverlay[order_of_pulse]->GetEntries();
+            edgeOverlayEntries  = CurrentPixel->hEdgeOverlay[order_of_pulse]->GetEntries();
+            maxProfileEntries   = CurrentPixel->hMaxProfile[order_of_pulse]->GetEntries();
+            edgeProfileEntries  = CurrentPixel->hEdgeProfile[order_of_pulse]->GetEntries();
+
+
             //Histograms for Distributions
             if (CurrentPixel->mOptions.Contains("S") )
@@ -1070,13 +1126,19 @@
             for ( int pos = Left; pos < Right; pos++)
             {
-                CurrentPixel->hMaxOverlay[order_of_pulse]->Fill( pos - (reg->maxPos), gAmeas[pos]) ;
-                CurrentPixel->hMaxProfile[order_of_pulse]->Fill( pos - (reg->maxPos), gAmeas[pos]) ;
+                CurrentPixel->hMaxOverlay[order_of_pulse]->Fill( pos - (reg->maxPos), gVcorr[pos]) ;
+                CurrentPixel->hMaxProfile[order_of_pulse]->Fill( pos - (reg->maxPos), gVcorr[pos]) ;
             }
             //Histograms for Edge Overlay
             for ( int pos = EdgeLeft; pos < EdgeRight; pos++)
             {
-                CurrentPixel->hEdgeOverlay[order_of_pulse]->Fill( pos - (reg->halfRisingEdgePos), gAmeas[pos]) ;
-                CurrentPixel->hEdgeProfile[order_of_pulse]->Fill( pos - (reg->halfRisingEdgePos), gAmeas[pos]) ;
+                CurrentPixel->hEdgeOverlay[order_of_pulse]->Fill( pos - (reg->halfRisingEdgePos), gVcorr[pos]) ;
+                CurrentPixel->hEdgeProfile[order_of_pulse]->Fill( pos - (reg->halfRisingEdgePos), gVcorr[pos]) ;
             }
+
+            //set histogram entries
+            CurrentPixel->hMaxOverlay[order_of_pulse]->SetEntries(  maxOverlayEntries +1  );
+            CurrentPixel->hEdgeOverlay[order_of_pulse]->SetEntries( edgeOverlayEntries +1 );
+            CurrentPixel->hMaxProfile[order_of_pulse]->SetEntries(  maxProfileEntries +1  );
+            CurrentPixel->hEdgeProfile[order_of_pulse]->SetEntries( edgeProfileEntries +1 );
 
             if (verbosityLevel > 2) cout << "...done" << endl;
@@ -1099,4 +1161,5 @@
         int             AmplWindowWidth,
         float           gainMean,
+        float           bslMean,
         int             maxPulseOrder,
         int             verbosityLevel
@@ -1105,6 +1168,6 @@
     //determine if pulse is of given order
 bool use_this_peak = false;
-        if (gAmeas[maxPos] >= ((gainMean*(order+1)) - (AmplWindowWidth/2))
-            && gAmeas[maxPos] < ((gainMean*(order+1)) + AmplWindowWidth/2))
+        if (gVcorr[maxPos] >= ((gainMean*(order+1)) - bslMean - (AmplWindowWidth/2))
+            && gVcorr[maxPos] < ((gainMean*(order+1)) -bslMean + AmplWindowWidth/2))
         {
             if (verbosityLevel > 3) cout << "...#" << order ;
