Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 9035)
+++ trunk/MagicSoft/Mars/Changelog	(revision 9036)
@@ -44,6 +44,58 @@
      - added
 
+   * mfileio/MreadRflFile.[h,cc]:
+     - derived from MReadFiles (untested)
+
    * datacenter/macros/checkfileavail.C:
      - fixed a type (TSQLRow instead of TSQlRow)
+     - added a missing ;
+
+   * mfileio/MReadTree.[h,cc]:
+     - added some includes
+     - added the IsOwned member function to make sure we don't
+       spoil memory when out pointers are deleted in the destructor
+     - removed some really obsolete workarounds
+     - added a member function to add friends
+     - don't allocate memory in AddFile
+     - implemented the possibility to process also friends
+     - implemented the possibility to read branches from unknown
+       "vendors", i.e. branches for which the class is not available
+       in the dictionary
+     - added a list to hold all parameter containers to be able to 
+       call SetReadyToSave more easily than previously
+
+   * mbase/MParEmulated.[h,cc]:
+     - added
+
+   * mbase/Makefile, mbase/BaseLinkDef.h:
+     - added MParEmulated
+
+   * mbase/MParameters.h:
+     - added functions to get the pointer to the values. Handle this
+       with care.
+
+   * mbase/MParConatiner.h:
+     - made GetterMethod virtual to allow MParEmulated to overwrite it
+     - made fgListMethodCall protected to allow MParEmulated to
+       add its new TMethodCalls
+
+   * datacenter/macros/checkfileavail.C:
+     - removed dependence on caco-files.
+     - fixed query
+     - output query in case of failure
+
+   * datacenter/scripts/checkfilesforsequenceavail:
+     - fixed to use new checkfileavail.C
+
+   * mcalib/MCalibrateData.cc:
+     - removed the fixed scale factor for MCs. It is now left open,
+       thus it can be changed from the callisto resource file
+
+   * mdata/MDataMember.cc:
+     - small improvement to error output
+
+   * mfileio/MWriteRootFile.cc:
+     - small change to log-level
+
 
 
Index: trunk/MagicSoft/Mars/NEWS
===================================================================
--- trunk/MagicSoft/Mars/NEWS	(revision 9035)
+++ trunk/MagicSoft/Mars/NEWS	(revision 9036)
@@ -21,4 +21,9 @@
    * sequences (MSequence or MSequenceSQL) can now directly be retrieved
      from the database. See the constructors for more details.
+
+   * MReadTree now supports friends
+
+   * MReadTree can now read also files not written by MARS and store
+     the data encapsulated in emulated MParContainers
 
  ;merpp
Index: trunk/MagicSoft/Mars/datacenter/macros/checkfileavail.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/checkfileavail.C	(revision 9035)
+++ trunk/MagicSoft/Mars/datacenter/macros/checkfileavail.C	(revision 9036)
@@ -75,9 +75,9 @@
     // -------------------------------------------
 
-    TString query = "MIN( NOT ("
+    TString query = "SELECT MIN( NOT ("
         "ISNULL(fRawFileAvail) OR "
         "ISNULL(fCCFileAvail) OR "
-        "ISNULL(fCaCoFileAvail) OR "
-        "ISNULL(fCaCoFileFound) OR "
+       // "ISNULL(fCaCoFileAvail) OR "
+       // "ISNULL(fCaCoFileFound) OR "
         "ISNULL(fTimingCorrection) OR "
         "ISNULL(fCompmux) "
