Index: trunk/MagicSoft/Mars/mtemp/mifae/Changelog
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/Changelog	(revision 4071)
+++ trunk/MagicSoft/Mars/mtemp/mifae/Changelog	(revision 4072)
@@ -18,4 +18,17 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2004/05/14  Javier Rico
+   * library/MSrcPlace.[cc,h]
+     - added
+
+   * library/MSrcPosFromFile.[cc,h], library/MSrcRotate.[cc,h]
+     - inherit from MSrcPlace
+
+   * programs/srcPos.cc programs/srcpos.datacard
+     - adapt to new MSrcPlace class
+	
+   * library/Makefile, library/IFAELinkDef.h
+     - include MSrcPlace
 
  2004/05/13  Javier Rico
Index: trunk/MagicSoft/Mars/mtemp/mifae/library/IFAELinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/library/IFAELinkDef.h	(revision 4071)
+++ trunk/MagicSoft/Mars/mtemp/mifae/library/IFAELinkDef.h	(revision 4072)
@@ -8,4 +8,5 @@
 #pragma link C++ class MPSFFit+;
 #pragma link C++ class MPSFFitCalc+;
+#pragma link C++ class MSrcPlace+;
 #pragma link C++ class MSrcPosFromFile+;
 #pragma link C++ class MSrcRotate+;
