Index: fact/tools/marsmacros/mc2csv/MonteCarlo.C
===================================================================
--- fact/tools/marsmacros/mc2csv/MonteCarlo.C	(revision 14610)
+++ fact/tools/marsmacros/mc2csv/MonteCarlo.C	(revision 14625)
@@ -17,4 +17,5 @@
 
     OpenRootFile();
+    if ( !mRootFileOpend ) return;
 
     LoadEventTree(  "Events");
@@ -36,4 +37,5 @@
 
     OpenRootFile();
+    if ( !mRootFileOpend ) return;
 
     LoadEventTree(  evtsTreeName);
@@ -56,4 +58,5 @@
 
     OpenRootFile();
+    if ( !mRootFileOpend ) return;
 
     LoadEventTree(  evtsTreeName);
@@ -84,4 +87,5 @@
     // source file
     mpRootFile      = NULL;
+    mRootFileOpend  = false;
 
     // Trees
@@ -141,6 +145,8 @@
     {
         cout << "ERROR - Could not find file " << mFileName << endl;
+        mRootFileOpend =false;
         return ;
     }
+    mRootFileOpend = true;
 
     return;
@@ -192,5 +198,10 @@
     }
 
+    // =======================================================================
     //Set Adresses to Branches in RunHeader-Tree
+
+    // -----------------------------------------------------------------------
+
+    // MGeomCam
     if ( mpHeaderTree->GetBranchStatus("MGeomCam.") )
     {
@@ -198,4 +209,8 @@
         mpHeaderTree->SetBranchAddress("MGeomCam.",         &mpGeomCam);
     }
+
+    // -----------------------------------------------------------------------
+
+    // IntendedPulsePos
     if ( mpHeaderTree->GetBranchStatus("IntendedPulsePos.") )
     {
@@ -203,4 +218,8 @@
         mpHeaderTree->SetBranchAddress("IntendedPulsePos.", &mpIntendedPulsePos);
     }
+
+    // -----------------------------------------------------------------------
+
+    // MMcRunHeader
     if ( mpHeaderTree->GetBranchStatus("MMcRunHeader.") )
     {
@@ -208,4 +227,8 @@
         mpHeaderTree->SetBranchAddress("MMcRunHeader.",     &mpMcRunHeader);
     }
+
+    // -----------------------------------------------------------------------
+
+    // ElectronicNoise
     if ( mpHeaderTree->GetBranchStatus("ElectronicNoise.") )
     {
@@ -213,4 +236,8 @@
         mpHeaderTree->SetBranchAddress("ElectronicNoise.",  &mpElectronicNoise);
     }
+
+    // -----------------------------------------------------------------------
+
+    // MRawRunHeader
     if ( mpHeaderTree->GetBranchStatus("MRawRunHeader.") )
     {
@@ -218,4 +245,8 @@
         mpHeaderTree->SetBranchAddress("MRawRunHeader.",    &mpRawRunHeader);
     }
+
+    // -----------------------------------------------------------------------
+
+    // MCorsikaRunHeader
     if ( mpHeaderTree->GetBranchStatus("MCorsikaRunHeader.") )
     {
@@ -224,4 +255,6 @@
     }
 
+    // =======================================================================
+
     return;
 }
@@ -237,43 +270,84 @@
     mpHeaderTree->GetEntry();    //This causes problems
 
+    // -----------------------------------------------------------------------
 
     //Get values from "MGeomCam" Branch
         //Getter functions from "MGeomCamFACT.h"