@@ -94,4 +94,5 @@
     {
         cout << "ERROR - Unexpected number of returned rows (" << res->GetRowCount() << ")" << endl;
+        cout << query << endl;
         delete res;
         return 3;
@@ -103,4 +104,5 @@
     {
         cout << "ERROR - Unexpected result." << endl;
+        cout << query << endl;
         delete res;
         return 3;
Index: trunk/MagicSoft/Mars/datacenter/scripts/checkfilesforsequenceavail
===================================================================
--- trunk/MagicSoft/Mars/datacenter/scripts/checkfilesforsequenceavail	(revision 9035)
+++ trunk/MagicSoft/Mars/datacenter/scripts/checkfilesforsequenceavail	(revision 9036)
@@ -72,12 +72,10 @@
    checkfileavailpath=$logpath/$program/$no
    makedir $checkfileavailpath  >> $scriptlog 2>&1 
-   checkfileavaillog=$checkfileavailpath/$program-$sequence.log
+   checkfileavaillog=$checkfileavailpath/$program-$no2.log
 
-   sequencefile=$sequpath/$no/sequence$no2.txt
-
-   primvar=$no2
+   primvar=$sequence
    setstatus "start" >> $scriptlog 2>&1
 
-   check1=`root -q -b $macrospath/checkfileavail.C+\(\""$sequencefile\",1"\) | tee $checkfileavaillog | intgrep`
+   check1=`root -q -b $macrospath/checkfileavail.C+\($sequence,1\) | tee $checkfileavaillog | intgrep`
 
    case $check1 in
Index: trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc	(revision 9035)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc	(revision 9036)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MCalibrateData.cc,v 1.71 2008-06-14 15:55:51 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MCalibrateData.cc,v 1.72 2008-07-22 18:55:08 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -156,5 +156,5 @@
     : fGeomCam(NULL),   fBadPixels(NULL), fCalibrations(NULL),
       fQEs(NULL), fSignals(NULL), fCerPhotEvt(NULL), fCalibConstCam(NULL),
-    fPedestalFlag(kNo), fSignalType(kPhot), fRenormFactor(1.), fScaleFactor(1.)
+    fPedestalFlag(kNo), fSignalType(kPhot), fRenormFactor(1.), fScaleFactor(1)
 {
 
@@ -424,6 +424,6 @@
     if (header->IsMonteCarloRun())
     {
-        *fLog << inf << "Additional scale factor forced to: 1 (MonteCarloRun)" << endl;
-        fScaleFactor = 1;
+        if (fScaleFactor!=1)
+            *fLog << warn << "WARNING - Additional scale factor for MonteCarlo run not unity but " << fScaleFactor << endl;
     }
     else
Index: trunk/MagicSoft/Mars/mdata/MDataMember.cc
===================================================================
--- trunk/MagicSoft/Mars/mdata/MDataMember.cc	(revision 9035)
+++ trunk/MagicSoft/Mars/mdata/MDataMember.cc	(revision 9036)
@@ -136,6 +136,7 @@
 
     default:
-        *fLog << err << "DataMember " << fDataMember << " of ";
-        *fLog << fObject->GetName() << " neither int nor float... returning 0." << endl;
+        *fLog << err << "ERROR - MDataMember::GetValue: " << fObject->GetName();
+        *fLog << "." << fDataMember << " has unkown type (=";
+        *fLog << fCall->ReturnType() << ")... returning 0." << endl;
         return 0;
     }
Index: trunk/MagicSoft/Mars/mfileio/MReadTree.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 9035)
+++ trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 9036)
@@ -60,5 +60,5 @@
 #include <TFile.h>           // TFile::GetName
 #include <TSystem.h>         // gSystem->ExpandPath
-#include <TLeafElement.h>
+#include <TLeaf.h>
 #include <TChainElement.h>
 #include <TFriendElement.h>
@@ -70,4 +70,5 @@
 #include "MTaskList.h"
 #include "MParameters.h"
+#include "MParEmulated.h"
 #include "MStatusDisplay.h"
 
@@ -226,4 +227,14 @@
 }
 
+void MReadTree::GetListOfBranches(TList &list) const
+{
+    list.AddAll(fTree->GetListOfBranches());
+
+    TIter NextF(fTree->GetListOfFriends());
+    TFriendElement *e = 0;
+    while ((e=(TFriendElement*)NextF()))
+        list.AddAll(e->GetTree()->GetListOfBranches());
+}
+
 // --------------------------------------------------------------------------
 //
