Index: trunk/MagicSoft/Mars/mtemp/mucm/classes/MDataSetIter.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mucm/classes/MDataSetIter.cc	(revision 6690)
+++ trunk/MagicSoft/Mars/mtemp/mucm/classes/MDataSetIter.cc	(revision 6691)
@@ -62,9 +62,10 @@
 
 #include "MRunIter.h"
+#include "MSequence.h"
 
 #include "TObjArray.h"
 
 #include <TSystem.h>
-
+#include <fstream>
 
 ClassImp(MDataSetIter);
@@ -79,11 +80,126 @@
 //
 MDataSetIter::MDataSetIter() 
-    :  fLastProcessedRun(0), fLog(&gLog), fDefCalRun(0)
+    : fInitialRun(-1), fEndRun(-1), fLastProcessedDataRun(0), fLastDataRun(0), fDefCalRun(0), fDefCalRunPath(""), fLog(&gLog)
 {
     fPedRuns = new MRunIter;
     fCalRuns = new MRunIter;
     fDataRuns = new MRunIter;
-}
-
+    fSequence = new MSequence;
+}
+
+
+// ---------------------------------------------------------------------------
+//
+//  Add all the files inside a given directory to the fFileList.
+//
+void MDataSetIter::AddToFileList(MDirIter& dir)
+{
+    TString name;
+   
+    while (!(name=dir.Next()).IsNull())
+     	fFileList.Add((TObject*)(new TNamed((const char*)name, "")));
+   
+
+    //
+    // Sort File List by name
+    //
+    fFileList.Sort();
+
+    //
+    // Find last Data runnumber of the filelist to be processed
+    //
+    FindLastDataRun();
+}
+
+
+// -------------------------------------------------------------------------
+//
+// Find last Data runnumber of the filelist to be processed
+//
+void MDataSetIter::FindLastDataRun()
+{
+
+    if(fFileList.GetLast()==-1)
+	return;
+
+
+    TIter next(&fFileList,kIterBackward);
+    TNamed* n;
+
+    while ( (n=(TNamed*)next()) )
+    {  
+	TString name, path, date, src;
+	Int_t run;
+	char type;
+	
+	ScanFileName(n->GetName(), name, path, date, src, &run, &type);
+
+	if(type!='D')
+	    continue;
+
+	
+	if(fEndRun==-1)
+	{
+	    if(run>fLastDataRun)
+		fLastDataRun = run;
+	    
+	    break;
+	}
+	else
+	{
+	    if(run<=fEndRun)
+	    {
+		if(run>fLastDataRun)
+		    fLastDataRun = run;
+		
+		break;
+	    }
+	    
+	}
+    }
+
+}
+
+
+// ----------------------------------------------------------------------------
+//
+//  Overload MDirIter::AddDirectory
+//
+//  Each time a new directory is added, all the files inside that directory
+//  are added to the fFileList.
+//
+//  Returns the number of directories added
+//
+Int_t MDataSetIter::AddDirectory(const char *dir, const char *filter, Int_t recursive) 
+{
+    const Int_t rc = MDirIter::AddDirectory(dir, filter, recursive);
+    if (rc==0)
+	return 0;
+  
+    //
+    // Add the files inside that directory to the fFileList. In order to 
+    // add only the files inside the new directory added, we create
+    // and MDirIter with only that directory. Otherwise, it we call 
+    // directly this.Next() inside FillFileList the new files and the 
+    // previously existing one will be again adde to the fFileList. 
+    //
+    MDirIter next(dir,filter,recursive);
+    AddToFileList(next);
+
+    next.Print() ;
+    
+    return kTRUE;
+} 
+
+
+// ----------------------------------------------------------------------------
+//
+//  Add a source name to the list of selected source names.
+//
+void MDataSetIter::SelectSourceName(const char *src)
+{
+    fSrcList.Add((TObject*)(new TNamed(src, src)));  
+}
+    
 
 // ----------------------------------------------------------------------------
@@ -112,4 +228,15 @@
 	return kFALSE;
     }
+
+    //
+    // Skip Green and Blue calib
+    //
+    if(src.Contains("Blue",TString::kIgnoreCase) || src.Contains("Green",TString::kIgnoreCase))
+    {
+        *fLog << warn << "Skipping Blue/Green run [" << src << "]" << endl;
+        return kFALSE;
+    }
+
+
 
     //
