Introducing cos modulation in a flat distribution

Please provide the following information:
5.34/34
Ubuntu 16.04

Hello
I have a flat distribution and I want to introduce a cos modulation in this (See figures). Is there any easier way to do?
What I have


What I want

Thank you :slight_smile:

Easier than what ? can you show what you are currently doing ?

Actually, I didn’t do anything. But it can be done by subtracting the ‘cos of angle’ from each subsequent bin and this lengthy process. I want some inbuilt function that can do the same job. Now, please suggest if you have some idea.

Maybe something like this:

{
  TH1F *h = new TH1F("h", "random cosine histo", 100, 0., TMath::TwoPi());
  TF1 *f = new TF1("f", "1.0 + 0.5 * TMath::Cos(2.0 * x)",
                   h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax());
#if 1 /* 0 or 1 */
  h->FillRandom("f", 10000000); // cosine
#else /* 0 or 1 */
  h->FillRandom("pol0", 10000000); // flat
  if (h->GetSumw2N() == 0) h->Sumw2(kTRUE); // ensure proper errors recalculation
  h->Multiply(f, 1.0); // available methods: Add, Divide, Multiply
#endif /* 0 or 1 */
  delete f; // no longer needed
  h->Draw("HIST");
}

@Wile_E_Coyote Thanks for your reply but my problem is I already have a flat histogram (which is generated by standard data) and now I need a random cos modulated histogram so that I can superimpose on the flat and my standard plot look like cos modulated.

  • My flat histogram has 100 bin in the range of 0-6.28

  • randomly generated cos histogram which has range 0 - 4*PI will be subtracted from it. For x=0 it will subtract cos 0.

  • As the x increases upto pi/2, cos pi should be subtracted from it.

  • For x = PI, cos 2PI and hence for x = 2PI, cos 4PI will be subtracted.
    So the flat curve will look like as I mentioned the cos modulated figure above.

Thanks :slight_smile:

ROOT Primer -> Histograms -> Add and Divide Histograms
ROOT User’s Guide -> Histograms -> Adding, Dividing, and Multiplying
ROOT User’s Guide -> Histograms -> Random Numbers and Histograms

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