@@ -234,28 +245,25 @@
 Bool_t MReadTree::CheckBranchSize()
 {
-    TArrayI entries(fTree->GetListOfBranches()->GetSize());
-    Int_t num=0;
-
-    TIter Next(fTree->GetListOfBranches());
-
-    TBranch *element = NULL;
-    while ((element=(TBranch*)Next()))
-        entries[num++] = (Int_t)element->GetEntries();
-
-    // Check the number of entries of the branches pair-wise
-    for (int i=0; i<num; i++)
-        for (int j=i; j<num; j++)
+    Int_t entries = -1;
+
+    TList list;
+    GetListOfBranches(list);
+
+    TIter Next(&list);
+    TBranch *b = NULL;
+    while ((b=(TBranch*)Next()))
+    {
+        if (entries>=0 && entries!=(Int_t)b->GetEntries())
         {
-            if (entries[i]==entries[j])
-                continue;
-
             *fLog << err << "ERROR - File corruption detected:" << endl;
-            *fLog << "  Due to several circumstances (such at a bug in MReadTree or wrong" << endl;
-            *fLog << "  usage of the file UPDATE mode) you may have produced a file in which" << endl;
-            *fLog << "  at least two branches in the same tree (" << fTree->GetName() << ") have different" << endl;
-            *fLog << "  number of entries. Sorry, but this file (" << GetFileName() << ")" << endl;
-            *fLog << "  is unusable." << endl;
+            *fLog << "  Due to several circumstances  (such at a bug in MReadTree or wrong usgae of" << endl;
+            *fLog << "  the file  UPDATE  mode)  you may have produced a file in which at least two" << endl;
+            *fLog << "  branches have different number of  entries.  Sorry, but this combination of" << endl;
+            *fLog << "  branches, trees and files is unusable." << endl;
             return kFALSE;
         }
+
+        entries = (Int_t)b->GetEntries();
+    }
 
     return kTRUE;
@@ -678,4 +686,78 @@
 
     SetBranchStatus(branch->GetListOfBranches(), kFALSE);
+}
+
+MParContainer *MReadTree::FindCreateObj(MParList &plist, const char *cname, const char *name)
+{
+    MParContainer *pcont=plist.FindCreateObj(cname, name);
+    if (!pcont)
+    {
+        //
+        // if class is not existing in the (root) environment
+        // we cannot proceed reading this branch
+        //
+        *fLog << err << dbginf << "ERROR - Class '" << cname;
+        *fLog << "' for " << name << " not existing in dictionary. Branch skipped." << endl;
+        return 0;
+    }
+
+    fParList.Add(pcont);
+    return pcont;
+}
+
+void *MReadTree::GetParameterPtr(MParList &plist, const TString &name, const char *parname)
+{
+    if (name=="Int_t"   || name=="UInt_t"   ||
+        name=="Short_t" || name=="UShort_t" ||
+        name=="Char_t"  || name=="UChar_t"  ||
+        name=="Bool_t")
+    {
+        MParameterI *par = (MParameterI*)FindCreateObj(plist, "MParameterI", parname);
+        return par ? par->GetPtr() : 0;
+    }
+
+    if (name=="Float_t" || name=="Double_t")
+    {
+        MParameterD *par = (MParameterD*)FindCreateObj(plist, "MParameterD", parname);
+        return par ? par->GetPtr() : 0;
+    }
+
+    // MParContainer **pcont= new MParContainer*;
+    // return FindCreateObj(plist, name, parname);
+
+    MParEmulated *par = (MParEmulated*)FindCreateObj(plist, "MParEmulated", parname);
+    if (!par)
+        return 0;
+
+    par->SetClassName(name);
+    return par->GetPtr();
+}
+
+Bool_t MReadTree::SetBranchAddress(TBranch &b, void *ptr, const char *prefix, const char *type)
+{
+    if (!ptr)
+    {
+        DisableSubBranches(&b);
+        return kFALSE;
+    }
+
+    if (fChain)
+    {
+        TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(b.GetName());
+        if (element && IsOwned(*element))
+            *fLog << warn << "WARNING - Branch address for " << b.GetName() << " was already setup by the user." << endl;
+    }
+
+    const TString bname = b.GetName();
+    const TString cname = type ? type : b.GetClassName();
+
+    fTree->SetBranchAddress(bname, ptr);
+
+    *fLog << inf2 << prefix << " address '" << bname << "' ";
+    if (bname!=cname)
+        *fLog << "[" << cname << "] ";
+    *fLog << "setup for reading." << endl;
+
+    return kTRUE;
 }
 
@@ -751,9 +833,13 @@
     *fLog << inf << fNumEntries << " entries found in file(s)." << endl;
 
+    // Get all branches of this tree and all friends
+    TList blist;
+    GetListOfBranches(blist);
+
     //
     // Get all branches of this tree and
     // create the Iterator to loop over all branches
     //
-    TIter Next(fTree->GetListOfBranches()); // *CHANGED-fChain-to-fTree*
+    TIter Next(&blist); // *CHANGED-fChain-to-fTree*
     TBranch *branch=NULL;
 
@@ -784,73 +870,43 @@
         }
 
-        //
-        // Create a pointer to the pointer to the object in which the
-        // branch data is stored. The pointers are stored in the TChain
-        // object and we get the pointers from there to delete it.
-        //
-        MParContainer **pcont= new MParContainer*;
-
-#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
-        const char *classname = oname;
-#else
-        const char *classname = branch->GetClassName();
-#endif
-
-        //
-        // check if object is existing in the list
-        //
-        *pcont=pList->FindCreateObj(classname, oname);
-
-        if (!*pcont)
+        // Branch already setup/ FIXME: WHAT IF THIS IS A TREE???
+        if (fChain && fChain->GetStatus()->FindObject(branch->GetName()))
         {
-            //
-            // if class is not existing in the (root) environment
-            // we cannot proceed reading this branch
-            //
-            *fLog << warn << dbginf << "Warning: Class '" << classname;
-            *fLog << "' for " << oname << " not existing in dictionary. Branch skipped." << endl;
-            DisableSubBranches(branch);
+            *fLog << warn << "WARNING - Branch " << branch->GetName() << " already setup." << endl;
             continue;
         }
 