@@ -124,6 +251,6 @@
     if(src.Contains("-"))
     {
-	*fLog << warn << "For source [" << src << "] ignoring suffix [" 
-	      << src( src.First("-")+1, src.Length() ) << "]" << endl;
+	//*fLog << warn << "For source [" << src << "] ignoring suffix [" 
+	//     << src( src.First("-")+1, src.Length() ) << "]" << endl;
 	src.Remove(src.First("-"));
     }
@@ -141,6 +268,6 @@
 	ValidSrc = n->GetName();
 	
-	//if (src==ValidSrc)       // For dealing with new calib files as Mrk421blue5
-	if (src.Contains(ValidSrc))
+	if (src==ValidSrc)       // For dealing with new calib files as Mrk421blue5
+	//if (src.Contains(ValidSrc))
 	    return kTRUE;
     }
@@ -151,4 +278,27 @@
 }
 
+// -------------------------------------------------------------------
+//	
+// Compare two source names
+//
+Int_t MDataSetIter::CompareSourceNames(TString& src1, TString& src2)
+{
+    if(src1.Contains("-"))
+    {
+        //*fLog << warn << "For source [" << src << "] ignoring suffix ["
+        //     << src( src.First("-")+1, src.Length() ) << "]" << endl;
+        src1.Remove(src1.First("-"));
+    }
+
+
+    if(src2.Contains("-"))
+    {
+        //*fLog << warn << "For source [" << src << "] ignoring suffix ["
+        //     << src( src.First("-")+1, src.Length() ) << "]" << endl;
+        src2.Remove(src2.First("-"));
+    }
+
+    return (src1.CompareTo(src2)==0) ? kTRUE : kFALSE;
+}
 
 // ---------------------------------------------------------------------------
@@ -163,78 +313,35 @@
     path = file(0,slash+1);
     name = file(slash+1,file.Length());
-    
+  
+    TString tmp = name;
+    
+
     // Get date
-    date = name(0,8);
-    
+    date = tmp(0,tmp.First('_'));
+    tmp = tmp.Remove(0,tmp.First('_')+1);
+
+ 
     // Get run number
-    const TString runstr = name(9,5);
+    const TString runstr = tmp(0,tmp.First('_')); 
     *run = atoi(runstr.Data());
-    
+    tmp = tmp.Remove(0,tmp.First('_')+1);
+
+
     // Get run type
-    *type = name[15];
+    *type = tmp[0];
+    tmp = tmp.Remove(0,tmp.First('_')+1);
+  
 
     // Get source name
-    src = name(17,name.Last('_')-17);
-}
-
-
-// ---------------------------------------------------------------------------
-//
-//  Add all the files inside a given directory to the fFileList.
-//
-void MDataSetIter::AddToFileList(MDirIter& dir)
-{
-   
-    TString name;
-    while (!(name=dir.Next()).IsNull())
-    {
-	fFileList.Add((TObject*)(new TNamed((const char*)name, "")));  
-    }
-
-    //
-    // Sort File List by name
-    //
-    fFileList.Sort();
-}
-
-
-// ----------------------------------------------------------------------------
-//
-//  Overload MDirIter::AddDirectory
-//
-//  Each time a new directory is added, all the files inside that directory
-//  are added to the fFileList.
-//
-//  Returns the number of directories added
-//
-Int_t MDataSetIter::AddDirectory(const char *dir, const char *filter, Int_t recursive) 
-{
-    const Int_t rc = MDirIter::AddDirectory(dir, filter, recursive);
-    if (rc==0)
-	return 0;
-  
-    //
-    // Add the files inside that directory to the fFileList. In order to 
-    // add only the files inside the new directory added, we create
-    // and MDirIter with only that directory. Otherwise, it we call 
-    // directly this.Next() inside FillFileList the new files and the 
-    // previously existing one will be again adde to the fFileList. 
-    //
-    MDirIter next(dir,filter,recursive);
-    AddToFileList(next);
-    
-    return kTRUE;
-} 
-
-
-// ----------------------------------------------------------------------------
-//
-//  Add a source name to the list of selected source names.
-//
-void MDataSetIter::SelectSourceName(const char *src)
-{
-    fSrcList.Add((TObject*)(new TNamed(src, src)));  
-}
-    
+    //src = tmp(0,tmp.First('_')); 
+    //
+    // To deal with  4C15 with filemames doesn't include '_E' (18-11-2004) 
+    //
+    if (tmp.Contains('_'))
+	src = tmp(0,tmp.First('_'));
+    else	
+	src = tmp(0,tmp.First('.'));
+}
+
 
 // ----------------------------------------------------------------------------
