Writing A List Of Data To A File

[code]double invmass= sqrt(pow((e0->energy()+e1->energy()),2.)-pow((e0->px()+e1->px()),2.)-pow((e0->py()+e1->py()),2.)-pow((e0->pz()+e1->pz()),2.));

 if ( preshower > 0 && preshower2 > 0)
    {
     h_Zmass->Fill(invmass);
     //h_Zmass->Fit("gaus");
     LogInfo("Demo")<<"Invariant Mass"<<invmass;
     ofstream myfile ("invmass.txt");
     if (myfile.is_open())
        {
         for (int i=0; i<800; i++)
             {
              myfile << invmass[i];
              myfile.close();
             }
        }
    }

[/code]

Why is this not working? It keeps giving me an error:

/afs/cern.ch/user/a/aayilara/CMSSW_4_2_5/src/Demo/DemoAnalyzer2/src/DemoAnalyzer2.cc:410: error: invalid types ‘double[int]’ for array subscript

Before I put in the ‘if’ loop, it would write 1 data and then keep over-writing until only the last data remained. I have 768 events here as invmass values, I need all of them into the text file :frowning:

I improved it slightly, but now it just gives me a long list of numbers with no spaces. I don’t know how to get the data from the invmass equation into an array for me to access:

[code]double invmass= sqrt(pow((e0->energy()+e1->energy()),2.)-pow((e0->px()+e1->px()),2.)-pow((e0->py()+e1->py()),2.)-pow((e0->pz()+e1->pz()),2.));

 int invmassarray [] = { invmass, };

 if ( preshower > 0 && preshower2 > 0)
    {
     h_Zmass->Fill(invmass);
     //h_Zmass->Fit("gaus");
     LogInfo("Demo")<<"Invariant Mass"<<invmass;
     ofstream myfile ("invmass.txt");
     if (myfile.is_open())
        {
         for (unsigned int i=0; i<800; i++)
             {
              myfile << invmassarray[i];
             }
         myfile.close();
        }
    }

[/code]

Please be aware it is 3am, I have 4 hours trying to do this. I’m a dumbass at programming but I’m trying my best here.

Hi,

int invmassarray [] = { invmass, };declares a fix size array of exactly one element but then for (unsigned int i=0; i<800; i++) { myfile << invmassarray[i]; } tries to store 800 elements in the array … leading to disaster.

Try:[code]double invmass= sqrt(pow((e0->energy()+e1->energy()),2.)-pow((e0->px()+e1->px()),2.)-pow((e0->py()+e1->py()),2.)-pow((e0->pz()+e1->pz()),2.));

 std::vector<int> invmassarray;

 if ( preshower > 0 && preshower2 > 0)
    {
     h_Zmass->Fill(invmass);
     //h_Zmass->Fit("gaus");
     LogInfo("Demo")<<"Invariant Mass"<<invmass;
     ofstream myfile ("invmass.txt");
     if (myfile.is_open())
        {
         for (unsigned int i=0; i<800; i++)
             {
              int readtmp;
              myfile << readtmp;
              invmassarray.push_back(readtmp);
             }
         myfile.close();
        }
    }[/code]

Cheers,
Philippe.

Thank you for your reply.

I don’t seem to be filling the invmass array with any data from the invmass equation. I’m looking at this by inspection, did you leave out a line?

Hi,

humm … I am confused. In order to make sense the snippet of code need to read data from the file:[code]double invmass= sqrt(pow((e0->energy()+e1->energy()),2.)-pow((e0->px()+e1->px()),2.)-pow((e0->py()+e1->py()),2.)-pow((e0->pz()+e1->pz()),2.));

 std::vector<int> invmassarray;

 if ( preshower > 0 && preshower2 > 0)
    {
     h_Zmass->Fill(invmass);
     //h_Zmass->Fit("gaus");
     LogInfo("Demo")<<"Invariant Mass"<<invmass;
     ifstream myfile ("invmass.txt");
     if (myfile.is_open())
        {
         for (unsigned int i=0; i<800; i++)
             {
              int readtmp;
              myfile >> readtmp;
              invmassarray.push_back(readtmp);
             }
         myfile.close();
        }
    }[/code]If the intent is to write to the file ... then I have not clue [b]what[/b] you are trying to write (i.e. where does the data come from) ....

Cheers,
Philippe.


This line gives calculated values.  I have plotted invmass in a histogram and I get a gaussian distribution with around 768 events.  Now I want all invmass values put into the array (invmassarray), so that I can write the array to a text file and then play with those values.  Basically I need some way of getting the values I calculate, into a text file.

invmass -> invmassarray -> textfile -> read invmass values

[b]The data that I want to write to the file comes from invmass.[/b]

This line gives calculated values. I have plotted invmass in a histogram and I get a gaussian distribution with around 768 events. Now I want all invmass values put into the array (invmassarray), so that I can write the array to a text file and then play with those values. Basically I need some way of getting the values I calculate, into a text file.

invmass -> invmassarray -> textfile -> read invmass values

The data that I want to write to the file comes from invmass.

I have managed to resolve the issue:

[code]double invmass= sqrt(pow((e0->energy()+e1->energy()),2.)-pow((e0->px()+e1->px()),2.)-pow((e0->py()+e1->py()),2.)-pow((e0->pz()+e1->pz()),2.));

 if ( preshower > 0 && preshower2 > 0)
    {
     h_Zmass->Fill(invmass);
     //h_Zmass->Fit("gaus");
     LogInfo("Demo")<<"Invariant Mass"<<invmass;
     ofstream myfile("invmass.txt", std::ios::app);
     if (myfile.is_open())
     {
         myfile << invmass << std::endl;
         myfile.close();
     }            
    }[/code]