# Pad with primitives painting during executing

**URL:** <https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093>\
**Category:** ROOT\
**Tags:** graphics, root\
**Created:** [August 7, 2022, 11:56am UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093 "2022-08-07T11:56:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 7, 2022, 11:56am UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/1 "2022-08-07T11:56:05Z")

</div>

\_ROOT Version: 6.18

Hello,

I try to paint pads with some primitives on a canvas. The following code really paint the primitives, but menus bar appears only after finishing the execution of a script. Also I can’t open the context menu on primitives and axes (although if I try to change axes range before execution finishing, the changing appears after finishing).  
Can I open the context menu and other things before execution finishing?

```auto
#include "root/TCanvas.h"
#include "root/TF1.h"
#include "root/TH1.h"
#include "root/TPad.h"
#include "root/TList.h"
#include "root/TCollection.h"

int canvas()
{
	TCanvas *c;
	TPad *p1, *p2;
	TList *list;
	TF1 *f1, *f2;
	int k;
	char strb[32];

	k = 1;
	f1 = new TF1("f1", "[0] * sin(x)", 0, 10);
	f1->SetParameter(0, k);
	f2 = new TF1("f1", "[0] * cos(x)", 0, 10);
	f2->SetParameter(0, k);
	sprintf(strb, "%s_%d", "canva", k);
	c = new TCanvas(strb, strb);
	p1 = new TPad("p1", "p1", 0.01, 0.01, 0.49, 0.99);
	p2 = new TPad("p2", "p2", 0.51, 0.01, 0.99, 0.99);
	p1->SetNumber(1);
	p2->SetNumber(2);
	p1->SetFrameFillColor(30);
	p2->SetFrameFillColor(29);
	p1->Draw();
	p2->Draw();
	c->cd(1);
	f1->DrawCopy();
	delete f1;
	c->cd(2);
	f2->DrawCopy();
	delete f2;

	list = p1->GetListOfPrimitives();
	{
		TIter next(list);
		TObject *obj;

		while ( (obj = next()) ) {
			sprintf(strb, "%s", obj->GetName());
			printf("primitive name: %s\n", strb);
		}
	}
	p1->Paint();
	c->Update();
	getchar();
	return (0);
}

```

Also, why if I do “p1-\>Update” instead of “c-\>Update()”, p2 painted too?  
Summarizing: I want too see on my screen the pad with the primitives when I want, but not after finishing the execution of a script.

Best regards,  
Nazar

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 8, 2022, 10:43am UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/2 "2022-08-08T10:43:33Z")

</div>

The Paint method `Paint()` of any primitive a not meant to be used by users’. programs except in some special cases. I your case I would suggest you replace:

```auto
   p1->Paint();
   c->Update();
   getchar();

```

by:

```auto
   c->Modifed();
   c->Update();
   getchar();

```

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 8, 2022, 3:14pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/3 "2022-08-08T15:14:53Z")

</div>

Thanks for answer, but I don’t see anything on my screen using `c->Modified()`.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 8, 2022, 3:40pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/4 "2022-08-08T15:40:13Z")

</div>

Indeed I do not really understand why you want to block your macro execution with this `getchar()`. The following macro works fine for me and you get the attached plot.

```auto
void canvas()
{
   TCanvas *c;
   TPad *p1, *p2;
   TList *list;
   TF1 *f1, *f2;
   int k;
   char strb[32];

   k = 1;
   f1 = new TF1("f1", "[0] * sin(x)", 0, 10);
   f1->SetParameter(0, k);
   f2 = new TF1("f1", "[0] * cos(x)", 0, 10);
   f2->SetParameter(0, k);
   sprintf(strb, "%s_%d", "canva", k);
   c = new TCanvas(strb, strb);
   p1 = new TPad("p1", "p1", 0.01, 0.01, 0.49, 0.99);
   p2 = new TPad("p2", "p2", 0.51, 0.01, 0.99, 0.99);
   p1->SetNumber(1);
   p2->SetNumber(2);
   p1->SetFrameFillColor(30);
   p2->SetFrameFillColor(29);
   p1->Draw();
   p2->Draw();
   c->cd(1);
   f1->DrawCopy();
   delete f1;
   c->cd(2);
   f2->DrawCopy();
   delete f2;

   list = p1->GetListOfPrimitives();
   {
      TIter next(list);
      TObject *obj;

      while ( (obj = next()) ) {
         sprintf(strb, "%s", obj->GetName());
         printf("primitive name: %s\n", strb);
      }
   }
}

```

 ![Screenshot 2022-08-08 at 17.39.10](https://root-forum.cern.ch/uploads/default/original/3X/9/2/92d82eac6a349406f3df090b1e44e87930632df3.png)

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 8, 2022, 3:52pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/5 "2022-08-08T15:52:33Z")

</div>

I have some database with many entries. I don’t want to call the function, that paint single entry, over and over again. So If I will have some mechanism to see an entry not finishing macro, It’s just easier to work for me.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 9, 2022, 6:11am UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/6 "2022-08-09T06:11:23Z")

</div>

Ok, try:

```auto
void canvas()
{
   TCanvas *c;
   TPad *p1, *p2;
   TF1 *f1, *f2;
   int k = 1;

   f1 = new TF1("f1", "[0] * sin(x)", 0, 10);
   f1->SetParameter(0, k);
   f2 = new TF1("f1", "[0] * cos(x)", 0, 10);
   f2->SetParameter(0, k);

   c = new TCanvas("c", "c");
   p1 = new TPad("p1", "p1", 0.01, 0.01, 0.49, 0.99);
   p2 = new TPad("p2", "p2", 0.51, 0.01, 0.99, 0.99);

   p1->SetFrameFillColor(30);
   p2->SetFrameFillColor(29);
   p1->Draw();
   p2->Draw();
   p1->cd(); f1->Draw();
   p2->cd(); f2->Draw();

   c->Modified();
   c->Update();
   gSystem->ProcessEvents();
   getchar();
}

```

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 9, 2022, 10:37am UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/7 "2022-08-09T10:37:47Z")

</div>

It works, but not every time.) Seems like it depends on previous session in root). So, I don’t understand what is going on).

Also, I noticed, that you remove `p1->SetNumber(1)` and replace `c->cd(1)` by `p1->cd()`. This two approach works different?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 9, 2022, 12:44pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/8 "2022-08-09T12:44:38Z")

</div>

> [@nazar\_sokour](#):
>
> It works, but not every time.) Seems like it depends on previous session in root). So, I don’t understand what is going on).

Weird, on which machine are you working ?

> [@nazar\_sokour](#):
>
> Also, I noticed, that you remove `p1->SetNumber(1)` and replace `c->cd(1)` by `p1->cd()`. This two approach works different?

It is similar but you do not really need to loop on the pads using numbers in your example. So the 2nd approach is more compact.

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 9, 2022, 12:48pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/9 "2022-08-09T12:48:11Z")

</div>

> [@couet](#):
>
> Weird, on which machine are you working ?

ubuntu 18

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 9, 2022, 12:53pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/10 "2022-08-09T12:53:39Z")

</div>

I tried to execute several times the macro `canvas.C`. It always works the same way.

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 9, 2022, 1:15pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/11 "2022-08-09T13:15:39Z")

</div>

Can you try this macro?

```auto
void canvas()
{
	TCanvas *canva;
	TPad *p1, *p2;
	TList *list;
	TF1 *f1, *f2;
	int k;
	char strb[32];
	char c;

	k = 1;
 L_again:
	f1 = new TF1("f1", "[0] * sin(x)", 0, 10);
	f1->SetParameter(0, k);
	f2 = new TF1("f1", "[0] * cos(x)", 0, 10);
	f2->SetParameter(0, k);

	sprintf(strb, "%s_%d", "canva", k);
	canva = new TCanvas(strb, strb);
	sprintf(strb, "%s_%d", "pad1", k);
	p1 = new TPad(strb, strb, 0.01, 0.01, 0.49, 0.99);
	sprintf(strb, "%s_%d", "pad2", k);
	p2 = new TPad(strb, strb, 0.51, 0.01, 0.99, 0.99);
	
	p1->SetFrameFillColor(30);
	p1->SetFillColor(20);
	p2->SetFrameFillColor(31);
	p2->SetFillColor(21);
	p1->Draw();
	p2->Draw();
	p1->cd();
	f1->DrawCopy();
	delete f1;
	p2->cd();
	f2->DrawCopy();
	delete f2;

	canva->Modified();
	canva->Update();
	gSystem->ProcessEvents();
	printf("enter char\n");
	c = getchar();
	getc(stdin);
	printf("char = '%c'\n", c);
	if ('q' == c)
		return;
		/* NOTREACHED */
	k++;
	goto L_again;
}

```

Please, try to do the next steps several times:

1. run root.
2. “.L canvas.cpp”
3. canvas()
4. enter “q” after “enter char” command
5. “.q”

Sometimes functions are painted on pads after the 3rd step, sometimes not. Or it’s always painted after 3rd step on you machine?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 9, 2022, 2:02pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/12 "2022-08-09T14:02:14Z")

</div>

> [@nazar\_sokour](#):
>
> Or it’s always painted after 3rd step on you machine?

Yes, it is always painted for me.

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 9, 2022, 2:05pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/13 "2022-08-09T14:05:07Z")

</div>

Ok, then I don’t know what to do. Maybe, I will try to build the latest version of root.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 9, 2022, 2:05pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/14 "2022-08-09T14:05:38Z")

</div>

> [@nazar\_sokour](#):
>
> Maybe, I will try to build the latest version of root.

May be

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 9, 2022, 2:06pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/15 "2022-08-09T14:06:01Z")

</div>

thanks for trying)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 9, 2022, 2:06pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/16 "2022-08-09T14:06:42Z")

</div>

Try with:  
`gSystem->ProcessEvents(); gSystem->ProcessEvents(); gSystem->ProcessEvents(); // aller guten Dinge sind drei`

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 9, 2022, 2:12pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/17 "2022-08-09T14:12:20Z")

</div>

Still doesn’t work every time. It works well 4 or 5 times in a row, and then not).

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 9, 2022, 2:32pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/18 "2022-08-09T14:32:26Z")

</div>

Try:  
`gSystem->Sleep(100); gSystem->ProcessEvents(); gSystem->Sleep(100); gSystem->ProcessEvents(); gSystem->Sleep(100); gSystem->ProcessEvents();`

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [August 9, 2022, 2:39pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/19 "2022-08-09T14:39:36Z")

</div>

It really helps) 10 times in a row it works).

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 9, 2022, 2:44pm UTC](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093/20 "2022-08-09T14:44:32Z")

</div>

What operating system version / compiler version are you using?

[Next page](https://root-forum.cern.ch/t/pad-with-primitives-painting-during-executing/51093.md?page=2)
