Index: /trunk/MagicSoft/Mars/mtemp/mpisa/macros/AnalCurrents.C
===================================================================
--- /trunk/MagicSoft/Mars/mtemp/mpisa/macros/AnalCurrents.C	(revision 4435)
+++ /trunk/MagicSoft/Mars/mtemp/mpisa/macros/AnalCurrents.C	(revision 4436)
@@ -45,67 +45,106 @@
 void AnalCurrents(const TString filename)
 {
-  //UInt_t numEvents = 1000000;
-  UInt_t numEvents = 1;
-
-  //
-  // Create a empty Parameter List and an empty Task List
-  // The tasklist is identified in the eventloop by its name
-  //
-  MParList  plist;  
-  MTaskList tlist;
-  plist.AddToList(&tlist);
-
-
-  MCameraDC     dccam;
-
-  plist.AddToList(&geomcam);
-  plist.AddToList(&dccam);
-
-  //
-  // Now setup the tasks and tasklist:
-  // ---------------------------------
-  //
-  // Reads the trees of the root file and the analysed branches
-  MReadReports read;
-  read.AddTree("Currents"); 
-  read.AddFile(filename);     // after the reading of the trees!!!
-  read.AddToBranchList("MReportCurrents.*");
-
-  MGeomApply geomapl;
-
-  tlist.AddToList(&geomapl);
-  tlist.AddToList(&read);
-
-  //
-  // Create and setup the eventloop
-  //
-  MEvtLoop evtloop;
-  evtloop.SetParList(&plist);
+    //UInt_t numEvents = 1000000;
+    UInt_t numEvents = 1;
+    
+    //
+    // Create a empty Parameter List and an empty Task List
+    // The tasklist is identified in the eventloop by its name
+    //
+    MParList  plist;  
+    MTaskList tlist;
+    plist.AddToList(&tlist);
+    
+    
+    MCameraDC     dccam;
+    
+    plist.AddToList(&geomcam);
+    plist.AddToList(&dccam);
+    
+    //
+    // Now setup the tasks and tasklist:
+    // ---------------------------------
+    //
+    // Reads the trees of the root file and the analysed branches
+    MReadReports read;
+    read.AddTree("Currents"); 
+    read.AddFile(filename);     // after the reading of the trees!!!
+    read.AddToBranchList("MReportCurrents.*");
+    
+    MGeomApply geomapl;
+    
+    tlist.AddToList(&geomapl);
+    tlist.AddToList(&read);
+
+    //
+    // Create and setup the eventloop
+    //
+    MEvtLoop evtloop;
+    evtloop.SetParList(&plist);
      
-  MCerPhotEvt dcevt;
-  Int_t count = 0;
-  Double_t val;
-  //
-  // Execute your analysis
-  //
-  //if (!evtloop.Eventloop(numEvents))
-  //  return kFALSE;
-  if (!evtloop.PreProcess())
-    return;
-    
-  while (tlist.Process())
-    {
-      //cout << "Evt #" << ++count << endl;
-      for(Int_t i=0; i<gsNpix; i++){
-	dcevt.AddPixel(i,dccam.GetCurrent(i)/geomcam.GetPixRatio(i),0);
-	dcevt.GetPixelContent(val,i,geomcam,0);
-	//cout << i << "-" << dccam.GetCurrent(i) << "-" << val << endl;
-      }
-      //cout << endl;
-    }
-  tlist.PrintStatistics();
-
-  cout << "Calling test_findstar function" << endl;
-  test_findstar(&dcevt);
+    MCerPhotEvt dcevt;
+    
+    const Int_t NSteps = 5;  // Number of steps to average DC over events
+    Int_t count = 0;         // counter for averaging NSteps events
+    Int_t NumAverEvt = 1;    // counter for averaged events stored into dcevt
+    Double_t val[gsNpix];    // array for managing the averaging of gsNpix
+                             // DC values over NSteps events
+    
+    for(Int_t i=0; i<gsNpix; i++)
+	val[i] = 0.0;
+    //
+    // Execute your analysis
+    //
+    //if (!evtloop.Eventloop(numEvents))
+    //  return kFALSE;
+    if (!evtloop.PreProcess())
+	return;
+    
+    while (tlist.Process())
+    {
+	++count;
+	for(Int_t i=0; i<gsNpix; i++)
+	{
+	    val[i] += dccam.GetCurrent(i);
+	    if(i==27)
+		cout << "pixID #27 curr=" << dccam.GetCurrent(i) << endl;
+	    //dcevt.AddPixel(i,dccam.GetCurrent(i)/geomcam.GetPixRatio(i),0);
+	    if( (count%NSteps) == 0 )
+	    {
+		dcevt.AddPixel(i,val[i]/(Double_t)NSteps,0);
+		dcevt.GetPixelContent(val[i],i,geomcam,0);
+		//cout << "Pixel #" << i << "  current=" << val[i] << endl;
+		val[i] = 0.0;
+	    }
+	}
+
+	// Reset count and call findstar function on this macroevent
+	if( (count%NSteps) == 0 )
+	{
+	    count = 0;
+	    ++NumAverEvt;
+	    cout << "Calling test_findstar function" << endl;
+	    test_findstar(&dcevt);
+	}
+    }
+
+    // Last loop overall pixels to fill with the average 
+    // of last count events
+    if(count!=0)
+    {
+	for(Int_t i=0; i<gsNpix; i++)
+	{
+	    dcevt.AddPixel(i,val[i]/(Double_t)count,0);
+	    dcevt.GetPixelContent(val[i],i,geomcam,0);
+	    //cout << "Pixel #" << i << "  current=" << val[i] << endl;
+	}
+    }
+
+    cout << "Calling test_findstar function" << endl;
+    test_findstar(&dcevt);
+
+    cout << "Number of averaged events stored into container = " << NumAverEvt << endl;
+    tlist.PrintStatistics();
+    
 }
 