-        //
-        // Check whether a Pointer to a pointer already exists.
-        // If we created one already, delete it.
-        //
-        // *CHANGED-fChain-to-fTree*
-        if (fChain)
+        // Get the corresponding class
+        const TString classname = branch->GetClassName();
+
+        TClass *cls = gROOT->GetClass(classname);
+        if (!cls)
         {
-            TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(bname);
-            if (element)
-                delete (MParContainer**)element->GetBaddress();
+            // FIXME: With or without dot?
+            TLeaf *l = branch->GetLeaf(branch->GetName()); // FIXME: 1st leaf?
+            if (!l)
+                continue;
+
+            void *ptr = GetParameterPtr(*pList, l->GetTypeName(), oname);
+            if (SetBranchAddress(*branch, ptr, "Leaf", l->GetTypeName()))
+                num++;
+
+            continue;
         }
-        /* This can't be done here for memory trees - see
-           PostProcess for more details.
+
+        // The class is known in the dictionary and loaded!
+        if (!cls->IsLoaded() || !cls->InheritsFrom(MParContainer::Class()))
+        {
+            void *ptr = GetParameterPtr(*pList, classname, oname);
+            if (SetBranchAddress(*branch, ptr, "Emulated branch"))
+                num++;
+        }
         else
         {
-            TBranch *branch = (TBranch*)fTree->GetBranch(bname);
-            if (branch)
-            {
-                *fLog << bname << " " << (void*)branch->GetAddress() << " " << pcont << endl;
-                delete (MParContainer**)branch->GetAddress();
-            }
+            MParContainer **pcont= new MParContainer*;
+            *pcont=FindCreateObj(*pList, classname, oname);
+            if (SetBranchAddress(*branch, pcont, "Master branch"))
+                num++;
         }
-        */
-
-        //
-        // here pcont is a pointer the to container in which the data from
-        // the actual branch should be stored - enable branch.
-        //
-        fTree->SetBranchAddress(bname, pcont); // *CHANGED-fChain-to-fTree*
-
-        *fLog << inf2 << "Master branch address '" << bname << "' ";
-        if ((TString)bname!=(TString)classname)
-            *fLog << "[" << classname << "] ";
-        *fLog << "setup for reading." << endl;
-
-        //*fLog << "Branch " << bname << " autodel: " << (int)branch->IsAutoDelete() << endl;
-        //branch->SetAutoDelete();
-
-        num++;
     }
 
@@ -889,34 +945,5 @@
 void MReadTree::SetReadyToSave(Bool_t flag)
 {
-    if (!fChain)
-        return;
-
-    TIter Next(fChain->GetStatus());
-
-    TChainElement *element = NULL;
-    while ((element=(TChainElement*)Next()))
-    {
-        //
-        // Check whether the branch is enabled
-        //
-        if (!element->GetStatus())
-            continue;
-
-        //
-        // Get the pointer to the pointer of the corresponding container
-        //
-        MParContainer **pcont = (MParContainer**)element->GetBaddress();
-
-        //
-        // Check whether the pointer is not NULL
-        //
-        if (!pcont || !*pcont)
-            continue;
-
-        //
-        // Set the ready to save status of the container.
-        //
-        (*pcont)->SetReadyToSave(flag);
-    }
+    fParList.R__FOR_EACH(MParContainer, SetReadyToSave)(flag);
 
     MTask::SetReadyToSave(flag);
Index: trunk/MagicSoft/Mars/mfileio/MReadTree.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 9035)
+++ trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 9036)
@@ -31,4 +31,5 @@
 
     MTaskList *fTaskList;      // Tasklist to set StreamId
+    TList      fParList;
 
     enum { kChainWasChanged = BIT(14) };
@@ -42,4 +43,9 @@
 
     Bool_t IsOwned(const TChainElement &e) const;
+
+    void GetListOfBranches(TList &list) const;
+    MParContainer *FindCreateObj(MParList &plist, const char *cname, const char *name);
+    void *GetParameterPtr(MParList &plist, const TString &bb, const char *parname);
+    Bool_t SetBranchAddress(TBranch &b, void *ptr, const char *prefix, const char *type=0);
 
     void DisableSubBranches(TBranch *b);
@@ -103,3 +109,2 @@
 
 #endif
-
Index: trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 9035)
+++ trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 9036)
@@ -469,5 +469,5 @@
                 }
 
-                *fLog << inf << "Unnecessary parameter container '" << cname << "' not found..." << endl;
+                *fLog << inf2 << "Unnecessary parameter container '" << cname << "' not found..." << endl;
                 delete fBranches.Remove(entry);
                 continue;