Index: trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPlace.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPlace.cc	(revision 4072)
+++ trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPlace.cc	(revision 4072)
@@ -0,0 +1,192 @@
+/* ======================================================================== *\
+!
+! *
+! * This file is part of MARS, the MAGIC Analysis and Reconstruction
+! * Software. It is distributed to you in the hope that it can be a useful
+! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
+! * It is distributed WITHOUT ANY WARRANTY.
+! *
+! * Permission to use, copy, modify and distribute this software and its
+! * documentation for any purpose is hereby granted without fee,
+! * provided that the above copyright notice appear in all copies and
+! * that both that copyright notice and this permission notice appear
+! * in supporting documentation. It is provided "as is" without express
+! * or implied warranty.
+! *
+!
+!   Author(s): Javier Lopez    04/2004 <mailto:jlopez@ifae.es>
+!   Author(s): Javier Rico     04/2004 <mailto:jrico@ifae.es>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// MSrcPlace
+//
+// Abstract task to set the source position in any place in the camera
+// It keeps a 2D histogram with the assigned positions, so that the same
+// distribution can be applied later to any data set (tipically the OFF data)
+//
+//  Input Containers:
+//    MSrcPosCam
+//
+//  Output Containers:
+//    MSrcPosCam
+//    MDCA
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include <fstream>
+#include <math.h>
+
+#include "TH2F.h"
+
+#include "MParList.h"
+#include "MSrcPlace.h"
+#include "MSrcPosCam.h"
+#include "MDCA.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+ClassImp(MSrcPlace);
+
+using namespace std;
+
+static const TString gsDefName  = "MSrcPlace";
+static const TString gsDefTitle = "Set the position of the source";
+
+// -------------------------------------------------------------------------
+//
+// Default constructor. The first (second) argument is the name of a container
+// containing the source position in the camera plain, MScrPosCam (MDCA).
+// The default is "MSrcPosCam" ("MDCA"). 
+//
+MSrcPlace::MSrcPlace(const char* srcPos, const char* dca, const char *name, const char *title)
+  : fSrcPos(NULL), fDCA(NULL)
+{
+    fName  = name  ? name  : gsDefName.Data();
+    fTitle = title ? title : gsDefTitle.Data();
+
+    fSrcPosName  = srcPos;
+    fDCAName     = dca;
+
+    const Float_t cameraSize   = 600; //[mm]
+    const Float_t binPrecision =   1; //[mm] ~ 0.0033 deg
+
+    const UInt_t nbins =  (UInt_t)(cameraSize*2/binPrecision);
+    fHistSrcPos = new TH2F("HistSrcPos","",nbins,-cameraSize,cameraSize,nbins,-cameraSize,cameraSize);
+
+    fMode=kOn;
+}
+// -------------------------------------------------------------------------
+//
+// Destructor
+//
+MSrcPlace::~MSrcPlace()
+{
+  delete fHistSrcPos;
+}
+// -------------------------------------------------------------------------
+//
+// Look for/create the needed containers
+// Check whether RA and DEC have been initialized
+//
+Int_t MSrcPlace::SearchForSrcPos(MParList *pList)
+{
+    // look for/create MSrcPosCam
+  fSrcPos = (MSrcPosCam*)pList->FindObject(AddSerialNumber(fSrcPosName), "MSrcPosCam");
+  if (!fSrcPos)
+    {
+      *fLog << warn << AddSerialNumber(fSrcPosName) << " [MSrcPosCam] not found... creating default container." << endl;
+      fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam", AddSerialNumber(fSrcPosName));
+      if(!fSrcPos)
+	return kFALSE;
+    }
+
+  // look for/create MDCA
+  fDCA = (MDCA*)pList->FindObject(AddSerialNumber(fDCAName), "MDCA");
+  if (!fDCA)
+    {
+      *fLog << warn << AddSerialNumber(fDCAName) << " [MDCA] not found... creating default container." << endl;
+      fDCA = (MDCA*)pList->FindCreateObj("MDCA", AddSerialNumber(fDCAName));
+      if(!fDCA)
+	return kFALSE;
+    }
+
+  return kTRUE;
+}
+
+// -------------------------------------------------------------------------
+//
+// Save the position of the source in the histogram
+//
+void MSrcPlace::SavePosIntoHisto()
+{  
+  fHistSrcPos->Fill(fSrcPos->GetX(),fSrcPos->GetY());
+}
+// -------------------------------------------------------------------------
+//
+// Read the position of the source from the histogram
+//
+void MSrcPlace::ReadPosFromHisto()
+{  
+  Axis_t x;
+  Axis_t y;
+  
+  fHistSrcPos->GetRandom2(x,y);
+  fSrcPos->SetXY(x,y);
+  fDCA->SetRefPoint(x,y);
+}
+// -------------------------------------------------------------------------
+//
+// Look for needed containers
+//
+Int_t MSrcPlace::PreProcess(MParList* plist)
+{  
+  return SearchForSrcPos(plist);
+}
+
+// -------------------------------------------------------------------------
+//
+// Call to compute a new position and then save it in the histogram (fMode==kOn) 
+// of to read the new position from the histogram (fMode==kOff)
+//
+Int_t MSrcPlace::Process()
+{  
+  switch(fMode)
+    {
+    case kOn:
+      if(!ComputeNewSrcPosition())
+	return kFALSE;
+      SavePosIntoHisto();
+      break;
+    case kOff:
+      ReadPosFromHisto();
+      break;
+    default:
+      *fLog << err << "MSrcPlace::Process Warning: Wrong mode " << fMode << endl;
+      return kFALSE;
+    }
+  return kTRUE;
+}
+
+// -------------------------------------------------------------------------
+//
+// Dump 2D histo statistics
+//
+Int_t MSrcPlace::PostProcess()
+{
+  *fLog << inf << endl;
+  *fLog << inf << "MSrcPlace::PostProcess Message: Created internal histogram with: ";
+  *fLog << inf << "Entries: " << fHistSrcPos->GetEntries() << endl;
+  *fLog << inf << "X projection mean: " << fHistSrcPos->ProjectionX()->GetMean() << endl;
+  *fLog << inf << "X projection rms:  " << fHistSrcPos->ProjectionX()->GetRMS() << endl;
+  *fLog << inf << "Y projection mean: " << fHistSrcPos->ProjectionY()->GetMean() << endl;
+  *fLog << inf << "Y projection rms:  " << fHistSrcPos->ProjectionY()->GetRMS() << endl;
+  
+  return kTRUE;
+}
Index: trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPlace.h
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPlace.h	(revision 4072)
+++ trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPlace.h	(revision 4072)
@@ -0,0 +1,58 @@
+#ifndef MARS_MSrcPlace
+#define MARS_MSrcPlace
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MDCA;
+class MSrcPosCam;
+class TH2F;
+
+class MSrcPlace : public MTask
+{
+ public:
+  enum OnOffMode_t {kOn=0,kOff};
+  
+ private:
+  MSrcPosCam*  fSrcPos;      //  Pointer to the source position
+  MDCA*        fDCA;         //  Pointer to the MDCA object
+  
+  TString      fSrcPosName;  //  Name of the MSrcPosCam object
+  TString      fDCAName;     //  Name of the MDCA object
+  
+  TH2F*        fHistSrcPos;  //  histogram of the used source positions
+  OnOffMode_t  fMode;        //  On/Off data mode (write/read to/from the histogram)
+
+  virtual Int_t PreProcess(MParList *plist);
+  virtual Int_t Process();
+  virtual Int_t PostProcess();
+
+  void  SavePosIntoHisto();
+  void  ReadPosFromHisto();
+
+
+ public:
+  MSrcPlace(const char* src="MSrcPosCam", const char* dca="MDCA",
+	    const char* name=NULL, const char* title=NULL);
+
+  virtual ~MSrcPlace();
+
+  void SetMode(OnOffMode_t mode)   {fMode=mode;}
+  void SetSrcPosName(TString name) {fSrcPosName=name;}
+  void SetDCAName(TString name)    {fDCAName=name;}
+
+  OnOffMode_t GetMode()            {return fMode;}
+  TH2F*       GetPositionHisto()   {return fHistSrcPos;}
+  MSrcPosCam* GetSrcPosCam()       {return fSrcPos;}
+  MDCA*       GetDCA()             {return fDCA;}
+
+
+  Int_t SearchForSrcPos(MParList *plist);
+  virtual Int_t ComputeNewSrcPosition() {return kTRUE;}
+
+  ClassDef(MSrcPlace, 0) // task to rotate the position of the source as a function of Azimuth and Zenith angles
+};
+
+#endif
+
Index: trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPosFromFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPosFromFile.cc	(revision 4071)
+++ trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPosFromFile.cc	(revision 4072)
@@ -27,5 +27,6 @@
 // MSrcPosFromFile
 //
-// Task to calculate the position of the source as a function of run number
+// Task to set the position of the source as a function of run number according
+// to the positions read from an input file
 //
 //  Output Containers:
@@ -39,5 +40,4 @@
 
 #include "MParList.h"
-
 #include "MSrcPosFromFile.h"
 
@@ -53,42 +53,37 @@
 
 static const TString gsDefName  = "MSrcPosFromFile";
-static const TString gsDefTitle = "Calculate position of the (off axis) source";
+static const TString gsDefTitle = "Set the position of the (off axis) source according to input file";
 
 // -------------------------------------------------------------------------
 //
-// Default constructor
+// Default constructor. cardpath is the name of the input file (.pos) where the
+// source positions are stored in the format Run# x y (in mm). mode indicates whether
+// to read or to save the positions of the source in/from the internal histogram
 //
 MSrcPosFromFile::MSrcPosFromFile(TString cardpath, OnOffMode_t mode, const char *name, const char *title)
-    : fRawRunHeader(NULL), fSrcPos(NULL), fMode(mode), fSourcePositionFilePath(cardpath)
-{
-    fName  = name  ? name  : gsDefName.Data();
-    fTitle = title ? title : gsDefTitle.Data();
-
-    fLastRun = 0;
-
-// Count the number of runs in the card with the source poistions
-    ReadSourcePositionsFile(kCount);
-
-    fRunList = new Int_t[fNumRuns];
-    fRunSrcPos = new MSrcPosCam[fNumRuns];
-    fRunMap = new TExMap(fNumRuns);
-
-    Float_t cameraSize   = 600; //[mm]
-    Float_t binPrecision =  3;  //[mm] ~ 0.01 deg
-
-    UInt_t nbins =  (UInt_t)(cameraSize*2/binPrecision);
-
-    fHistSrcPos = new TH2F("HistSrcPos","",nbins,-cameraSize,cameraSize,nbins,-cameraSize,cameraSize);
-
-// Read card with the source poistions
-    ReadSourcePositionsFile(kRead);
+  : fRawRunHeader(NULL), fSourcePositionFilePath(cardpath)
+{
+  fName  = name  ? name  : gsDefName.Data();
+  fTitle = title ? title : gsDefTitle.Data();
+  SetMode(mode);
+
+  fLastRun = 0;
+  
+  // Count the number of runs in the card with the source poistions
+  ReadSourcePositionsFile(kCount);
+  
+  fRunList = new Int_t[fNumRuns];
+  fRunSrcPos = new MSrcPosCam[fNumRuns];
+  fRunMap = new TExMap(fNumRuns);
+  
+  // Read card with the source poistions
+  ReadSourcePositionsFile(kRead);
 }
 
 MSrcPosFromFile::~MSrcPosFromFile()
 {
-    delete [] fRunList;
-    delete [] fRunSrcPos;
-    delete fRunMap;
-    delete fHistSrcPos;
+  delete [] fRunList;
+  delete [] fRunSrcPos;
+  delete fRunMap;
 }
 
@@ -97,24 +92,53 @@
 Int_t MSrcPosFromFile::PreProcess(MParList *pList)
 {
-
-    fRawRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
-    if (!fRawRunHeader)
+  if(!SearchForSrcPos(pList))
+    return kFALSE;
+  
+  fRawRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
+  if (!fRawRunHeader)
     {
       *fLog << err << AddSerialNumber("MRawRunHeader") << " not found ... aborting" << endl;
-        return kFALSE;
-    }
-
-    fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam");
-    if (!fSrcPos)
-	return kFALSE;
-
-    return kTRUE;
+      return kFALSE;
+    }
+  
+  return kTRUE;
 }
 
 // --------------------------------------------------------------------------
 //
+// In case we enter a new run, look for the position in the read list
+// If there is no value for that run, take the previous one
 // 
-Bool_t MSrcPosFromFile::ReInit(MParList *pList)
-{
+Int_t MSrcPosFromFile::ComputeNewSrcPosition()
+{
+  const UInt_t run = fRawRunHeader->GetRunNumber();
+  if(run!=fLastRun)
+    {      
+      fLastRun=run;
+      MSrcPosCam* srcpos = (MSrcPosCam*)fRunMap->GetValue(run);
+      
+      Float_t x;
+      Float_t y;
+	
+      if (srcpos)
+	{
+	  x = srcpos->GetX();
+	  y = srcpos->GetY();
+	  
+	  GetSrcPosCam()->SetXY(x,y);
+		
+	  *fLog << inf << "Source position for run " << run;
+	  *fLog << inf << "\tX\t" << setprecision(3) << x;
+	  *fLog << inf << "\tY\t" << setprecision(3) << y << endl;
+	}	    
+      else
+	{
+	  *fLog << inf << "Source position for run " << run << " not found in file. ";
+	  *fLog << inf << "Taking previous position: ";
+	  *fLog << inf << "\tX\t" << setprecision(3) << x;
+	  *fLog << inf << "\tY\t" << setprecision(3) << y << endl;	  
+	}
+    }
+  
   return kTRUE;
 }
@@ -122,127 +146,57 @@
 // --------------------------------------------------------------------------
 //
+// Read file with table of source positions as function of run
 // 
-Int_t MSrcPosFromFile::Process()
-{
-
-    switch(fMode)
+Int_t MSrcPosFromFile::ReadSourcePositionsFile(UShort_t readmode)
+{
+  
+  ifstream fin(fSourcePositionFilePath);
+  if(!fin)
+    {
+      *fLog << err << "MSrcPosFromFile::ReadSourcePositionsFile. Cannot open file " << fSourcePositionFilePath << endl;
+      return kFALSE;
+    }
+  
+  UInt_t run;
+  Float_t x,y;
+  
+  fNumRuns=0;
+  
+  *fLog << dbg << "MSrcPosFromFile::ReadSourcePositionsFile(" << readmode << ')' << endl;
+  while(1)
+    {
+      fin >> run >> x >> y;
+      if(fin.eof())
+	break;
+      
+      switch(readmode)
 	{
-	    case kOn:
-	    {
-		const UInt_t run = fRawRunHeader->GetRunNumber();
-
-		MSrcPosCam* srcpos = (MSrcPosCam*)fRunMap->GetValue(run);
-		
-		Float_t x;
-		Float_t y;
-		
-		if (srcpos)
-		{
-		    x = srcpos->GetX();
-		    y = srcpos->GetY();
-		    
-		    
-		    if (srcpos && run != fLastRun)
-		    {
-			fSrcPos->SetXY(x,y);
+	case kCount:
+	  {
 	    
-			*fLog << inf << "Source position for run " << run;
-			*fLog << inf << "\tX\t" << setprecision(2) << x;
-			*fLog << inf << "\tY\t" << setprecision(2) << y << endl;
-		    }
-		    
-		    fLastRun = run;
-		}
-		
-		x = fSrcPos->GetX();
-		y = fSrcPos->GetY();
-		
-		fHistSrcPos->Fill(x,y);
-		break;
-	    }
-	    case kOff:
-	    {
-		Axis_t x;
-		Axis_t y;
-
-		fHistSrcPos->GetRandom2(x,y);
-		fSrcPos->SetXY(x,y);
-
-		break;
-	    }
-	    default:
-		*fLog << err << "Wrond mode " << fMode << endl;
-		return kFALSE;
+	    *fLog << dbg << "Source position for run " << run;
+	    *fLog << dbg << "\tX\t" << x << " mm";
+	    *fLog << dbg << "\tY\t" << y << " mm" << endl;
+	    
+	    fNumRuns++;
+	    break;
+	  }
+	case kRead:
+	  {
+	    fRunList[fNumRuns] = run;
+	    fRunSrcPos[fNumRuns].SetXY(x,y);
+	    fRunMap->Add(fRunList[fNumRuns],(Long_t)&(fRunSrcPos[fNumRuns]));
+	    fNumRuns++;
+	    break;
+	  }
+	default:
+	  *fLog << err << "Read mode " << readmode << " node defined" << endl;
+	  return kFALSE;
 	}
-
-    return kTRUE;
-}
-
-
-Int_t MSrcPosFromFile::PostProcess()
-{
-
-    *fLog << dbg << endl;
-    *fLog << dbg << "fHistSrcPos->GetEntries() " << fHistSrcPos->GetEntries() << endl;
-    *fLog << dbg << "fHistSrcPos->ProjectionX()->GetMean() " << fHistSrcPos->ProjectionX()->GetMean() << endl;
-    *fLog << dbg << "fHistSrcPos->ProjectionX()->GetRMS() " << fHistSrcPos->ProjectionX()->GetRMS() << endl;
-    *fLog << dbg << "fHistSrcPos->ProjectionY()->GetMean() " << fHistSrcPos->ProjectionY()->GetMean() << endl;
-    *fLog << dbg << "fHistSrcPos->ProjectionY()->GetRMS() " << fHistSrcPos->ProjectionY()->GetRMS() << endl;
-
-    return kTRUE;
-}
-
-
-Int_t MSrcPosFromFile::ReadSourcePositionsFile(UShort_t readmode)
-{
-
-    ifstream fin(fSourcePositionFilePath);
-    if(!fin)
-    {
-	*fLog << err << "MSrcPosFromFile::ReadSourcePositionsFile. Cannot open file " << fSourcePositionFilePath << endl;
-	return kFALSE;
-    }
-
-    UInt_t run;
-    Float_t x,y;
-
-    fNumRuns=0;
-
-    *fLog << dbg << "MSrcPosFromFile::ReadSourcePositionsFile(" << readmode << ')' << endl;
-    while(1)
-    {
-	fin >> run >> x >> y;
-	if(fin.eof())
-	    break;
-
-	switch(readmode)
-	{
-	    case kCount:
-	    {
-
-		*fLog << dbg << "Source position for run " << run;
-		*fLog << dbg << "\tX\t" << x << " mm";
-		*fLog << dbg << "\tY\t" << y << " mm" << endl;
-
-		fNumRuns++;
-		break;
-	    }
-	    case kRead:
-	    {
-		fRunList[fNumRuns] = run;
-		fRunSrcPos[fNumRuns].SetXY(x,y);
-		fRunMap->Add(fRunList[fNumRuns],(Long_t)&(fRunSrcPos[fNumRuns]));
-		fNumRuns++;
-		break;
-	    }
-	    default:
-		*fLog << err << "Read mode " << readmode << " node defined" << endl;
-		return kFALSE;
-	}
-    }
-
-    fin.close();
-
-
-    return kTRUE;
-}
+    }
+  
+  fin.close();
+  
+  
+  return kTRUE;
+}
Index: trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPosFromFile.h
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPosFromFile.h	(revision 4071)
+++ trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcPosFromFile.h	(revision 4072)
@@ -2,6 +2,6 @@
 #define MARS_MSrcPosFromFile
 
-#ifndef MARS_MTask
-#include "MTask.h"
+#ifndef MARS_MSrcPlace
+#include "MSrcPlace.h"
 #endif
 
@@ -10,15 +10,12 @@
 #endif
 
-class TH2F;
-
 class MRawRunHeader;
 class MSrcPosCam;
 
-class MSrcPosFromFile : public MTask
+class MSrcPosFromFile : public MSrcPlace
 {
  private:
 
     MRawRunHeader *fRawRunHeader;
-    MSrcPosCam    *fSrcPos;   //  Pointer to the source position
 
     Int_t  fNumRuns;
@@ -29,24 +26,16 @@
     TExMap     *fRunMap;   // list of run numbers positions
 
-    TH2F    *fHistSrcPos;
-    UShort_t fMode;
+    TString fSourcePositionFilePath;
 
-    TString fSourcePositionFilePath;
-    Int_t ReadSourcePositionsFile(UShort_t readmode);
-
-    Int_t PreProcess(MParList *plist);
-    Bool_t ReInit(MParList *plist);
-    Int_t Process();
-    Int_t PostProcess();
-
-public:
+    virtual Int_t ReadSourcePositionsFile(UShort_t readmode);
+    virtual Int_t ComputeNewSrcPosition();
     
+    virtual Int_t PreProcess(MParList *plist);
+    
+public:    
     enum ReadMode_t {kCount=0,kRead};
-    enum OnOffMode_t {kOn=0,kOff};
 
     MSrcPosFromFile(TString cardpath, OnOffMode_t mode=kOn, const char *name=NULL, const char *title=NULL);
     ~MSrcPosFromFile();
-    
-    void SetMode(OnOffMode_t mode) {fMode=mode;}
 
     ClassDef(MSrcPosFromFile, 0) // task to calculate the position of the source as a function of the run number 
Index: trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcRotate.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcRotate.cc	(revision 4071)
+++ trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcRotate.cc	(revision 4072)
@@ -26,6 +26,5 @@
 // MSrcRotate
 //
-// Task to rotate the position of the source as a function of Azimuth and 
-// Zenith angles
+// Task to rotate the position of the source as a function of time
 //
 //  Input Containers:
@@ -45,9 +44,9 @@
 #include "MRawEvtHeader.h"
 #include "MSrcRotate.h"
-#include "MSrcPosCam.h"
-#include "MDCA.h"
 
 #include "MAstroSky2Local.h"
 #include "MObservatory.h"
+#include "MSrcPosCam.h"
+#include "MDCA.h"
 
 #include "MLog.h"
@@ -59,5 +58,5 @@
 
 static const TString gsDefName  = "MSrcRotate";
-static const TString gsDefTitle = "De-rotate position of the source";
+static const TString gsDefTitle = "Rotate position of the source";
 
 // -------------------------------------------------------------------------
@@ -68,11 +67,11 @@
 //
 MSrcRotate::MSrcRotate(const char* srcPos, const char* dca, const char *name, const char *title)
-  : fSrcPos(NULL), fDCA(NULL), fRA(0), fDEC(0), fRunNumber(0)
+  : fRA(0), fDEC(0), fRefMJD(0), fRunNumber(0)
 {
     fName  = name  ? name  : gsDefName.Data();
     fTitle = title ? title : gsDefTitle.Data();
 
-    fSrcPosName  = srcPos;
-    fDCAName     = dca;
+    SetSrcPosName(srcPos);
+    SetDCAName(dca);
 }
 
@@ -84,24 +83,7 @@
 Int_t MSrcRotate::PreProcess(MParList *pList)
 {
-    // look for/create MSrcPosCam
-  fSrcPos = (MSrcPosCam*)pList->FindObject(AddSerialNumber(fSrcPosName), "MSrcPosCam");
-  if (!fSrcPos)
-    {
-      *fLog << warn << AddSerialNumber(fSrcPosName) << " [MSrcPosCam] not found... creating default container." << endl;
-      fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam", AddSerialNumber(fSrcPosName));
-      if(!fSrcPos)
-	return kFALSE;
-    }
-
-  // look for/create MDCA
-  fDCA = (MDCA*)pList->FindObject(AddSerialNumber(fDCAName), "MDCA");
-  if (!fDCA)
-    {
-      *fLog << warn << AddSerialNumber(fDCAName) << " [MDCA] not found... creating default container." << endl;
-      fDCA = (MDCA*)pList->FindCreateObj("MDCA", AddSerialNumber(fDCAName));
-      if(!fDCA)
-	return kFALSE;
-    }
-
+  if(!SearchForSrcPos(pList))
+    return kFALSE;
+  
   // look for MRawRunHeader
   fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
@@ -141,10 +123,10 @@
 // FIXME: for the time being, this is computed by assuming constant event rate
 //
-Int_t MSrcRotate::Process()
+Int_t MSrcRotate::ComputeNewSrcPosition()
 {  
   if(fRunHeader->GetRunNumber()!=fRunNumber)
     {
       fRunNumber=fRunHeader->GetRunNumber();
-
+      
       // save the number of events, initial and final times
       fNEvts    = fRunHeader->GetNumEvents();
@@ -155,8 +137,8 @@
       if((ULong_t)TMath::Nint(fIniTime.GetMjd())!=(ULong_t)TMath::Nint(fFinTime.GetMjd()))
 	{
-	  *fLog << err << "MSrcRotate::Process Error: Inial and final MJDs are different ("<<fIniTime.GetMjd()<<"!="<<fFinTime.GetMjd()<<")" << endl;
+	  *fLog << err << "MSrcRotate::ComputeNewSrcPosition Error: Inial and final MJDs are different ("<<fIniTime.GetMjd()<<"!="<<fFinTime.GetMjd()<<")" << endl;
 	  return kFALSE;
 	}
-
+      
 #ifdef DEBUG
       cout << endl << "********************"  << endl;
@@ -169,5 +151,5 @@
     }
   
-
+  
   // Compute the event time
   // FIXME: for the time being, this is computed by assuming constant event rate
@@ -177,9 +159,9 @@
   MTime refTime;
   refTime.SetMjd(fRefMJD);
-
+  
   // de-rotate the source position
   const MAstroSky2Local Observation(eventTime, *fObservatory);
   const MAstroSky2Local RefObservation(refTime, *fObservatory);
-
+  
 #ifdef DEBUG
   printf("Run:%d, Event:%d, iniMJD=%15.5f, finMJD=%15.5f, fDeltaT=%15.5f, newMJD=%15.5f, fRefMJD=%15.5f, rotation=%15.5f, ref=%15.5f\n",
@@ -190,22 +172,26 @@
   //  cout << "newMJD=" << newMJD << ", fRefMJD="<<fRefMJD<<", rotation="<<Observation.RotationAngle(fRA,fDEC)<<", ref="<<RefObservation.RotationAngle(fRA,fDEC)<< endl;
 #endif
-
-  Double_t rotationAngle = Observation.RotationAngle(fRA,fDEC)-RefObservation.RotationAngle(fRA,fDEC);
+  
+  Double_t rotationAngle = fRefMJD ? 
+    Observation.RotationAngle(fRA,fDEC)-RefObservation.RotationAngle(fRA,fDEC) :
+    Observation.RotationAngle(fRA,fDEC) ;
+  MSrcPosCam* srcpos = GetSrcPosCam();
+  MDCA* dca = GetDCA();
   
   Float_t c = TMath::Cos(rotationAngle);
   Float_t s = TMath::Sin(rotationAngle);
   // perform a rotation of -rotationAngle to move the source back to the "initial" position
-  Float_t newX = c*fSrcPos->GetX()-s*fSrcPos->GetY();
-  Float_t newY = s*fSrcPos->GetX()+c*fSrcPos->GetY();
-
+  Float_t newX = c*srcpos->GetX()-s*srcpos->GetY();
+  Float_t newY = s*srcpos->GetX()+c*srcpos->GetY();
+  
 #ifdef DEBUG
   Double_t rotationAngleComp = fObservatory->RotationAngle(0.1256,2.63);
   cout << "Event " << fEvtHeader->GetDAQEvtNumber() << endl;
-  cout << "newMJD=" << newMJD <<", rotationAngle=" << rotationAngle <<", rotationAngleComp=" << rotationAngleComp << ", oldX="<<fIniSrcPos.GetX()<< ", oldY="<<fIniSrcPos.GetY()<< ", newX="<<newX<< ", newY="<<newY << endl,
+  cout << "newMJD=" << newMJD <<", rotationAngle=" << rotationAngle <<", rotationAngleComp=" << rotationAngleComp << ", oldX="<<fIniSrcPos.GetX()<< ", oldY="<<fIniSrcPos.GetY()<< ", newX="<<newX<< ", newY="<<newY << endl;
 #endif
-
-  fSrcPos->SetX(newX);
-  fSrcPos->SetY(newY);
-  fDCA->SetRefPoint(newX,newY);
+  
+  srcpos->SetX(newX);
+  srcpos->SetY(newY);
+  dca->SetRefPoint(newX,newY);
 
   return kTRUE;
Index: trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcRotate.h
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcRotate.h	(revision 4071)
+++ trunk/MagicSoft/Mars/mtemp/mifae/library/MSrcRotate.h	(revision 4072)
@@ -2,48 +2,41 @@
 #define MARS_MSrcRotate
 
-#ifndef MARS_MTask
-#include "MTask.h"
+#ifndef MARS_MSrcPlace
+#include "MSrcPlace.h"
 #endif
 
-#include <TArrayF.h>
 #include "MTime.h"
-#include "MSrcPosCam.h"
 
-class MDCA;
 class MObservatory;
 class MRawEvtHeader;
 class MRawRunHeader;
 
-class MSrcRotate : public MTask
+class MSrcRotate : public MSrcPlace
 {
-private:
-    MSrcPosCam*    fSrcPos;      //  Pointer to the source position
-    MDCA*          fDCA;         //  Pointer to the MDCA object
-    MRawEvtHeader* fEvtHeader;   //  Pointer to the event header
-    MRawRunHeader* fRunHeader;   //  Pointer to the run header
-    MObservatory*  fObservatory; //  Pointer to the MObservatory    
-
-    TString     fSrcPosName;
-    TString     fDCAName;
-
-    Double_t   fRA;          //  [rad] Right ascention 
-    Double_t   fDEC;         //  [rad] Declination
-    Double_t   fRefMJD;      //  [MJ date] reference time for rotation
-    UInt_t     fNEvts;       //  Number of events in file
-    MTime      fIniTime;     //  Run initial time 
-    MTime      fFinTime;     //  Run final time 
-    Double_t   fDeltaT;      //  DeltaT between two events
-    UInt_t     fRunNumber;   //  Current run number
-
-    Int_t PreProcess(MParList *plist);
-    Int_t Process();
-
-public:
-    MSrcRotate(const char* src="MSrcPosCam", const char* dca="MDCA",
-	       const char* name=NULL, const char* title=NULL);
-
-    void SetRAandDECandRefMJD(Double_t ra, Double_t dec, Double_t ref) {fRA=ra;fDEC=dec;fRefMJD=ref;}
-
-    ClassDef(MSrcRotate, 0) // task to rotate the position of the source as a function of Azimuth and Zenith angles
+ private:
+  MRawEvtHeader* fEvtHeader;   //  Pointer to the event header
+  MRawRunHeader* fRunHeader;   //  Pointer to the run header
+  MObservatory*  fObservatory; //  Pointer to the MObservatory    
+  
+  Double_t   fRA;          //  [rad] Right ascenssion 
+  Double_t   fDEC;         //  [rad] Declination
+  Double_t   fRefMJD;      //  [MJ date] reference time for rotation
+  UInt_t     fNEvts;       //  Number of events in file
+  MTime      fIniTime;     //  Run initial time 
+  MTime      fFinTime;     //  Run final time 
+  Double_t   fDeltaT;      //  DeltaT between two events
+  UInt_t     fRunNumber;   //  Current run number
+  
+  virtual Int_t PreProcess(MParList *plist);
+  
+ public:
+  MSrcRotate(const char* src="MSrcPosCam", const char* dca="MDCA",
+	     const char* name=NULL, const char* title=NULL);
+  
+  void SetRAandDECandRefMJD(Double_t ra, Double_t dec, Double_t ref=0) {fRA=ra;fDEC=dec;fRefMJD=ref;}
+  void SetRAandDEC(Double_t ra, Double_t dec){SetRAandDECandRefMJD(ra,dec);}
+  virtual Int_t ComputeNewSrcPosition();
+  
+  ClassDef(MSrcRotate, 0) // task to rotate the position of the source as a function of Azimuth and Zenith angles
 };
 
Index: trunk/MagicSoft/Mars/mtemp/mifae/library/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/library/Makefile	(revision 4071)
+++ trunk/MagicSoft/Mars/mtemp/mifae/library/Makefile	(revision 4072)
@@ -51,4 +51,5 @@
         MPSFFit.cc \
         MPSFFitCalc.cc \
+	MSrcPlace.cc \
         MSrcPosFromFile.cc \
 	MSrcRotate.cc \
Index: trunk/MagicSoft/Mars/mtemp/mifae/programs/srcPos.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/programs/srcPos.cc	(revision 4071)
+++ trunk/MagicSoft/Mars/mtemp/mifae/programs/srcPos.cc	(revision 4072)
@@ -29,4 +29,5 @@
 #include "MArgs.h"
 #include "MWriteRootFile.h"
+#include "MTime.h"
 
 using namespace std;
@@ -40,13 +41,16 @@
 
 TString  inputFile;
+TString  offFile;
 TString  outputFile;
+TString  offOutputFile;
 ULong_t  nmaxevents=999999999;
 Float_t  xsrcpos=0.;
 Float_t  ysrcpos=0.;
 Double_t mjdpos=53050;
-Bool_t   kRotate=1;
-Bool_t   kSrcPolicy=kTRUE;
+Bool_t   kRotate=0;
+Bool_t   kSrcPolicy=kFALSE;
 Double_t fRA= -1.;
 Double_t fDEC= -1.;
+TString  srcFile;
 
 //-----------------------------------------------------------------------------
@@ -55,7 +59,4 @@
 
 const TString defaultcard="srcpos.datacard";
-
-const Float_t alphamin = 0.;       // minimum value in alpha (degrees) 
-const Float_t alphamax = 90.;      // maximum value in alpha (degrees)
 const Float_t conver   = 189./0.6; // conversion factor degrees to mm
 
@@ -147,4 +148,33 @@
   tlist.PrintStatistics();
 
+  
+  // do off-data if input file was specified
+  if(!offFile.Length())
+    return;
+
+  MReadTree read2("Parameters", offFile);
+  read2.DisableAutoScheme();  
+  tlist.AddToListBefore(&read2, &read, "All");
+  tlist.RemoveFromList(&read);
+
+  MWriteRootFile write2(offOutputFile,"RECREATE");
+  write2.AddContainer("MHillas"       , "Parameters");
+  write2.AddContainer("MHillasSrc"    , "Parameters");
+  write2.AddContainer("MHillasExt"    , "Parameters");
+  write2.AddContainer("MNewImagePar"  , "Parameters");
+  write2.AddContainer("MRawEvtHeader" , "Parameters");
+  write2.AddContainer("MRawRunHeader" , "Parameters");
+  write2.AddContainer("MConcentration" , "Parameters");
+  write2.AddContainer("MSrcPosCam"     , "Parameters");
+  tlist.AddToListBefore(&write2,&write,"All");
+  tlist.RemoveFromList(&write);
+
+  srcrotate.SetMode(MSrcPlace::kOff);
+
+  if (!evtloop.Eventloop(nmaxevents))
+    return;  
+
+  tlist.PrintStatistics();
+
 }
 //-----------------------------------------------------------------------
@@ -182,4 +212,12 @@
 	}
 
+      // off-data file name
+      if(strcmp(word.Data(),"OFFFILES")==0)
+	{
+	  if(offFile.Length())
+	    cout << "readDataCards Warning: overriding off-data file name" << endl;
+	  ifun >> offFile;
+	}
+
       // output file name
       if(strcmp(word.Data(),"OUTFILE")==0)
@@ -190,4 +228,20 @@
 	}
 
+      // output file name (off data)
+      if(strcmp(word.Data(),"OFFOUTFILE")==0)
+	{
+	  if(offOutputFile.Length())
+	    cout << "readDataCards Warning: overriding output file name for off data" << endl;
+	  ifun >> offOutputFile;
+	}
+
+      // source position input file
+      if(strcmp(word.Data(),"SRCFILE")==0)
+	{
+	  if(srcFile.Length())
+	    cout << "readDataCards Warning: overriding source-position file name" << endl;
+	  ifun >> srcFile;
+	}
+
       // source position
       if(strcmp(word.Data(),"SRCPOS")==0)
@@ -227,4 +281,10 @@
     }
 
+  if(offFile.Length() && !offOutputFile.Length())
+    {
+      cout << "No output file name specified for off data" << endl;
+      return kFALSE;
+    }
+
   if(xsrcpos==0 && ysrcpos==0)
     {
@@ -232,4 +292,6 @@
       return kFALSE;
     }
+
+  MTime thetime(mjdpos);
 
   // Dump read values
@@ -239,12 +301,22 @@
   cout << "Maximum number of input events: " << nmaxevents << endl;
   cout << "Input file name(s): " << inputFile << endl;
+  if(offFile.Length())
+    cout << "Input file name(s) for off data: " << offFile << endl;
   cout << "Output file name: " << outputFile << endl;
-  cout << "Source position (degrees) X=" << xsrcpos << ", Y="<<ysrcpos;
-  if(kSrcPolicy)
-    cout << " (RELATIVE TO INITIAL SOURCE POSITION)" << endl;
+  if(offFile.Length())
+    cout << "Output file name for off data: " << offOutputFile << endl;
+  if(srcFile.Length())
+    cout << "Reading source position from file " << srcFile << endl;
   else
-    cout << " (ABSOLUTE POSITION IN THE CAMERA)" << endl;
-  cout << "De-rotation flag " << kRotate << endl;
-  cout << "Source celestial coordiantes (rad): RA = " << fRA << ", DEC = " << fDEC << endl;
+    {
+      cout << "Source position (degrees) X=" << xsrcpos << ", Y="<<ysrcpos;
+      if(kSrcPolicy)
+	cout << " (RELATIVE TO INITIAL SOURCE POSITION)";
+      else
+	cout << " (ABSOLUTE POSITION IN THE CAMERA)";
+      cout << ", at " << thetime.GetSqlDateTime() << endl;
+      cout << "De-rotation flag " << kRotate << endl;
+      cout << "Source celestial coordiantes (rad): RA = " << fRA << ", DEC = " << fDEC << endl;
+    }
   cout << "***********" << endl << endl;
 
Index: trunk/MagicSoft/Mars/mtemp/mifae/programs/srcpos.datacard
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mifae/programs/srcpos.datacard	(revision 4071)
+++ trunk/MagicSoft/Mars/mtemp/mifae/programs/srcpos.datacard	(revision 4072)
@@ -1,13 +1,19 @@
 
 // Maximun number of on and off events to be processed)
-NEVENTS 20000
+NEVENTS 99999999
 
 // Input file name pattern
-INPUTFILES   /mnt/users/jrico/magic/mars/Mars_Standard02/mtemp/mifae/hillas/mrk20040215OnNoCalB.root
+INPUTFILES /mnt/users/jrico/magic/mars/Mars_Standard02/mtemp/mifae/hillas/mrk20040215OnNoCalB.root
 
-// output file name
+// Specify optionally the name of OFF-data files you want to apply the same source position distribution to
+OFFFILES  /mnt/users/jrico/magic/mars/Mars_Standard02/mtemp/mifae/hillas/mrk20040215OffNoCal*.root
+
+// output file name (on data)
 OUTFILE  ./srcPosPrueba.root
 
-// X and Y position of the source in degrees for a MJ date (days) -important only in case of rotation
+// output file name (off data)
+OFFOUTFILE ./srcPosOffPrueba.root
+
+// X and Y position of the source (degrees) for a MJ date (days) -important only in case of rotation
 SRCPOS  0.21  0.176   53050.0773
 
@@ -24,2 +30,8 @@
 SRCCOORDS 2.899 0.667   // (Mrk 421)
 
+
+// File containing source position as a function of run number (overrides SRCPOS, SRCABS, ROTFLAG and SRCCOORDS values)
+// SRCFILE ./20040215_Mrk421.B.pos
+
+
+
