Index: trunk/MagicSoft/Mars/datacenter/macros/filldotrun.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/filldotrun.C	(revision 9469)
+++ trunk/MagicSoft/Mars/datacenter/macros/filldotrun.C	(revision 9487)
@@ -42,5 +42,6 @@
 //   070416-0,
 //   080220-0, 080519-0, 080912-0, 081204-0, 081214-0,
-//   090203-0, 090221-0, 090522-0, 090525-0, 090616-0
+//   090203-0, 090221-0, 090522-0, 090525-0, 090616-0, 090625-0, 090702-0,
+//   090706-0, 090731-0
 //
 // Usage:
@@ -96,14 +97,10 @@
         return -1;
     }
-    //read the file a second time for the check for number of columns
-    ifstream fin2(filename);
 
     TString strng;
-    TString strng2;
     TObjArray *array = new TObjArray();
     Int_t check=0;
 
     strng.ReadLine(fin);
-    strng2.ReadLine(fin2);
     if (strng!=TString("[CC Plain Run Summary File]"))
     {
@@ -115,5 +112,4 @@
 
     strng.ReadLine(fin);
-    strng2.ReadLine(fin2);
     TRegexp reg("[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]");
     TString arehucas = strng(reg);
@@ -134,5 +130,6 @@
         version!=200812040 && version!=200812140 &&
         version!=200902030 && version!=200902210 && version!=200905220 &&
-        version!=200905250 && version!=200906160)
+	version!=200905250 && version!=200906160 && version!=200906250 &&
+	version!=200907020 && version!=200907060 && version!=200907310)
     {
         cout << filename << ": File Version unknown - please update the macro!" << endl;
@@ -142,12 +139,22 @@
     }
 
+    Int_t telcheck=1;
+    TString tcheck;
     if (version >= 200805190)
     {
-        strng.ReadLine(fin);
-        strng2.ReadLine(fin2);
+	strng.ReadLine(fin);
         if (!strng.BeginsWith("Telescope M"))
         {
             cout << "WARNING - Line 3 doesn't start with 'Telescope M'." << endl;
             cout << strng << endl;
+        }
+	telcheck = atoi(strng.Data()+11);
+
+        if (telcheck != 1 && telcheck != 2)
+        {
+            cout << filename << ": Telescope declaration wrong!" << endl;
+            cout << "Third Line: " << strng << endl;
+            cout << endl;
+            return -1;
         }
     }
@@ -156,5 +163,4 @@
     {
         strng.ReadLine(fin);
-        strng2.ReadLine(fin2);
         if (strng[0]!='#')
         {
@@ -176,12 +182,27 @@
         // 200603300 - 200802200: 52 columns
         // 200805190 - 200809120: 54 columns
-        // > 200812040:           55 columns
+        // 200812040 - 200906160: 55 columns
+        // >= 200906250:          61 columns
         //
-
-        strng2.ReadLine(fin2);
-        //fill an array with the values separated by ' '
-        array=strng2.Tokenize(" ");
-        check=array->GetEntries();
-        cout << check << endl;
+	strng.ReadLine(fin);
+	//fill an array with the values separated by ' '. Advantage: In contrast to ReadToDelim(' ') objects 'between'
+	//two whitespaces are not written to the array. In case of a missing column the error is detected by comparison
+	//with the number of default columns. In case of an 'accidental' double(triple)-whitespace the columns are treated
+	//in the right way, only a warning is released to edit the file, if necessary.
+	array = strng.Tokenize(" ");
+	check = array->GetEntries();
+
+        if (strng.Contains("  ") || strng. Contains("   "))
+	{
+	    cout << "WARNING - Multiple whitespaces found, please check the file:" << endl;
+	    cout << filename << endl;
+	}
+
+        /*
+	for (int n=0; n< array->GetEntries(); n++)
+	{
+	    cout << (*array)[n]->GetName() << endl;
+	}
+        */
         //check the number of columns for the different versions, if there is an empty line (check=0), do nothing
         if (check != 0)
@@ -192,14 +213,20 @@
                 (version >  200510250 && version <= 200802200 && check != 52) ||
                 (version >  200802200 && version <= 200809120 && check != 54) ||
-                (version >  200809120 && check != 55))
-            {
-                strng.ReadLine(fin);
-                cout << "ERROR - Number of columns doesn't match number of required columns." << endl;
-                array->Delete();
-                continue;
-            }
-        }
-        array->Delete();
-
+                (version >  200809120 && version <= 200906160 && check != 55) ||
+                (version >  200906160 && ((telcheck == 1 && check != 61) || (telcheck == 2 && check != 63))))
+            {
+		cout << "ERROR - Number of columns (" << check << ") doesn't match number of required columns, please check the file:" << endl;
+                cout << filename << endl;
+		delete array;
+                continue;
+            }
+	}
+
+	//when reaching the end of the file...
+	if (!fin)
+            break;
+
+        // count variable for the columns
+	Int_t i = 0;
 
         // ========== Col 1: Telescope Number =========