-    mCamDist                = mpGeomCam->GetCameraDist();
-    mNumberOfPixels         = mpGeomCam->GetNumPixels();
-    mNumberOfSectors        = mpGeomCam->GetNumSectors();
-    mNumberOfAreas          = mpGeomCam->GetNumAreas();
+    if ( mpGeomCam != NULL)
+    {
+        mCamDist                = mpGeomCam->GetCameraDist();
+        mNumberOfPixels         = mpGeomCam->GetNumPixels();
+        mNumberOfSectors        = mpGeomCam->GetNumSectors();
+        mNumberOfAreas          = mpGeomCam->GetNumAreas();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read data from: MGeomCam" << endl;
+
+    // -----------------------------------------------------------------------
 
     //Get values from "IntendedPulsePos" Branch
-    mIntendedPulsePos       = mpIntendedPulsePos->GetVal();
+    if ( mpIntendedPulsePos != NULL)
+    {
+        mIntendedPulsePos       = mpIntendedPulsePos->GetVal();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read data from: IntendedPulsePos" << endl;
+
+    // -----------------------------------------------------------------------
 
     //Get values from "MMcRunHeader" Branch from event Tree
-    mNumSimulatedShowers    = mpMcRunHeader->GetNumSimulatedShowers();
+    if ( mpMcRunHeader != NULL)
+    {
+        mNumSimulatedShowers    = mpMcRunHeader->GetNumSimulatedShowers();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read data from: MMcRunHeader" << endl;
+
+    // -----------------------------------------------------------------------
 
     //Get number of Events from event Tree
-    mNumberOfEntries        = mpEventTree->GetEntries();
+    if ( mpEventTree != NULL)
+    {
+        mNumberOfEntries        = mpEventTree->GetEntries();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read number of Events from event Tree" << endl;
+
     if (mVerbosityLvl > 0)
         cout << "Event Tree has " << mNumberOfEntries << " entries" << endl;
 
+    // -----------------------------------------------------------------------
+
     //Get values from "MRawRunHeader" Branch
-    mNumberOfEvents         = mpRawRunHeader->GetNumEvents(); // couses problems
-    mNumberOfEventsRead     = mpRawRunHeader->GetNumEventsRead();
-    mSamplingFrequency      = mpRawRunHeader->GetFreqSampling();
-    mSourceName             = mpRawRunHeader->GetSourceName();
-    mFileNumber             = mpRawRunHeader->GetFileNumber();
-    mNumberOfSamples        = mpRawRunHeader->GetNumSamplesHiGain();
-    mNumberOfBytes          = mpRawRunHeader->GetNumBytesPerSample();
-    mRunNumber              = mpRawRunHeader->GetRunNumber();
-    mRunType                = mpRawRunHeader->GetRunType();
-
+    if ( mpRawRunHeader != NULL)
+    {
+        mNumberOfEvents         = mpRawRunHeader->GetNumEvents(); // couses problems
+        mNumberOfEventsRead     = mpRawRunHeader->GetNumEventsRead();
+        mSamplingFrequency      = mpRawRunHeader->GetFreqSampling();
+        mSourceName             = mpRawRunHeader->GetSourceName();
+        mFileNumber             = mpRawRunHeader->GetFileNumber();
+        mNumberOfSamples        = mpRawRunHeader->GetNumSamplesHiGain();
+        mNumberOfBytes          = mpRawRunHeader->GetNumBytesPerSample();
+        mRunNumber              = mpRawRunHeader->GetRunNumber();
+        mRunType                = mpRawRunHeader->GetRunType();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read data from: MRawRunHeader" << endl;
+
+    // -----------------------------------------------------------------------
 
     //Get values from "MCorsikaRunHeader" Branch
-    mSlopeSpectrum          = mpCorsikaRunHeader->GetSlopeSpectrum();
-    mEnergyMin              = mpCorsikaRunHeader->GetEnergyMin();
-    mEnergyMax              = mpCorsikaRunHeader->GetEnergyMax();
-    mZdMin                  = mpCorsikaRunHeader->GetZdMin();
-    mZdMax                  = mpCorsikaRunHeader->GetZdMax();
-    mAzMin                  = mpCorsikaRunHeader->GetAzMin();
-    mAzMax                  = mpCorsikaRunHeader->GetAzMax();
+    if ( mpCorsikaRunHeader != NULL)
+    {
+        mSlopeSpectrum          = mpCorsikaRunHeader->GetSlopeSpectrum();
+        mEnergyMin              = mpCorsikaRunHeader->GetEnergyMin();
+        mEnergyMax              = mpCorsikaRunHeader->GetEnergyMax();
+        mZdMin                  = mpCorsikaRunHeader->GetZdMin();
+        mZdMax                  = mpCorsikaRunHeader->GetZdMax();
+        mAzMin                  = mpCorsikaRunHeader->GetAzMin();
+        mAzMax                  = mpCorsikaRunHeader->GetAzMax();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read data from: MCorsikaRunHeader" << endl;
 
     return;
@@ -293,8 +367,12 @@
     }
 
-
+    // =======================================================================
     //Set Adresses to Branches in Events-Tree
+
     if (mVerbosityLvl > 1) cout << "...SetBranchAddresses:" << endl;
 
+    // -----------------------------------------------------------------------
+
+    // MRawEvtData
     if ( mpEventTree->GetBranchStatus("MRawEvtData.") != -1 )
     {
@@ -302,4 +380,9 @@
         mpEventTree->SetBranchAddress("MRawEvtData.",       &mpRawEventData);
     }
+    else cout << "...could not load branch: MRawEvtData" << endl;
+
+    // -----------------------------------------------------------------------
+
+    // IncidentAngle
     if ( mpEventTree->GetBranchStatus("IncidentAngle.") )
     {
@@ -309,4 +392,9 @@
         mpEventTree->SetBranchAddress("IncidentAngle.",     &mpIncidentAngle);
     }
+    else cout << "...could not load branch: IncidentAngle" << endl;
+
+    // -----------------------------------------------------------------------
+
+    // MMcEvt
     if ( mpEventTree->GetBranchStatus("MMcEvt.") )
     {
@@ -314,4 +402,9 @@
         mpEventTree->SetBranchAddress("MMcEvt.",            &mpMcEventMetaData);
     }
+    else cout << "...could not load branch: McEvt" << endl;
+
+    // -----------------------------------------------------------------------
+
+    // MRawEvtHeader
     if ( mpEventTree->GetBranchStatus("MRawEvtHeader.") )
     {
@@ -319,4 +412,9 @@
         mpEventTree->SetBranchAddress("MRawEvtHeader.",     &mpRawEventHeader);
     }
+    else cout << "...could not load branch: MRawEvtHeader" << endl;
+
+    // -----------------------------------------------------------------------
+
+    // MCorsikaEvtHeader
     if ( mpEventTree->GetBranchStatus("MCorsikaEvtHeader.") )
     {
@@ -324,4 +422,7 @@
         mpEventTree->SetBranchAddress("MCorsikaEvtHeader.", &mpCorsikaEvtHeader);
     }
+    else cout << "...could not load branch: MCorsikaEvtHeader" << endl;
+
+    // =======================================================================
 
     return;
@@ -331,42 +432,71 @@
 MonteCarlo::ReadEventMetaData()
 {
-    if (mVerbosityLvl > 1) cout << "...reading event header" << endl;
+    if (mVerbosityLvl > 1)
+        cout << "...reading event header" << endl;
 
     //Get values from "MGeomCamFACT" Branch
-    mIncidentAngle          = mpIncidentAngle->GetVal();
+    if ( mpIncidentAngle != NULL)
+    {
+        mIncidentAngle          = mpIncidentAngle->GetVal();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read data from: MGeomCamFACT" << endl;
+
+    // -----------------------------------------------------------------------
 
     //Get values from "MMcEvt" Branch
+    if ( mpMcEventMetaData != NULL)
+    {
         //The following Getter Functions can be found in MMcEvt.h
-    mCorsikaEventNumber     = mpMcEventMetaData->GetEvtNumber();
-    mPhotElFromShower       = mpMcEventMetaData->GetPhotElfromShower();
-    mPhotElinCamera         = mpMcEventMetaData->GetPhotElinCamera();
-        //The following Getter Functions can be found in MMcEvtBasic.h
-    mPartId                 = mpMcEventMetaData->GetPartId();
-    mPartName               = mpMcEventMetaData->GetParticleName(mPartId);
-    mPartSymbol             = mpMcEventMetaData->GetParticleSymbol(mPartId);
-    mEnergy                 = mpMcEventMetaData->GetEnergy();
-    mImpact                 = mpMcEventMetaData->GetImpact();
-    mTelescopePhi           = mpMcEventMetaData->GetTelescopePhi();
-    mTelescopeTheta         = mpMcEventMetaData->GetTelescopeTheta();
-    mPhi                    = mpMcEventMetaData->GetParticlePhi();
-    mTheta                  = mpMcEventMetaData->GetParticleTheta();
+        mCorsikaEventNumber     = mpMcEventMetaData->GetEvtNumber();
+        mPhotElFromShower       = mpMcEventMetaData->GetPhotElfromShower();
+        mPhotElinCamera         = mpMcEventMetaData->GetPhotElinCamera();
+            //The following Getter Functions can be found in MMcEvtBasic.h
+        mPartId                 = mpMcEventMetaData->GetPartId();
+        mPartName               = mpMcEventMetaData->GetParticleName(mPartId);
+        mPartSymbol             = mpMcEventMetaData->GetParticleSymbol(mPartId);
+        mEnergy                 = mpMcEventMetaData->GetEnergy();
+        mImpact                 = mpMcEventMetaData->GetImpact();
+        mTelescopePhi           = mpMcEventMetaData->GetTelescopePhi();
+        mTelescopeTheta         = mpMcEventMetaData->GetTelescopeTheta();
+        mPhi                    = mpMcEventMetaData->GetParticlePhi();
+        mTheta                  = mpMcEventMetaData->GetParticleTheta();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read data from: MMcEvt" << endl;
+
+    // -----------------------------------------------------------------------
 
     //Get values from "MRawEventHeader" Branch
-    mEventNumber            = mpRawEventHeader->GetDAQEvtNumber();
-    mNumTriggerLvl1         = mpRawEventHeader->GetNumTrigLvl1();
-    mNumTriggerLvl2         = mpRawEventHeader->GetNumTrigLvl2();
+    if ( mpRawEventHeader != NULL)
+    {
+        mEventNumber            = mpRawEventHeader->GetDAQEvtNumber();
+        mNumTriggerLvl1         = mpRawEventHeader->GetNumTrigLvl1();
+        mNumTriggerLvl2         = mpRawEventHeader->GetNumTrigLvl2();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read data from: MRawEventHeader" << endl;
+
+    // -----------------------------------------------------------------------
 
     //Get values from "MCorsikaEvtHeader" Branch
-    mFirstInteractionHeight = mpCorsikaEvtHeader->GetFirstInteractionHeight();
-    mEvtReuse               = mpCorsikaEvtHeader->GetNumReuse();
-    mMomentumX              = mpCorsikaEvtHeader->GetMomentum().X();
-    mMomentumY              = mpCorsikaEvtHeader->GetMomentum().Y();
-    mMomentumZ              = mpCorsikaEvtHeader->GetMomentum().Z();
-    mZd                     = mpCorsikaEvtHeader->GetZd();
-    mAz                     = mpCorsikaEvtHeader->GetAz();
-    mX                      = mpCorsikaEvtHeader->GetX();
-    mY                      = mpCorsikaEvtHeader->GetY();
-
-//    mWeightedNumPhotons     = mpCorsikaEvtHeader->Get;
+    if ( mpCorsikaEvtHeader != NULL)
+    {
+        mFirstInteractionHeight = mpCorsikaEvtHeader->GetFirstInteractionHeight();
+        mEvtReuse               = mpCorsikaEvtHeader->GetNumReuse();
+        mMomentumX              = mpCorsikaEvtHeader->GetMomentum().X();
+        mMomentumY              = mpCorsikaEvtHeader->GetMomentum().Y();
+        mMomentumZ              = mpCorsikaEvtHeader->GetMomentum().Z();
+        mZd                     = mpCorsikaEvtHeader->GetZd();
+        mAz                     = mpCorsikaEvtHeader->GetAz();
+        mX                      = mpCorsikaEvtHeader->GetX();
+        mY                      = mpCorsikaEvtHeader->GetY();
+    }
+    else if (mVerbosityLvl > 2)
+        cout << "...could not read data from: MCorsikaEvtHeader" << endl;
+
+    // -----------------------------------------------------------------------
+
+    // mWeightedNumPhotons     = mpCorsikaEvtHeader->Get;
     // no getter function, no idea how to extract information
 
@@ -378,4 +508,6 @@
 {
     if (mVerbosityLvl > 1) cout << "...reading event raw data" << endl;
+
+    // -----------------------------------------------------------------------
 
     // delete Pixel Array before you set refferences for a new one
@@ -386,4 +518,12 @@
     mpPixel = new pixel_t[mNumberOfPixels];
 
+    // -----------------------------------------------------------------------
+
+    if ( mpRawEventData == NULL)
+    {
+        cout << "ERROR: cannot read event data" << endl;
+        return;
+    }
+
     mpRawEventData->InitRead(mpRawRunHeader);
 
@@ -391,7 +531,15 @@
 //    int pix_last_sample;
 
+    // -----------------------------------------------------------------------
+
     unsigned short* all_raw_data    = NULL;
 
-    all_raw_data            = (unsigned short*) mpRawEventData->GetSamples();
+    if ( mpRawEventData != NULL)
+    {
+        all_raw_data    = (unsigned short*) mpRawEventData->GetSamples();
+    }
+    else cout << "...cannot read event raw data" << endl;
+
+    // -----------------------------------------------------------------------
 
     if (mVerbosityLvl > 1)
@@ -407,7 +555,17 @@
         }
 
-
-        mpPixel[i].pedestal   = mpElectronicNoise[0][i].GetPedestal();
-        mpPixel[i].SoftId     = mpRawEventData->GetPixelIds()[i];
+        // Read Pedestal Offset
+        if ( mpElectronicNoise != NULL)
+        {
+            mpPixel[i].pedestal   = mpElectronicNoise[0][i].GetPedestal();
+        }
+        else cout << "...cannot read pedestal offset" << endl;
+
+        // Read Pixel SoftId
+        if ( mpRawEventData != NULL)
+        {
+            mpPixel[i].SoftId     = mpRawEventData->GetPixelIds()[i];
+        }
+        else cout << "...cannot read pixel id" << endl;
 
         pix_first_sample        = i*mNumberOfSamples;
@@ -415,18 +573,5 @@
         // point beginning of pixel's rawdata array to address
         // of pixel's first sample's adress
-
         mpPixel[i].rawData      = &(all_raw_data[pix_first_sample]);
-
-//        for (int j = pix_first_sample; j < pix_last_sample; j++)
-//        {
-//            cout << mpPixel[i].rawData[j] << " ";
-//        }
-//        cout << endl;
-
-//        // this could be improved by
-//         DONE BUT NOT TESTED
-//        for (int j = i*mSampleSize; j < (i+1)*mSampleSize; j++)
-//        {
-//        }
     }
 
@@ -459,4 +604,10 @@
 MonteCarlo::WriteMc2Csv(TString filename)
 {
+    if ( !mRootFileOpend ){
+        cout << "ERROR - no rootfile loaded, no data loaded, cannot write csv"
+             << endl;
+        return;
+    }
+
     cout << "...writing mc to csv: " << filename << endl;
     cout << "...processing " << mNumberOfEntries << "Events" << endl;
