Overwriting histogram

what should I do to the code if i dont want the previous histogram to be overwrite. especially the 1D.

// -*- c++ -*-

#ifndef RootWriter_H_
#define RootWriter_H_ 1

#include <string>
#include <memory>

class TH1;
class TH2;
class TH3;

typedef TH1* TH1p;
typedef TH2* TH2p;
typedef TH3* TH3p;

class Histogram1D;
class Histogram2D;
class Histogram3D;

typedef std::shared_ptr<Histogram1D> Histogram1Dp;
typedef std::shared_ptr<Histogram2D> Histogram2Dp;
typedef std::shared_ptr<Histogram3D> Histogram3Dp;

class Histograms;

//! Functions to write histograms into ROOT files.
 * \class RootWriter
 * \brief Class to write hitograms in ROOT files.
 * \details This class recives a list of histograms and writes them to a root file.
 * \author unknown
 * \copyright GNU Public License v. 3
class RootWriter {
    //! Write many histograms at once.
    /*! All of the histograms in the list will be written. The output
     *  file will be overwritten if it exists.
    static void Write( Histograms& histograms,     /*!< The histogram list. */
                       const std::string& filename /*!< The output filename. */);

    //! Create a ROOT histogram from a Histogram1D.
    /*! \return the ROOT 1D histogram.
    static TH1p CreateTH1(Histogram1Dp h /*!< The Histogram1D to be copied. */);

    //! Create a ROOT histogram from a Histogram2D.
    /*! \return the ROOT 2D histogram.
    static TH2p CreateTH2(Histogram2Dp h /*!< The Histogram2D to be copied. */);

    //! Create a ROOT histogram from a Histogram3D.
    /*! \return the ROOT 3D histogram.
    static TH3p CreateTH3(Histogram3Dp h /*!< The Histogram2D to be copied. */);

#endif /* RootWriter_H_ */

Hi Munirat,

I assume it would depent greatly of the implementation of ‘CreateTH1’ … without seeing the code I don’t even know how it currently ‘ovrerwrite’ the previous histogram …


The other strange thing is the usage of shared_ptr here:
static TH1p CreateTH1(Histogram1Dp h /*!< The Histogram1D to be copied. */);
What is this function doing exactly? Why would it take a shared pointer as parameter?
All the ...p typedefs are confusing. Some of them are just plain pointers, others are shared_ptrs - consistency? I would just avoid the typedefs here. If you want to work with smart pointers and you have a “create” function, then you should return a unique_ptr from the create function (and not take the smart pointers as input parameter). Often this isn’t necessary in ROOT though unless you do something like TH1::AddDirectory(false) or histo->SetDirectory(nullptr) or if you have temporary histograms. Anyway, if you do that, use unique_ptr instead of shared_ptr unless you really want to share ownership.

I didn’t actually write the code and it has been confusing and difficult to understand not talk more of editing
OfflineSorting.h (5.9 KB)
DetectorRead.h (13.5 KB)
TDRRoutine.h (7.3 KB)
Unpacker.h (4.3 KB)
RootWriter.h (1.6 KB)
UserSort.cpp (6.1 KB)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.