@@ -207,23 +234,15 @@
         if (version >=200805190)
         {
-            strng.ReadToDelim(fin, ' ');
-            if (!fin)
-                break;
-            if (strng[0]!='M')
+            strng = (*array)[i++]->GetName();
+
+	    if (strng[0]!='M')
             {
                 cout << "WARNING - First character is not an M." << endl;
                 cout << strng << endl;
-                strng.ReadLine(fin);
-                continue;
-            }
-            if (strng[1]!='1')
-            {
-                cout << "WARNING - Only MAGIC 1 implemented so far." << endl;
-                cout << strng << endl;
-                strng.ReadLine(fin);
-                continue;
-            }
-
-            telnumber = atoi(strng.Data()+1);
+                delete array;
+                continue;
+            }
+	    telnumber = atoi(strng.Data()+1);
+
         }
 
@@ -231,7 +250,5 @@
         //Reading the line
         //and converting some strings to ints/floats
-        strng.ReadToDelim(fin, ' ');
-        if (!fin)
-            break;
+	strng = (*array)[i++]->GetName();
 
         Int_t runnumber = atoi(strng.Data());
@@ -241,9 +258,10 @@
         if (runnumber == 0)
         {
-            strng.ReadLine(fin);
+	    delete array;
             cout << "WARNING - Runnumber == 0" << endl;
-            cout << strng << endl;
             continue;
-        }
+	}
+
+	//cout << runnumber << " ";
 
         // ========== Col 3: Subrun Number ========= starting with version 200805190
@@ -251,5 +269,5 @@
         if (version >=200805190)
         {
-            strng.ReadToDelim(fin, ' ');
+	    strng = (*array)[i++]->GetName();
             filenumber = atoi(strng.Data());
         }
@@ -261,10 +279,11 @@
             // FIXME: Maybe we can implement a switch to update mode?
             cout << "WARNING - Entry M" << telnumber << ":" << runnumber << "/" << filenumber << " already existing... skipped." << endl;
-            strng.ReadLine(fin);
+            delete array;
             continue;
-        }
+	}
+
 
         // ========== Col 4: Run Type =========
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         if (strng.Contains("???"))
             strng="n/a";
@@ -274,20 +293,19 @@
         {
             cout << "ERROR - RunType " << strng << " not available." << endl;
-            strng.ReadLine(fin);
+            delete array;
             continue;
-        }
-
-        //cout << runtype << " ";
+	}
+
 
         // ========== Col 5,6: Start Time =========
         TString startdate, starttime;
-        startdate.ReadToDelim(fin, ' ');
-        starttime.ReadToDelim(fin, ' ');
+	startdate = (*array)[i++]->GetName();
+	starttime = (*array)[i++]->GetName();
         //cout << startdate << " " << starttime << " ";
 
         // ========== Col 7,8: Stop Time =========
-        TString stopdate, stoptime;
-        stopdate.ReadToDelim(fin, ' ');
-        stoptime.ReadToDelim(fin, ' ');
+	TString stopdate, stoptime;
+	stopdate = (*array)[i++]->GetName();
+	stoptime = (*array)[i++]->GetName();
         //cout << stopdate << " " << stoptime << " ";
 