@@ -248,5 +355,5 @@
     {
 	*fLog << warn << "Doesn't exist previous set." << endl;
-       return kFALSE;
+	return kFALSE;
     }
     
@@ -264,21 +371,17 @@
     
     ScanFileName(oldRuns.Next(), name, path, date, src, &run, &type);
-    
-   //   if( src != fSrcName)
-//      {
-//  	*fLog << err << "Previous runs were for a diffrent source...exit." << endl;
-//  	return kFALSE;
-//      }
-
-	//
-	// For dealing with calibration color we just ask than the srcname 
-	// contains the FirstSrcName
-	//
-	if( !src.Contains(fSrcName) )
-	{
-	    *fLog << warn << "Previous runs were for a diffrent source [" << src << "] vs [" << fSrcName << "] ...exit." << endl;
-	    return kFALSE;
-	}
-
+ 
+    
+    if ( !CompareSourceNames(src,fSrcName) )
+    //
+    // For dealing with calibration color we just ask than the srcname 
+    // contains the FirstSrcName
+    //
+    //if( !src.Contains(fSrcName) )
+    {
+	*fLog << warn << "Previous runs were for a diffrent source [" << src << "] vs [" << fSrcName << "] ...exit." << endl;
+	return kFALSE;
+    }
+    
     
     return kTRUE;
@@ -300,5 +403,5 @@
 // 
 // Process all the runs which runnumber is later to the last processed run 
-//  (the first time this function is call fLastProcessedRun is 0, so it start
+//  (the first time this function is call fLastProcessedDataRun is 0, so it start
 //  processing from the beginning)
 //
@@ -310,5 +413,8 @@
    
     if(fFileList.GetLast()==-1)
+    {
+	*fLog << warn << "File list empty." << endl;
 	return kFALSE;    
+    }
 
 
@@ -320,5 +426,4 @@
     MRunIter* oldCalRuns = 0;
     
-
     if(fDataRuns)
       oldDataRuns = (MRunIter*)fDataRuns->Clone();
@@ -340,5 +445,5 @@
     // the directory. Then Return kFALSE
     //
-    if ( fDataRuns->GetNumRuns()==0 ) 
+    if ( fDataRuns->GetNumRuns()==0 )
     {
 	*fLog << warn << "No more Data runs left." << endl;
@@ -470,5 +575,5 @@
     // Print the runs of this data set
     //
-    Print();
+    //Print();
 
 
@@ -478,4 +583,71 @@
     
 
+    return kTRUE;
+}
+
+
+// -----------------------------------------------------------------------
+//
+//  Write an ASCII file with the sequence info. 
+//  The name of the file is 'sequenceRunNum.txt' where 
+//  RunNum is the run number of the first data file to analyse. We use 
+//  the data runnumber instead of calib run because several sequences can
+//  have the same calib run (just the P can change)
+//
+Int_t MDataSetIter::WriteSequence()
+{
+    //
+    // Sequence
+    //
+    //fSequence->SetupDatRuns(fDataRuns, fPathData, "D");
+    //fSequence->SetupPedRuns(fPedRuns, fPathData, "P");
+    //fSequence->SetupCalRuns(fCalRuns, fPathData, "C");
+    
+    //fSequence->Print();
+
+    TString sped =  fPedRuns->GetRunsAsFileName();
+    TString scal =  fCalRuns->GetRunsAsFileName();
+    TString sdata = fDataRuns->GetRunsAsFileName();
+
+    sped.ReplaceAll("_"," ");
+    scal.ReplaceAll("_"," ");
+    sdata.ReplaceAll("_"," ");
+
+   
+    Int_t seqnum;
+    //sscanf(scal.Data(),"%d",&seqnum);
+    sscanf(sdata.Data(),"%d",&seqnum);
+
+    TString fname = Form("sequence_%s_%d.txt",fDate.Data(), seqnum);
+    ofstream out(fname);
+
+
+    TString date = fDate;
+    date.Insert(4,"-");
+    date.Insert(7,"-");
+
+
+    //out << "# Sequence number (identifier)" <<endl;
+    out << "Sequence: " << seqnum << endl;
+    //out << endl;
+    //out << "# date of sunrise of the observation night" << endl;
+    //out << "Night: 2004-09-21" << endl;
+    out << "Night: " << date << endl;
+    out << endl;
+    //out << "# Source name of all runs of sequence" << endl;
+    //out << "Source: " << fSrcName << endl;
+    //out << endl;
+    //out << "# List of all runs of this sequence" << endl;
+    //out << "Runs: " << scal << " " << sped << " "<< sdata << endl;  
+    //out << endl;
+    //out << "# List of all calibration runs of this sequence" << endl;
+    out << "CalRuns: " << scal << endl;
+    //out << "# List of all pedestal runs of this sequence" << endl;
+    out << "PedRuns: " << sped << endl;
+    //out << "# List of all data runs of this sequence" << endl;
+    out << "DatRuns: "<<  sdata << endl;
+    out.close();
+
+    
     return kTRUE;
 }
