<commit_before>#include "gtest/gtest.h"
#include "ROOT/RHist.hxx"
#include "ROOT/RHistDrawable.hxx"
#include "ROOT/RCanvas.hxx"
#include "ROOT/RColor.hxx"
#include "ROOT/RFile.hxx"

#include "TFile.h"



#include <TApplication.h>

using namespace ROOT::Experimental;

int myArgc = 2;
const char* myArgv[] = {"app", "-b", nullptr};
TApplication theApp("iotest", &myArgc, const_cast<char**>(myArgv));

// Test drawing of histograms.
TEST(IOTest, OneD)
{
   TClass::GetClass("ROOT::Experimental::Detail::RHistImpl<ROOT::Experimental::Detail::RHistData<1,double,std::vector<double>,ROOT::Experimental::RHistStatContent,ROOT::Experimental::RHistStatUncertainty>,ROOT::Experimental::RAxisEquidistant>")->GetClassInfo();

   RAxisConfig xaxis{10, 0., 1.};
   RH1D h(xaxis);
   auto file = RFile::Recreate("IOTestOneD.root");
   file->Write("h", h);
}

// Drawing options:
TEST(IOTest, OneDOpts)
{
   RAxisConfig xaxis{10, 0., 1.};
   auto h = std::make_shared<RH1D>(xaxis);
   RCanvas canv;
   auto drawable1 = canv.Draw(h);
   drawable1->AttrLine().SetColor(RColor::kRed);
   auto drawable2 = canv.Draw(h);
   drawable2->AttrLine().SetColor(RColor::kBlue);

   EXPECT_EQ(canv.NumPrimitives(), 2u);
   EXPECT_NE(canv.GetPrimitive(0).get(), canv.GetPrimitive(1).get());

   auto pr1 = std::dynamic_pointer_cast<RHistDrawable<1>>(canv.GetPrimitive(0));
   auto pr2 = std::dynamic_pointer_cast<RHistDrawable<1>>(canv.GetPrimitive(1));
   EXPECT_NE(pr1.get(), nullptr);
   EXPECT_NE(pr2.get(), nullptr);
   if (pr1 && pr2) {
      EXPECT_NE(pr1->GetHist().get(), nullptr);
      EXPECT_NE(pr2->GetHist().get(), nullptr);
      EXPECT_EQ(pr1->GetHist().get(), pr2->GetHist().get());
   }

   {
      auto file = RFile::Recreate("IOTestOneDOpts.root");
      file->Write("canv", canv);
      file->Close();
   }

   {
      auto file2 = TFile::Open("IOTestOneDOpts.root");
      auto canv2 = file2->Get<ROOT::Experimental::RCanvas>("canv");
      EXPECT_NE(canv2, nullptr);

      if(canv2) {
         EXPECT_EQ(canv2->NumPrimitives(), 2u);
         EXPECT_NE(canv2->GetPrimitive(0).get(), canv2->GetPrimitive(1).get());
         auto dr1 = std::dynamic_pointer_cast<RHistDrawable<1>>(canv2->GetPrimitive(0));
         auto dr2 = std::dynamic_pointer_cast<RHistDrawable<1>>(canv2->GetPrimitive(1));
         EXPECT_NE(dr1.get(), nullptr);
         EXPECT_NE(dr2.get(), nullptr);
         if (dr1 && dr2) {
            EXPECT_NE(dr1->GetHist().get(), nullptr);
            EXPECT_NE(dr2->GetHist().get(), nullptr);
            EXPECT_EQ(dr1->GetHist().get(), dr2->GetHist().get());
         }
      }

      delete canv2;
      delete file2;
   }


}
<commit_msg>[hist7draw] improve I/O test for RCanvas with shared hist pointer<commit_after>
#include "gtest/gtest.h"
#include "ROOT/RHist.hxx"
#include "ROOT/RHistDrawable.hxx"
#include "ROOT/RCanvas.hxx"
#include "ROOT/RColor.hxx"
#include "ROOT/RFile.hxx"

#include "TFile.h"

using namespace ROOT::Experimental;

// Test storing of 1D histogram
TEST(IOTest, OneD)
{
   RAxisConfig xaxis{10, 0., 1.};
   RH1D h(xaxis);
   auto file = RFile::Recreate("IOTestOneD.root");
   file->Write("h", h);
}

// Test storing RCanvas with two RHistDrawable, referencing same histo
TEST(IOTest, OneDOpts)
{
   {
      RAxisConfig xaxis{10, 0., 1.};
      auto h = std::make_shared<RH1D>(xaxis);
      RCanvas canv;
      auto drawable1 = canv.Draw(h);
      drawable1->AttrLine().SetColor(RColor::kRed);
      auto drawable2 = canv.Draw(h);
      drawable2->AttrLine().SetColor(RColor::kBlue);

      EXPECT_EQ(canv.NumPrimitives(), 2u);
      EXPECT_NE(canv.GetPrimitive(0).get(), canv.GetPrimitive(1).get());

      auto pr1 = std::dynamic_pointer_cast<RHistDrawable<1>>(canv.GetPrimitive(0));
      auto pr2 = std::dynamic_pointer_cast<RHistDrawable<1>>(canv.GetPrimitive(1));
      ASSERT_NE(pr1, nullptr);
      ASSERT_NE(pr2.get(), nullptr);

      EXPECT_NE(pr1->GetHist().get(), nullptr);
      EXPECT_NE(pr2->GetHist().get(), nullptr);
      EXPECT_EQ(pr1->GetHist().get(), pr2->GetHist().get());

      auto file = RFile::Recreate("IOTestOneDOpts.root");
      file->Write("canv", canv);
      file->Close();
   }

   auto file2 = TFile::Open("IOTestOneDOpts.root");
   ASSERT_NE(file2, nullptr);
   auto canv2 = file2->Get<ROOT::Experimental::RCanvas>("canv");
   ASSERT_NE(canv2, nullptr);

   EXPECT_EQ(canv2->NumPrimitives(), 2u);
   EXPECT_NE(canv2->GetPrimitive(0).get(), canv2->GetPrimitive(1).get());

   auto dr1 = std::dynamic_pointer_cast<RHistDrawable<1>>(canv2->GetPrimitive(0));
   auto dr2 = std::dynamic_pointer_cast<RHistDrawable<1>>(canv2->GetPrimitive(1));
   ASSERT_NE(dr1, nullptr);
   ASSERT_NE(dr2, nullptr);

   EXPECT_EQ(dr1->AttrLine().GetColor(), RColor::kRed);
   EXPECT_EQ(dr2->AttrLine().GetColor(), RColor::kBlue);

   EXPECT_NE(dr1->GetHist(), nullptr);
   EXPECT_NE(dr2->GetHist(), nullptr);
   EXPECT_EQ(dr1->GetHist(), dr2->GetHist());

   delete canv2;
   delete file2;
}

{'fraction_non_alphanumeric': 0.1814687436779284, 'fraction_numerical': 0.024681367590532067, 'mean_word_length': 4.293361884368308, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6059058', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1861, 'n_words': 273}