@@ -302,5 +320,5 @@
 
         // ========== Col 9: Source Name =========
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         if (strng.Contains("???"))
             strng="Unavailable";
@@ -309,26 +327,27 @@
         if (sourcekey<0)
         {
-            strng.ReadLine(fin);
+            delete array;
             continue;
         }
-        //cout << sourcekey << " ";
+
+	//cout << sourcekey << " ";
 
         // ========== Col 10,11: Local source position =========
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         Float_t zd = atof(strng.Data());
 
-        strng.ReadToDelim(fin, ' ');
-        Float_t az = atof(strng.Data());
+	strng = (*array)[i++]->GetName();
+	Float_t az = atof(strng.Data());
 
         //cout << zd << " " << az << " ";
 
         // ========== Col 12: Number of Events =========
-        strng.ReadToDelim(fin, ' ');
-        Int_t evtno = atoi(strng.Data());
+	strng = (*array)[i++]->GetName();
+	Int_t evtno = atoi(strng.Data());
 
         //cout << evtno << " ";
 
         // ========== Col 13: Project Name =========
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         if (strng.Contains("???"))
             strng="Unavailable";
@@ -337,12 +356,12 @@
         if (projkey<0)
         {
-            strng.ReadLine(fin);
+            delete array;
             continue;
         }
-        //cout << projkey << " ";
+	//cout << projkey << " ";
 
         // ========== Col 14: Trigger Table Name =========
         // starting from version 200411130: Col 14,15: Trigger Table Name =========
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         if (strng.Contains("???"))
             strng="n/a";
@@ -355,9 +374,9 @@
             if (l1triggerkey<0)
             {
-                strng.ReadLine(fin);
-                continue;
-            }
-
-            strng.ReadToDelim(fin, ' ');
+                delete array;
+                continue;
+            }
+
+	    strng = (*array)[i++]->GetName();
             if (strng.Contains("???"))
                 strng="n/a";
@@ -366,7 +385,7 @@
             if (l2triggerkey<0)
             {
-                strng.ReadLine(fin);
-                continue;
-            }
+                delete array;
+                continue;
+	    }
         }
         else
@@ -390,5 +409,5 @@
                     if (l2triggerkey<0)
                     {
-                        strng.ReadLine(fin);
+                        delete array;
                         continue;
                     }
@@ -408,5 +427,5 @@
                     if (l1triggerkey<0)
                     {
-                        strng.ReadLine(fin);
+                        delete array;
                         continue;
                     }
@@ -415,5 +434,5 @@
                     if (l2triggerkey<0)
                     {
-                        strng.ReadLine(fin);
+                        delete array;
                         continue;
                     }
@@ -426,5 +445,5 @@
                     if (l1triggerkey<0)
                     {
-                        strng.ReadLine(fin);
+                        delete array;
                         continue;
                     }
@@ -450,25 +469,27 @@
 
         // ========== Col 16-18: TrigRate, L2 UnPresc Rate, L2 Presc Rate ==========
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         Float_t trigrate = atof(strng.Data());
 
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         Float_t l2uprate = atof(strng.Data());
 
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         Float_t l2prrate = atof(strng.Data());
 
         // ========== Col 19,20: DaqRate, Storage Rate ==========
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         Float_t daqrate = atof(strng.Data());
 
-        strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
         Float_t storerate = atof(strng.Data());
 
         // ========== Col 21: HV table =========
-        if (version==200405050 || version==200405140)
-            strng.ReadToDelim(fin, '\n');
-        else
-            strng.ReadToDelim(fin, ' ');
+	strng = (*array)[i++]->GetName();
+	if (version==200405050 || version==200405140)
+	{
+	    delete array;
+	    continue;
+	}
         if (strng.Contains("???"))
             strng="n/a";
@@ -477,10 +498,13 @@
         if (hvkey<0)
         {
-            //strng.ReadLine(fin);
+            delete array;
             continue;
         }
 
         if (version==200405180 || version==200407270)
-            strng.ReadLine(fin);
+	{
+	    delete array;
+	    continue;
+	}
 
         Int_t testflagkey=1;