@@ -520,11 +692,14 @@
     fCalRuns = new MRunIter();
 
-
-    //
-    TString FirstSrcName = "";       // SrcName of the first run found
+    //
+    // Init
+    //
+    TString FirstSrcName = "";     // SrcName of the first run found
     Bool_t RunFound     = kFALSE; 
     Bool_t DataRunFound = kFALSE; 
     Bool_t CalRunFound  = kFALSE;
     Bool_t PedRunFound  = kFALSE;
+
+   
 
 
@@ -559,11 +734,20 @@
 	ScanFileName(n->GetName(), name, path, date, src, &run, &type);
 
-	
+
+	//
+	// Check that runnumber is inside the desire range of range to analyse [fInitialRun,fEndRun]
+	//
+	if(fEndRun !=-1 && run > fEndRun)
+	    break;
+	if(run < fInitialRun)
+	    continue;
+
+
 	// 
-	// Skip until finding the following run after the fLastDataRun
-	//
-	if( run <= fLastProcessedRun )
+	// Skip until finding the following run after the last processed one, fLastDataRun
+	//
+	if( run <= fLastProcessedDataRun )
 	    continue;
-	
+
 	//
 	// Check that the source name is among one of the selected source names
@@ -571,12 +755,10 @@
 	if( LockSrcName != "")
 	{	
-	    if( src != LockSrcName )
-		continue;
+	    FirstSrcName = LockSrcName;
 	}
-	else
-	{
-	    if( !CheckSourceName(src) )
-		continue;
-	}
+	
+	if( !CheckSourceName(src) )
+	    continue;
+
 
 	//
@@ -608,17 +790,12 @@
 	}
 
-	if( src != FirstSrcName)
+	
+	if( !CompareSourceNames(src,FirstSrcName) )
 	{
-	    if(type!='C')
-	    {  
-		*fLog << err << "ERROR: Source Name differs inside data set ("
+	    *fLog << err << "ERROR: Source Name differs inside data set ("
 		  << src << " vs. " << FirstSrcName << ") ...exit." << endl;
-		return kFALSE;
-	    }
-	    else
-	    {
-	    	*fLog << warn << "Warning: Cal Source Name differs inside data set (" << src << " vs. " << FirstSrcName << ")." << endl;
-	    }
+	    return kFALSE;
 	}
+
 
 
@@ -641,6 +818,5 @@
 		DataRunFound = kTRUE;
 
-		fLastProcessedRun = run;
-		
+		fLastProcessedDataRun = run;	
 	    }
 	    break;
