• Reading Numbers into an Array C++

    Miranda65M Member

    So I’m just starting out in C++ and I managed to convert an image into a text file which contains: x y intensity values.

    So this is an example of the file:

    
       1   1   0.00
       1   2   2.10
       1   3   0.00
       1   4  76.20
       1   5   0.00
       1   6   0.00
       1   7   0.00
    

    The code I’m writing is in C++ but is actually processed in root.

    The code I’ve started writing is:

    
    using std::cout;
    using std::endl;
    using std::string;
    
    string fOutFileName("gaintest.root");
    void Usage();
    void ParseArgs(int argc, char **argv); 
    
    
    int main(int argc, char * argv[])
    {
    
      ParseArgs(argc, argv);
    
    TH2D * p001_1 = new TH2D("p001_1","p001_1",640,0,640,480,0,480)
    
     std::ifstream file1("data_p30.dat");
    
      int x, y;
      double intensity;
    
      double getarray[640,480]
    
    if (file1.is_open()) {
        file1.seekg(0);
        while (!file1.eof()) {
          file1 >> x >> y >> intensity;
          p001_1->; //HELP HERE
        }
        file1.close();
      } else cout << "Error, cannot open file 1";
    }
    
    

    Now the part with "p001_1->" is where I'm tripping up, ultimately I'd like an array that looks something like this:

    myarray[x,y]=intensity
    

    where I can pick which x,y values I want for it to spit out the intensity value. The only catch is I need those x,y values to correspond to the values in the file. I know I'm close, but I just need a little help over the final hurdle =P

    THANKS =)

    P.S I've sort of found a solution:

    Code: Select all
    p001_1->SetBinContent(x,y,intensity);

    BUT this isn't brilliant for what I need it for next (taking certain areas of the picture and finding the difference of the intensities)

    EDIT:

    std::ifstream file1("data_p30.dat");
    
    //int x, y;
    //double intensity;
    
     //double getarray[640,480];
      int i;
      int j ;
      double c[1000,1000];
    if (file1.is_open()) {
        file1.seekg(0);
        while (!file1.eof()) {
          file1 >> i >> j >> c[i,j];
          cout<
    

    but then I get this error:

    
    46: error: expected `]' before ‘,’ token
    46: error: expected unqualified-id before numeric constant
    50: error: ‘c’ was not declared in this scope
    50:warning: left-hand operand of comma has no effect
    

    I appreciate you don't know the line numbers, but I think it really doesn't like the way I'm defining the variables

  • ShikhaTan Member

    So on http://root.cern.ch/root/html/src/TH2D.h.html#kvMDeC

    It seems to be saying there is a function for:

    virtual Double_t GetBinContent(Int_t binx, Int_t biny)
    

    So wouldn’t you be able to just use that instead of making the array of doubles?

    But if you really want to make the array of doubles, I don’t think you can initialize with

    c[1000,1000]
    

    I think you’d need to use

    c[1000][1000]
    

    You can see this in the error where it says the operand of comma has no effect and that it expects to see an end bracket

Viewing 1 reply thread
  • You must be logged in to reply to this topic.
en_USEnglish