@@ -492,11 +516,11 @@
         {
             // ========== Col 22-38: DC and HV-values, mjd =========
-            for (int i=0 ; i<17 ; i++)
-            {
-                strng.ReadToDelim(fin, ' ');
+            for (int n=0 ; n<17 ; n++)
+            {
+                i++;
             }
 
             // ========== Col 39: test-flag =========
-            strng.ReadToDelim(fin, ' ');
+	    strng = (*array)[i++]->GetName();
             if (strng.Contains("???"))
                 strng="n/a";
@@ -505,10 +529,10 @@
             if (testflagkey<0)
             {
-                strng.ReadLine(fin);
+                delete array;
                 continue;
             }
 
             // ========== Col 40: light conditions =========
-            strng.ReadToDelim(fin, ' ');
+	    strng = (*array)[i++]->GetName();
             if (strng.Contains("???"))
                 strng="n/a";
@@ -517,10 +541,10 @@
             if (lightcondkey<0)
             {
-                strng.ReadLine(fin);
+                delete array;
                 continue;
             }
 
             // ========== Col 41: discriminator threshold table =========
-            strng.ReadToDelim(fin, ' ');
+	    strng = (*array)[i++]->GetName();
             if (strng.Contains("???"))
                 strng="n/a";
@@ -529,10 +553,10 @@
             if (dttablekey<0)
             {
-                strng.ReadLine(fin);
+                delete array;
                 continue;
             }
 
             // ========== Col 42: trigger delay table =========
-            strng.ReadToDelim(fin, ' ');
+	    strng = (*array)[i++]->GetName();
             if (strng.Contains("???"))
                 strng="n/a";
@@ -541,17 +565,19 @@
             if (triggerdelaytablekey<0)
             {
-                strng.ReadLine(fin);
+                delete array;
                 continue;
             }
 
             // ========== Col 43,44: Telescope RA and Dec sent to drive =========
-            strng.ReadToDelim(fin, ' ');
-            strng.ReadToDelim(fin, ' ');
+            i++;
+            i++;
 
             // ========== Col 45: Calibration Script =========
+	    strng = (*array)[i++]->GetName();
             if (version>=200411130 && version<=200510250)
-                strng.ReadToDelim(fin, '\n');
-            else
-                strng.ReadToDelim(fin, ' ');
+	    {
+		delete array;
+		continue;
+	    }
             if (strng.Contains("???"))
                 strng="n/a";
@@ -560,5 +586,5 @@
             if (calibrationscriptkey<0)
             {
-                strng.ReadLine(fin);
+                delete array;
                 continue;
             }
@@ -570,5 +596,5 @@
         {
             // ========== Col 46: Observation Mode =========
-            strng.ReadToDelim(fin, ' ');
+	    strng = (*array)[i++]->GetName();
             if (strng.Contains("???"))
                 strng="n/a";
@@ -577,17 +603,19 @@
             if (observationmodekey<0)
             {
-                strng.ReadLine(fin);
+                delete array;
                 continue;
             }
 
             // ========== Col 47-54: Source RA and Dec, DT's and IPR =========
-            for (int i=0 ; i<7 ; i++)
-            {
-                strng.ReadToDelim(fin, ' ');
-            }
-            if (version<=200809120)
-                strng.ReadToDelim(fin, '\n');
-            else
-                strng.ReadToDelim(fin, ' ');
+            for (int n=0 ; n<7 ; n++)
+            {
+                i++;
+            }
+	    strng = (*array)[i++]->GetName();
+	    if (version<=200809120)
+	    {
+		delete array;
+		continue;
+	    }
         }
 
@@ -596,6 +624,11 @@
         {
             // ========= Col 55: SumTrigger flag =========
-            strng.ReadToDelim(fin, '\n');
-            if (strng.Contains("???"))
+	    strng = (*array)[i++]->GetName();
+            if (version<=200906160)
+	    {
+		delete array;
+		continue;
+	    }
+	    if (strng.Contains("???"))
                 strng="n/a";
 
@@ -603,9 +636,94 @@
             if (sumtriggerflagkey<0)
             {
-                strng.ReadLine(fin);
-                continue;
-            }
-
-        }
+                delete array;
+                continue;
+            }
+        }
+
+        Int_t l3triggerkey=1;
+        Int_t cyclekey=1;
+        Int_t pikey=1;
+        Int_t workinggroupkey=1;
+	Int_t proposalkey=1;
+        Float_t l3trigrate=0;
+        TString wheelpos1 = "NULL";
+        TString wheelpos2 = "NULL";
+        if (version>=200906250)
+        {
+            // for MAGIC 2: additional columns wheel_pos1 and ~2
+
+            if (telnumber == 2)
+            {
+		strng = (*array)[i++]->GetName();
+                wheelpos1 = strng.Data();
+		strng = (*array)[i++]->GetName();
+                wheelpos2 = strng.Data();
+            }
+
+	    // ========= Col 56: L3 trigger table =========
+	    strng = (*array)[i++]->GetName();
+            if (strng.Contains("???") || strng.Contains("na") || strng.Contains("Unknown"))
+                strng="n/a";
+
+            l3triggerkey = serv.QueryKeyOfName("L3TriggerTable", strng);
+            if (l3triggerkey<0)
+            {
+                delete array;
+                continue;
+            }
+
+            // ========= Col 57: L3 trigger rate =========
+	    strng = (*array)[i++]->GetName();
+            l3trigrate = atof(strng.Data());
+
+            // ========= Col 58: Cycle =========
+	    strng = (*array)[i++]->GetName();
+            if (strng.Contains("???") || strng.Contains("na"))
+                strng="n/a";
+
+            cyclekey = serv.QueryKeyOfName("Cycle", strng);
+            if (cyclekey<0)
+            {
+                delete array;
+                continue;
+            }
+
+            // ========= Col 59: PI =========
+	    strng = (*array)[i++]->GetName();
+            if (strng.Contains("???") || strng.Contains("na"))
+                strng="n/a";
+
+            pikey = serv.QueryKeyOfName("PI", strng);
+            if (pikey<0)
+            {
+                delete array;
+                continue;
+            }
+
+            // ========= Col 60: Working group =========
+	    strng = (*array)[i++]->GetName();
+            if (strng.Contains("???") || strng.Contains("na"))
+                strng="n/a";
+
+            workinggroupkey = serv.QueryKeyOfName("WorkingGroup", strng);
+            if (workinggroupkey<0)
+            {
+                delete array;
+                continue;
+            }
+
+            // ========= Col 61: Proposal =========
+	    strng = (*array)[i++]->GetName();
+            if (strng.Contains("???") || strng.Contains("na"))
+                strng="n/a";
+
+            proposalkey = serv.QueryKeyOfName("Proposal", strng);
+            if (proposalkey<0)
+            {
+                delete array;
+                continue;
+            }
+        }
+	delete array;
 
 
@@ -635,4 +753,9 @@
         query += Form("fObservationModeKEY=%d, ",   observationmodekey);
         query += Form("fSumTriggerFlagKEY=%d, ",    sumtriggerflagkey);
+        query += Form("fL3TriggerTableKEY=%d, ",    l3triggerkey);
+        query += Form("fCycleKEY=%d, ",             cyclekey);
+        query += Form("fPIKEY=%d, ",                pikey);
+        query += Form("fWorkingGroupKEY=%d, ",      workinggroupkey);
+        query += Form("fProposalKEY=%d, ",          proposalkey);
         if (!TMath::IsNaN(zd) && TMath::Finite(zd))
             query += Form("fZenithDistance=%d, ", TMath::Nint(zd));
@@ -649,4 +772,8 @@
         if (!TMath::IsNaN(l2uprate) && TMath::Finite(l2uprate))
             query += Form("fL2RateUnpresc=%d, ", TMath::Nint(l2uprate));
+        if (!TMath::IsNaN(l3trigrate) && TMath::Finite(l3trigrate))
+            query += Form("fL3TriggerRate=%d, ", TMath::Nint(l3trigrate));
+	query += Form("fWheelPos1=%s, ", wheelpos1.Data());
+	query += Form("fWheelPos2=%s, ", wheelpos2.Data());
         query += "fMagicNumberKEY=1, fExcludedFDAKEY=1";
 