@@ -752,2 +928,23 @@
 
 
+// ------------------------------------------------------------------------------------
+//
+//  Deterimines wheather of data runs has been processed or not
+//
+Bool_t MDataSetIter::HasFinishedSuccesfully()
+{
+
+    if(fEndRun==-1)
+	*fLog << "LastDataRun = " << fLastDataRun << endl;
+    else 
+	*fLog << "EndRun = " <<  fEndRun << endl;
+    
+    *fLog << "LastProcessedDataRun = " << fLastProcessedDataRun << endl;   
+
+
+    if(fEndRun==-1)
+	return fLastProcessedDataRun==fLastDataRun ? kTRUE : kFALSE;
+    else 
+	return fLastProcessedDataRun==fEndRun ? kTRUE : kFALSE;
+       
+}
Index: trunk/MagicSoft/Mars/mtemp/mucm/classes/MDataSetIter.h
===================================================================
--- trunk/MagicSoft/Mars/mtemp/mucm/classes/MDataSetIter.h	(revision 6690)
+++ trunk/MagicSoft/Mars/mtemp/mucm/classes/MDataSetIter.h	(revision 6691)
@@ -8,4 +8,8 @@
 #ifndef MARS_MRunIter
 #include "MRunIter.h"
+#endif
+
+#ifndef MARS_MSequence
+#include "MSequence.h"
 #endif
 
@@ -25,22 +29,32 @@
     TObjArray fSrcList;   // List with the valid source names
 
-    Int_t fLastProcessedRun;  // Runnumber of the last processed run
+   
+    Int_t fInitialRun;  // Runnumber of the first run to proccess
+    Int_t fEndRun;      // Runnumber of the last run to proccess
+    Int_t fLastProcessedDataRun;  // Runnumber of the last processed run
+    Int_t fLastDataRun; // Lst Data runnumber in fFileList
 
     MRunIter* fPedRuns;
     MRunIter* fCalRuns;
     MRunIter* fDataRuns;
+    MSequence* fSequence;
 
     TString fSrcName;
     TString fDate;
 
+    Int_t fDefCalRun;
+    TString fDefCalRunPath;  
+
     MLog *fLog;
     
-    Int_t fDefCalRun;
-    TString fDefCalRunPath;  
    
     Int_t CheckSourceName(TString& src);
+    Int_t CompareSourceNames(TString& src1, TString& src2);
     void  AddToFileList(MDirIter& dir);
-    void  ScanFileName(const TString& file, TString& name, TString& path, TString& date, TString& src, Int_t* run, char* type);
-    
+    void  ScanFileName(const TString& file, TString& name, TString& path, TString& date, TString& src, Int_t* run, char* type);  
+    Int_t IsPreviousRunUsable(MRunIter& oldRun);
+    Int_t Loop(TString option, TString LockSrcName="");
+    void FindLastDataRun();
+
  public:
     
@@ -52,5 +66,13 @@
     Int_t NextDataSet(); 
 
-    void SetInitialRun(Int_t run) { fLastProcessedRun = run; }
+    void SetInitialRun(Int_t run) { fInitialRun = run; }
+    void SetEndRun(Int_t run) 
+    { 
+	fEndRun = run; 
+	FindLastDataRun();
+    } 
+    void SetDefCalRun(Int_t run, const char* path) {fDefCalRun = run; fDefCalRunPath = path; }
+
+    Int_t GetLastProcessedRun() const { return fLastProcessedDataRun; }
 
     TString*  GetSrcName()  { return &fSrcName; }
@@ -74,21 +96,15 @@
     void Reset()
     {
-	MDirIter::Reset();
-	fLastProcessedRun = 0; 
+	fLastProcessedDataRun = 0; 
     }
 
     void Print(const Option_t *option="") const;
+    
+    Bool_t HasFinishedSuccesfully();
 
-    Int_t Loop(TString option, TString LockSrcName="");
-
-    Int_t IsPreviousRunUsable(MRunIter& oldRun);
- 
-
-    Int_t GetLastProcessedRun() const { return fLastProcessedRun; }
-
-    void SetDefCalRun(Int_t run, const char* path) {fDefCalRun = run; fDefCalRunPath = path; }
+    Int_t WriteSequence();
 
 
-    ClassDef(MDataSetIter, 1) // Iterator for runs
+    ClassDef(MDataSetIter, 1) // Iterator for data set made of Pec, Cal and Data runs
 };
 
