HiLLiPOP.cc 46.5 KB
Newer Older
1
#include "HiLLiPOP.hh"
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
2

3 4 5 6
#include "cxxsupport/paramfile.h"
#include "cxxsupport/fitshandle.h"
#include "cxxsupport/cxxutils.h"
#include "Parser.hh"
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
7 8 9 10 11

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
12
#include<sstream>
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
13 14 15 16 17 18 19 20 21
#include <libgen.h> //for basename
using namespace std;

// For extra degree of freedom on calibration i.e. T ne P
// #define PolarizationEfficiency

// Output residual ranges. Full range is 50->2500 for TT, EE and TE at all frequencies
// #define FullRange

22
HiLLiPOP::HiLLiPOP(const string fileName)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
23 24 25 26 27 28 29
{
  planck_assert(file_present(fileName),string("missing file : ")+fileName);
  _name=string("HiLLiPOP:")+basename(const_cast<char*>(fileName.c_str()));
  Init(fileName);
}


30
HiLLiPOP::~HiLLiPOP()
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
31 32 33 34 35
{
  cout << "Chi2 from HiLLiPOP: " << _chi2 << " ndof: " << _ndof << endl;
}


36
void HiLLiPOP::Init(const string fileName)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
37 38 39 40
{
  cout << "Using: " << fileName << endl;
  paramfile parameters(fileName);

41
  
42 43 44
  string multipolesFile       = Parser::CheckPath(parameters.find<string>("MultipolesRange"));
  string SZFile               = Parser::CheckPath(parameters.find<string>("SZ"));
  string kSZFile              = Parser::CheckPath(parameters.find<string>("kSZ"));
45

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
46 47 48 49 50 51 52
  string pathToBeams          = parameters.find<string>("Beams");
  string pathToXSpectra       = parameters.find<string>("XSpectra");
  string pathToXSpectraErrors = parameters.find<string>("XSpectraErrors");
  string pathToCovMatrix      = parameters.find<string>("CovMatrix");
  string pathToCIBSpectra     = parameters.find<string>("CIB");
  string pathToSZxCIBSpectra  = parameters.find<string>("SZxCIB");
  string pathToDustSpectra    = parameters.find<string>("Dust");
53

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  _modeStatus[0]              = parameters.find<unsigned int>("TT");
  _modeStatus[1]              = parameters.find<unsigned int>("EE");
  _modeStatus[2]              = parameters.find<unsigned int>("BB");
  _modeStatus[3]              = parameters.find<unsigned int>("TE");
  _modeStatus[4]              = parameters.find<unsigned int>("ET");
  _nMap                       = parameters.find<int>("map");
  _fileOut                    = parameters.find<string>("FileOut","");

  char tmp[32];
  for(unsigned int i = 0; i < _nMap; i++) {sprintf(tmp,"freq%d",i); _freq.push_back(parameters.find<string>(tmp));}

  _nXSpectra = _nMap*(_nMap-1)/2;

  _typeStatus[0] = _modeStatus[0];
  _typeStatus[1] = _modeStatus[1];
  _typeStatus[2] = _modeStatus[2];
  if( _modeStatus[3] == 1 || _modeStatus[4] == 1) _typeStatus[3] = 1; else _typeStatus[3] = 0; 

  ProduceList();

  ProcessMultipoles(multipolesFile);

  ProcessBeamsEigenmodes(pathToBeams);

  ProcessXSpectra(pathToXSpectra);

  ProcessXSpectraErrors(pathToXSpectraErrors);

82
  ProcessNuisanceVariables(SZFile,pathToCIBSpectra,pathToDustSpectra,kSZFile,pathToSZxCIBSpectra);
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
83 84 85 86 87

  ProcessCovMatrix(pathToCovMatrix);
}


88
void HiLLiPOP::ProduceList()
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
  char tmp[32];
  vector<string>::const_iterator it;

  for(unsigned int i = 0; i < _nMap; i++) for(unsigned int j = i+1; j < _nMap; j++) {
    sprintf(tmp,"%sx%s",_freq[i].c_str(),_freq[j].c_str());
    it = find(_XFreq.begin(),_XFreq.end(),tmp);
    if( it == _XFreq.end() ) _XFreq.push_back(tmp);
  }

  _nXFreq = _XFreq.size();

  _XSpectra2Maps.resize(_nXSpectra);
  int k = 0;
  for(unsigned int i = 0; i < _nMap; i++) for(int j = i+1; j < _nMap; j++) {
    _XSpectra2Maps[k].resize(2);
    _XSpectra2Maps[k][0] = i;
    _XSpectra2Maps[k][1] = j;
    k++;
  }

  _XFreq2XSpectra.resize(_nXFreq);
  for(unsigned int c = 0; c < _nXSpectra; c++) {
    sprintf(tmp,"%sx%s",_freq[_XSpectra2Maps[c][0]].c_str(),_freq[_XSpectra2Maps[c][1]].c_str());
    it = find(_XFreq.begin(),_XFreq.end(),tmp);
    _XFreq2XSpectra[it-_XFreq.begin()].push_back(c);
  }

  _XFreq2Freq.resize(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
    _XFreq2Freq[f].resize(2);
    for(unsigned int i = 0; i < 2; i++) _XFreq2Freq[f][i] = _freq[_XSpectra2Maps[_XFreq2XSpectra[f][0]][i]];
  }
}


125
void HiLLiPOP::ProcessMultipoles(const string multipolesFile)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
126 127 128
{
  planck_assert(file_present(multipolesFile),string("missing file : ")+multipolesFile);

129 130
  string gModeName[] = {"TT","EE","BB","TE","ET"};

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  // Cross-spectra
  _lminXSpectra.resize(5);
  _lmaxXSpectra.resize(5);
  _XSpectraStatus.resize(5);

  fitshandle input;
  input.open(multipolesFile);

  unsigned int type = 0;

  for(unsigned int m = 0; m < 5; m++) {
    if( m < 4) type = m; else type = 3;

    input.goto_hdu(type+2); 

    _lminXSpectra[m].resize(_nXSpectra);
    _lmaxXSpectra[m].resize(_nXSpectra);

    double * lmin = new double[_nXSpectra];
    double * lmax = new double[_nXSpectra];

    input.read_column_raw(1,lmin,_nXSpectra);
    input.read_column_raw(2,lmax,_nXSpectra);

    for(unsigned int c = 0; c < _nXSpectra; c++) {
      _lminXSpectra[m][c] = lmin[c];
      _lmaxXSpectra[m][c] = lmax[c];
    }

    _maxOflmax[m] = *max_element(_lmaxXSpectra[m].begin(),_lmaxXSpectra[m].end());

    _XSpectraStatus[m].resize(_nXSpectra,0);
    if( _typeStatus[type] != 0 ) for(unsigned int c = 0; c < _nXSpectra; c++) if( lmin[c] != lmax[c] ) _XSpectraStatus[m][c] = 1;

    delete [] lmin;
    delete [] lmax;
  }
  input.close();

  // Cross-frequency
  _lminXFreq.resize(5);
  _lmaxXFreq.resize(5);
  _XFreqStatus.resize(5);

  for(unsigned int m = 0; m < 5; m++) {
    _lminXFreq[m].resize(_nXFreq,0);
    _lmaxXFreq[m].resize(_nXFreq,0);
178
    _XFreqStatus[m].resize(_nXFreq,0);
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
179 180 181 182
    for(unsigned int f = 0; f < _nXFreq; f++) {
      unsigned int lminXFreq = 0;
      unsigned int lmaxXFreq = 0;
      for(unsigned int c = 0; c < _XFreq2XSpectra[f].size(); c++) {
183 184 185 186 187 188 189 190 191 192
        if( _XSpectraStatus[m][_XFreq2XSpectra[f][c]] ) {
	  if( _XFreqStatus[m][f] == 0) {
	    lminXFreq = _lminXSpectra[m][_XFreq2XSpectra[f][c]];
	    lmaxXFreq = _lmaxXSpectra[m][_XFreq2XSpectra[f][c]];
	  } else {
	    lminXFreq = min(lminXFreq,_lminXSpectra[m][_XFreq2XSpectra[f][c]]);
	    lmaxXFreq = max(lmaxXFreq,_lmaxXSpectra[m][_XFreq2XSpectra[f][c]]);
	  }
	  _XFreqStatus[m][f] = 1;
	}
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
193 194 195 196 197
      }
      _lminXFreq[m][f] = lminXFreq;
      _lmaxXFreq[m][f] = lmaxXFreq;
    }
  }
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217


  for(unsigned int m = 0; m < 4; m++) {
    if( _typeStatus[m]) {
      cout << gModeName[m] << "XspectraStatus: ";
      for(unsigned int c = 0; c < _nXSpectra; c++) cout << " " << _XSpectraStatus[m][c];
      cout << endl;
      cout << gModeName[m] << " XFreqStatus: ";
      for(unsigned int c = 0; c < _nXFreq; c++) cout << " " << _XFreqStatus[m][c];
      cout << endl;
      cout << gModeName[m] << " lmin ";
      for( unsigned int f=0; f<_nXFreq; f++)
	cout << "\t" << _lminXFreq[m][f];
      cout << endl;
      cout << gModeName[m] << " lmax ";
      for( unsigned int f=0; f<_nXFreq; f++)
	cout << "\t" << _lmaxXFreq[m][f];
      cout << endl;
    }
  }
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
218 219 220
}


221
void HiLLiPOP::ProcessBeamsEigenmodes(const string pathToBeams)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
222 223 224
{
  _beamEigenmodes.resize(_nXSpectra);
  for(unsigned int c = 0; c < _nXSpectra; c++) {
225 226
    ostringstream os ;
    os << pathToBeams << "_" << _XSpectra2Maps[c][0] << "x" << _XSpectra2Maps[c][1] << ".fits";
227
    string beamFile=Parser::CheckPath(os.str());
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
228 229 230 231 232

    _beamEigenmodes[c].resize(_maxOflmax[0]+1,0);

    fitshandle * input = new fitshandle();
    input->open(beamFile);
233

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    input->goto_hdu(2);

    unsigned int size       = input->nelems(2);
    double * beamEigenmodes = new double[size];
    input->read_column_raw(3,beamEigenmodes,size);

    for(unsigned int l = 0; l <= _maxOflmax[0]; l++) _beamEigenmodes[c][l] = beamEigenmodes[l];

    input->close();

    delete [] beamEigenmodes;
    delete input;
  }
}


250
void HiLLiPOP::ProcessXSpectra(const string pathToXSpectra)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
251 252 253
{
  _ClData.resize(_nXSpectra);

254
  //char XSpectrumFile[1024];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
255 256
  for(unsigned int c = 0; c < _nXSpectra; c++) {
    // TT - EE - BB - TE
257 258 259 260 261
    //sprintf(XSpectrumFile,"%s_%d_%d.fits",pathToXSpectra.c_str(),_XSpectra2Maps[c][0],_XSpectra2Maps[c][1]);
    //planck_assert(file_present(XSpectrumFile),string("missing file : ")+XSpectrumFile);
    
    ostringstream os ;
    os << pathToXSpectra << "_" << _XSpectra2Maps[c][0] << "_" << _XSpectra2Maps[c][1] << ".fits";
262
    string XSpectrumFile=Parser::CheckPath(os.str());
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
263 264 265 266 267

    fitshandle * input_c1c2 = new fitshandle();
    input_c1c2->open(XSpectrumFile);

    // ET
268 269
    os.clear(); os.str("");
    os << pathToXSpectra << "_" << _XSpectra2Maps[c][1] << "_" << _XSpectra2Maps[c][0] << ".fits";
270
    XSpectrumFile=Parser::CheckPath(os.str());
271 272 273

    //sprintf(XSpectrumFile,"%s_%d_%d.fits",pathToXSpectra.c_str(),_XSpectra2Maps[c][1],_XSpectra2Maps[c][0]);
    //planck_assert(file_present(XSpectrumFile),string("missing file : ")+XSpectrumFile);
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

    fitshandle * input_c2c1 = new fitshandle();
    input_c2c1->open(XSpectrumFile);

    _ClData[c].resize(5*(_maxOflmax[0]+1),0);
    unsigned int type = 0;
    unsigned int size = 0;
    fitshandle * input;

    for(unsigned int m = 0; m < 5; m++) {
      if( m < 4) {type = m; input = input_c1c2;} else {type = 3; input = input_c2c1;}

      input->goto_hdu(type+2);
      size = input->nelems(1);

      double * ell = new double[size];
      double * Cl  = new double[size];

      input->read_column_raw(1,ell,size);
      input->read_column_raw(2,Cl,size);

      bool status  = true;
      if( ell[size-1]-_lmaxXSpectra[type][c] < 0 ) status = false;
      planck_assert(status,string("multipoles mismatch"));

      for(unsigned int l = 0; l <= _maxOflmax[0]-ell[0]; l++) _ClData[c][m*(_maxOflmax[0]+1)+ell[l]] = 2*M_PI/ell[l]/(ell[l]+1)*Cl[l]*1e12;

      delete [] ell;
      delete [] Cl;
      input = NULL;
    }
    input_c1c2->close();
    input_c2c1->close();

    delete input_c1c2;
    delete input_c2c1;
  }
}


314
void HiLLiPOP::ProcessXSpectraErrors(const string pathToXSpectraErrors)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
315 316 317
{
  _ClWeightData.resize(_nXSpectra);

318
  //char XSpectrumFile[1024];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
319 320
  for(unsigned int c = 0; c < _nXSpectra; c++) {
    // TT - EE - BB - TE
321 322 323 324 325 326
    //sprintf(XSpectrumFile,"%s_%d_%d.fits",pathToXSpectraErrors.c_str(),_XSpectra2Maps[c][0],_XSpectra2Maps[c][1]);
    //planck_assert(file_present(XSpectrumFile),string("missing file : ")+XSpectrumFile);

     
    ostringstream os ;
    os << pathToXSpectraErrors << "_" << _XSpectra2Maps[c][0] << "_" << _XSpectra2Maps[c][1] << ".fits";
327
    string XSpectrumFile=Parser::CheckPath(os.str());
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
328 329 330 331

    fitshandle * input_c1c2 = new fitshandle();
    input_c1c2->open(XSpectrumFile);

332

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
333
    // ET
334 335 336 337 338
    //sprintf(XSpectrumFile,"%s_%d_%d.fits",pathToXSpectraErrors.c_str(),_XSpectra2Maps[c][1],_XSpectra2Maps[c][0]);
    //planck_assert(file_present(XSpectrumFile),string("missing file : ")+XSpectrumFile);
    
    os.clear();os.str("");
    os << pathToXSpectraErrors << "_" << _XSpectra2Maps[c][1] << "_" << _XSpectra2Maps[c][0] << ".fits";
339
    XSpectrumFile=Parser::CheckPath(os.str());
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

    fitshandle * input_c2c1 = new fitshandle();
    input_c2c1->open(XSpectrumFile);

    _ClWeightData[c].resize(5*(_maxOflmax[0]+1),0);
    unsigned int type = 0;
    unsigned int size = 0;
    fitshandle * input;

    for(unsigned int m = 0; m < 5; m++) {
      if( m < 4) {type = m; input = input_c1c2;} else {type = 3; input = input_c2c1;}

      input->goto_hdu(type+2);
      size = input->nelems(1);

      double * ell   = new double[size];
      double * ClErr = new double[size];

      input->read_column_raw(1,ell,size);
      input->read_column_raw(3,ClErr,size);

      bool status  = true;
      if( ell[size-1]-_lmaxXSpectra[type][c] < 0 ) status = false;
      planck_assert(status,string("multipoles mismatch"));

      for(unsigned int l = 0; l <= _maxOflmax[0]-ell[0]; l++) _ClWeightData[c][m*(_maxOflmax[0]+1)+ell[l]] = pow(2*M_PI/ell[l]/(ell[l]+1)*ClErr[l]*1e12,-2); 

      delete [] ell;
      delete [] ClErr;
      input = NULL;
    }
    input_c1c2->close();
    input_c2c1->close();

    delete input_c1c2;
    delete input_c2c1;
  }
}


380
void HiLLiPOP::ProcessCovMatrix(const string pathToCovMatrix)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
381 382 383 384 385 386 387
{
  _invCovMat.resize(4);

  string gModeName[] = {"TT","EE","BB","TE","ET"};

  string covMatrixFile = pathToCovMatrix+'_';
  for(unsigned int m = 0; m < 5; m++) if( _modeStatus[m] == 1 ) covMatrixFile = covMatrixFile+gModeName[m];
388
  covMatrixFile = Parser::CheckPath(covMatrixFile+".fits");
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

  cout << "Reading " << covMatrixFile << endl;
  planck_assert(file_present(covMatrixFile),string("missing file : ")+covMatrixFile);

  // Begin: Mutipole bounds
  vector<vector<unsigned int> > lrangeXFreq(4);
  unsigned int side = 0;

  for(unsigned int m = 0; m < 4; m++) {
    if( _typeStatus[m] ) {
      lrangeXFreq[m].resize(_nXFreq+1,0);
      for(unsigned int f = 0; f < _nXFreq; f++) {
        if( f == 0 ) lrangeXFreq[m][f] = side;
        if( _XFreqStatus[m][f] ) lrangeXFreq[m][f+1] = lrangeXFreq[m][f]+_lmaxXFreq[m][f]-_lminXFreq[m][f]+1;
        else lrangeXFreq[m][f+1] = lrangeXFreq[m][f];
      }
      side = lrangeXFreq[m][_nXFreq];
    }
  }
  // End: Multipole bounds

410 411 412 413 414 415
//   cout << "side " << side << endl;
//   cout << "lrange ";
//   for( unsigned int f=0; f<_nXFreq; f++)
//     cout << "\t" << lrangeXFreq[0][f];
//   cout << endl;

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
416 417 418 419 420
  fitshandle * input = new fitshandle();
  input->open(covMatrixFile);
  input->goto_hdu(2);

  unsigned int size = input->nelems(1);
421
  if( size != long(side)*long(side)) { cout << "Wrong Matrix size ! (read:" << size << " need:" << side*side << ")" << endl; exit(-1);};
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
422

423
  double * cov = new double[size];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  input->read_column_raw(1,cov,size);

  for(unsigned int m1 = 0; m1 < 4; m1++) {
    if( _typeStatus[m1] == 1 ) {
      _invCovMat[m1].resize(4);
      for(unsigned int m2 = m1; m2 < 4; m2++) {
        if( _typeStatus[m2] == 1 ) {
          _invCovMat[m1][m2].resize(_nXFreq);
          for(unsigned int f1 = 0; f1 < _nXFreq; f1++) {
            if( _XFreqStatus[m1][f1] == 1 ) {
              _invCovMat[m1][m2][f1].resize(_nXFreq);
              for(unsigned int f2 = 0; f2 < _nXFreq; f2++) {
                if( _XFreqStatus[m2][f2] == 1 ) {
                  _invCovMat[m1][m2][f1][f2].resize(_lmaxXFreq[m1][f1]-_lminXFreq[m1][f1]+1);
                  for(unsigned int l1 = 0; l1 <= _lmaxXFreq[m1][f1]-_lminXFreq[m1][f1]; l1++) {
                    _invCovMat[m1][m2][f1][f2][l1].resize(_lmaxXFreq[m2][f2]-_lminXFreq[m2][f2]+1,0);
440 441
                    for(unsigned int l2 = 0; l2 <= _lmaxXFreq[m2][f2]-_lminXFreq[m2][f2]; l2++) 
		      _invCovMat[m1][m2][f1][f2][l1][l2] = 1.0/4.0/M_PI/M_PI*(_lminXFreq[m1][f1]+l1)*(_lminXFreq[m1][f1]+l1+1)*(_lminXFreq[m2][f2]+l2)*(_lminXFreq[m2][f2]+l2+1)*cov[(l1+lrangeXFreq[m1][f1])*side+(l2+lrangeXFreq[m2][f2])]/1e24;
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
442 443 444 445 446 447 448 449 450 451 452 453 454
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  delete [] cov;
  input->close();
  delete input;
455

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
456 457 458
}


459
void HiLLiPOP::ProcessNuisanceVariables(const string SZFile,const string pathToCIBSpectra,const string pathToDustSpectra,const string kSZFile,const string pathToSZxCIBSpectra)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
460 461 462 463
{
  string gFreqPlanckHFI[] = {"100","143","217","353","545","857"};

  char tmpPar[32];
464 465 466 467

  //char tmpFile[256];
  string tmpFile;

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  vector<string>::const_iterator it;
  vector<string> freqPlanckHFI(&gFreqPlanckHFI[0],&gFreqPlanckHFI[0]+5);

  // Begin: Calibration
  for(unsigned int i = 0; i < _nMap; i++) {sprintf(tmpPar,"c%d",i); _n.push_back(tmpPar);}
  // End: Calibration

  //SP 9/3/15: global calibration factor
  _n.push_back("A_planck");

  //SP comment
  // Begin: First beam eigenmode amplitudes
  //for(unsigned int c = 0; c < _nXSpectra; c++) {sprintf(tmpPar,"beta%d",c); _n.push_back(tmpPar);}
  // End: First beam eigenmode amplitudes


  // Begin: Amplitudes of the point sources power spectrum
  for(unsigned int f = 0; f < _nXFreq; f++) {sprintf(tmpPar,"Aps%s",_XFreq[f].c_str()); _n.push_back(tmpPar);}
  // End: Amplitudes of the point sources power spectrum


  // Begin : tSZ
  // frequency dependence (100,143,217,353,545,857) GHz of the SZ effect
  double fnu[6] = {-4.031, -2.785, 0.187, 6.205, 14.455, 26.335};
  _fnu.resize(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
    _fnu[f].resize(2);
    for(unsigned int i = 0; i < 2; i++) {
      it = find(freqPlanckHFI.begin(),freqPlanckHFI.end(),_XFreq2Freq[f][i].c_str());
      if( it != freqPlanckHFI.end() ) _fnu[f][i] = fnu[it-freqPlanckHFI.begin()];
    }
  }

Matthieu Tristram's avatar
Matthieu Tristram committed
501
  // tSZ spectrum template, in Dl=l(l+1)/2pi Cl, units uK at 143GHz
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  planck_assert(file_present(SZFile),string("missing file : ")+SZFile);

  fitshandle * SZInput = new fitshandle();
  SZInput->open(SZFile);
  SZInput->goto_hdu(2);

  unsigned int sizeSZ = SZInput->nelems(2);
  double * ellSZ      = new double[sizeSZ];
  double * Cl1haloSZ  = new double[sizeSZ];
  double * Cl2haloSZ  = new double[sizeSZ];
  SZInput->read_column_raw(1,ellSZ,sizeSZ);
  SZInput->read_column_raw(2,Cl1haloSZ,sizeSZ);
  SZInput->read_column_raw(3,Cl2haloSZ,sizeSZ);

  _ClSZ.resize(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
    _ClSZ[f].resize(_maxOflmax[0]+1,0);
    for(unsigned int l = 0; l <= _maxOflmax[0]-ellSZ[0]; l++) _ClSZ[f][ellSZ[l]] = 2*M_PI/ellSZ[l]/(ellSZ[l]+1)*(Cl1haloSZ[l]+Cl2haloSZ[l])*_fnu[f][0]*_fnu[f][1]/fnu[1]/fnu[1];
  }

  SZInput->close();

  delete [] ellSZ;
  delete [] Cl1haloSZ;
  delete [] Cl2haloSZ;
  delete SZInput;

  _n.push_back("Asz");
Matthieu Tristram's avatar
Matthieu Tristram committed
530
  // End : tSZ
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547


  // Begin : CIB
  // frequency dependence (100,143,217) GHz
  double gnu[3] = {244.059, 371.658, 483.485};
  _gnu.resize(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
    _gnu[f].resize(2);
    for(unsigned int i = 0; i < 2; i++) {
      it = find(freqPlanckHFI.begin(),freqPlanckHFI.end(),_XFreq2Freq[f][i].c_str());
      if( it != freqPlanckHFI.end() ) _gnu[f][i] = gnu[it-freqPlanckHFI.begin()];
    }
  }

  // CIB spectra
  _ClCIB.resize(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
548 549
    //sprintf(tmpFile,"%s_%s.fits",pathToCIBSpectra.c_str(),_XFreq[f].c_str());
    //planck_assert(file_present(tmpFile),string("missing file : ")+tmpFile);
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
550

551 552
    ostringstream os ;
    os << pathToCIBSpectra  << "_" << _XFreq[f] << ".fits";
553
    tmpFile=Parser::CheckPath(os.str());
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

    fitshandle * CIBInput = new fitshandle();
    CIBInput->open(tmpFile);
    CIBInput->goto_hdu(2);

    unsigned int sizeCIB = CIBInput->nelems(2);
    double * ellCIB      = new double[sizeCIB];
    double * Cl1haloCIB  = new double[sizeCIB];
    double * Cl2haloCIB  = new double[sizeCIB];
    CIBInput->read_column_raw(1,ellCIB,sizeCIB);
    CIBInput->read_column_raw(2,Cl1haloCIB,sizeCIB);
    CIBInput->read_column_raw(3,Cl2haloCIB,sizeCIB);

    _ClCIB[f].resize(_maxOflmax[0]+1);
    for(unsigned int l = 0; l <= _maxOflmax[0]-ellCIB[0]; l++) _ClCIB[f][ellCIB[l]] = (Cl1haloCIB[l]+Cl2haloCIB[l])/_gnu[f][0]/_gnu[f][1];

    CIBInput->close();

    delete [] ellCIB;
    delete [] Cl1haloCIB;
    delete [] Cl2haloCIB;
    delete CIBInput;
  }

  _n.push_back("Acib");
  // End : CIB


  // Begin : Dust
  // frequency dependence (100,143,217) GHz (I and Q/U)
  unsigned int index[5][2] = {{0,0},{1,1},{1,1},{0,1},{1,0}};
  double hnu[2][3] = {{0.01957,0.03982,0.13185},{0.01703,0.03605,0.12498}};

  _hnu.resize(5);
  for(unsigned int m = 0; m < 5; m++) {
    _hnu[m].resize(_nXFreq);
    for(unsigned int f = 0; f < _nXFreq; f++) {
      _hnu[m][f].resize(2);
      for(unsigned int i = 0; i < 2; i++) {
        it = find(freqPlanckHFI.begin(),freqPlanckHFI.end(),_XFreq2Freq[f][i].c_str());
        if( it != freqPlanckHFI.end() ) _hnu[m][f][i] = hnu[index[m][i]][it-freqPlanckHFI.begin()];
      }
    }
  }

  // Dust spectra
  _ClDust.resize(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
602 603 604 605 606
    //sprintf(tmpFile,"%s_%s.fits",pathToDustSpectra.c_str(),_XFreq[f].c_str());
    //planck_assert(file_present(tmpFile),string("missing file : ")+tmpFile);
    
    ostringstream os ;
    os << pathToDustSpectra << "_" << _XFreq[f] << ".fits";
607
    tmpFile=Parser::CheckPath(os.str());
608

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

    _ClDust[f].resize(5*(_maxOflmax[0]+1),0);

    fitshandle * DustInput = new fitshandle();
    DustInput->open(tmpFile);
    DustInput->goto_hdu(2);

    unsigned int sizeDust = DustInput->nelems(2);
    for(unsigned int m = 0; m < 5; m++) {
      double * ellDust = new double[sizeDust];
      double * ClDust  = new double[sizeDust];

      DustInput->read_column_raw(1,ellDust,sizeDust);
      DustInput->read_column_raw(m+2,ClDust,sizeDust);

      for(unsigned int l = 0; l <= _maxOflmax[0]-ellDust[0]; l++) _ClDust[f][m*(_maxOflmax[0]+1)+ellDust[l]] = _hnu[m][f][0]*_hnu[m][f][1]*ClDust[l];

      delete [] ellDust;
      delete [] ClDust;
    }
    DustInput->close();

    delete DustInput;
  }
  _n.push_back("AdustTT");
  _n.push_back("AdustPP");
  _n.push_back("AdustTP");
  // End: Dust


  // Begin : kSZ
  planck_assert(file_present(kSZFile),string("missing file : ")+kSZFile);

  fitshandle * kSZInput = new fitshandle();
  kSZInput->open(kSZFile);
  kSZInput->goto_hdu(2);

Matthieu Tristram's avatar
Matthieu Tristram committed
646
  //kSZ template, Dl=l(l+1)/2pi Cl, units uK
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
  unsigned int sizekSZ = kSZInput->nelems(2);
  double * ellkSZ = new double[sizekSZ];
  double * ClkSZ  = new double[sizekSZ];
  kSZInput->read_column_raw(1,ellkSZ,sizekSZ);
  kSZInput->read_column_raw(2,ClkSZ,sizekSZ);

  _ClkSZ.resize(_maxOflmax[0]+1,0);
  for(unsigned int l = 0; l <= _maxOflmax[0]-ellkSZ[0]; l++) _ClkSZ[ellkSZ[l]] = 2*M_PI/ellkSZ[l]/(ellkSZ[l]+1)*ClkSZ[l];

  kSZInput->close();

  delete [] ellkSZ;
  delete [] ClkSZ;
  delete kSZInput;

  _n.push_back("Aksz");
  // End : kSZ


  // Begin: tSZxCIB spectra
  _ClSZxCIB.resize(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
669 670 671 672 673
    //sprintf(tmpFile,"%s_%s.fits",pathToSZxCIBSpectra.c_str(),_XFreq[f].c_str());
    //planck_assert(file_present(tmpFile),string("missing file : ")+tmpFile);

    ostringstream os ;
    os <<  pathToSZxCIBSpectra  << "_" << _XFreq[f] << ".fits";
674
    tmpFile=Parser::CheckPath(os.str());
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707


    fitshandle * SZxCIBInput = new fitshandle();
    SZxCIBInput->open(tmpFile);
    SZxCIBInput->goto_hdu(2);

    unsigned int sizeSZxCIB = SZxCIBInput->nelems(2);
    double * ellSZxCIB      = new double[sizeSZxCIB];
    double * ClSZxCIB       = new double[sizeSZxCIB];
    SZxCIBInput->read_column_raw(1,ellSZxCIB,sizeSZxCIB);
    SZxCIBInput->read_column_raw(2,ClSZxCIB,sizeSZxCIB);

    _ClSZxCIB[f].resize(_maxOflmax[0]+1);
    for(unsigned int l = 0; l <= _maxOflmax[0]-ellSZxCIB[0]; l++) _ClSZxCIB[f][ellSZxCIB[l]] = ClSZxCIB[l]/_gnu[f][0]/_gnu[f][1];

    SZxCIBInput->close();

    delete [] ellSZxCIB;
    delete [] ClSZxCIB;
    delete SZxCIBInput;
  }

  _n.push_back("Aszxcib");
  // End : SZxCIB

#ifdef PolarizationEfficiency
  // Begin: Polarization efficiency
  for(unsigned int i = 0; i < _nMap; i++) {sprintf(tmpPar,"epsilon%d",i); _n.push_back(tmpPar);}
  // End: Polarization efficiency
#endif
}


708
double HiLLiPOP::computeLikelihood(const vector<unsigned int>& l,vector<double>& cltt,vector<double>& clte,vector<double>& clee,vector<double>& clbb,vector<double>& nuisance)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
709 710 711 712 713 714 715 716 717 718 719
{
  // Reshaping the theoretical CMB power spectra
  vector<double> ClCMB(5*(_maxOflmax[0]+1),0);
  for(unsigned int i = 0; i <= _maxOflmax[0]-l[0]; i++) {
    ClCMB[0*(_maxOflmax[0]+1)+l[i]] = cltt[i];
    ClCMB[1*(_maxOflmax[0]+1)+l[i]] = clee[i];
    ClCMB[2*(_maxOflmax[0]+1)+l[i]] = clbb[i];
    ClCMB[3*(_maxOflmax[0]+1)+l[i]] = clte[i];
    ClCMB[4*(_maxOflmax[0]+1)+l[i]] = clte[i];
  }

720 721 722 723 724 725 726
  //Compute Residuals
  computeResiduals( ClCMB, nuisance, false);

  double chi2 = 0;
  double ndof = 0;

/*
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
727 728 729 730 731 732 733 734 735 736
  // Begin: gathering information regarding the parameters
  char tmpPar[32];
  vector<string>::const_iterator it;

  // Calibration
  vector<double> cal(_nMap);
  for(unsigned int i = 0; i < _nMap; i++) cal[i] = nuisance[i];

  //SP
  const double Aplanck=nuisance[getIndex("A_planck")];
737

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
738
  // First beam eigenmode amplitude
739 740
//  vector<double> beta(_nXSpectra);
//  for(unsigned int c = 0; c < _nXSpectra; c++) beta[c] = nuisance[_nMap+c];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
741 742 743 744 745 746

  // Amplitudes of the point sources
  vector<double> Aps(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
    sprintf(tmpPar,"Aps%s",_XFreq[f].c_str());
    it = find(_n.begin(),_n.end(),tmpPar);
747
    if( it != _n.end() ) Aps[f] = nuisance[it-_n.begin()]; else {cout << "HiLLiPOP::computeLikelihood. Aps mismatch" << endl; exit(0);}
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
  }

  // tSZ amplitude
  double Asz = nuisance[++it-_n.begin()];

  // CIB amplitude
  double Acib = nuisance[++it-_n.begin()];

  // Dust amplitude
  double AdustTT = nuisance[++it-_n.begin()];
  double AdustPP = nuisance[++it-_n.begin()];
  double AdustTP = nuisance[++it-_n.begin()];

  // kSZ amplitude
  double Aksz = nuisance[++it-_n.begin()];

  // tSZxCIB amplitude
  double Aszxcib = nuisance[++it-_n.begin()];

#ifdef PolarizationEfficiency
  // Polarization efficiency
  vector<double> epsilon(_nMap);
  for(unsigned int i = 0; i < _nMap; i++) epsilon[i] = nuisance[++it-_n.begin()];
#endif
  // End: gathering information regarding the parameters

  unsigned int modeOffset = 0;

  // Begin: Residuals
  vector<vector<vector<double> > > residual(5), weight(5);
  for(unsigned int m = 0; m < 5; m++) {
    residual[m].resize(_nXFreq); weight[m].resize(_nXFreq);
    for(unsigned int f = 0; f < _nXFreq; f++) {residual[m][f].resize(_maxOflmax[0]+1,0); weight[m][f].resize(_maxOflmax[0]+1,0);}
  }

  for(unsigned int m = 0; m < 5; m++) {
    if( _modeStatus[m] ) {
      modeOffset = m*(_maxOflmax[0]+1);
      for(unsigned int f = 0; f < _nXFreq; f++) {
        if( _XFreqStatus[m][f] ) {
          for(unsigned int l = _lminXFreq[m][f]; l <= _lmaxXFreq[m][f]; l++) {
            double foregrounds = 0;
            if( m == 0 ) foregrounds = Aps[f]+Asz*_ClSZ[f][l]+Acib*_ClCIB[f][l]+AdustTT*_ClDust[f][modeOffset+l]+Aksz*_ClkSZ[l]+Aszxcib*_ClSZxCIB[f][l];
            if( m == 1 || m == 2 ) foregrounds = AdustPP*_ClDust[f][modeOffset+l];
            if( m == 3 || m == 4 ) foregrounds = AdustTP*_ClDust[f][modeOffset+l];

            double tmpCal, tmpBeamEigenmodes, tmpWeight, tmpPolEffPP, tmpPolEffTP, tmpPolEffPT = 0.;
            for(unsigned int c = 0; c < _XFreq2XSpectra[f].size(); c++) {
              if( _XSpectraStatus[m][_XFreq2XSpectra[f][c]] && l >= _lminXSpectra[m][_XFreq2XSpectra[f][c]] && l <= _lmaxXSpectra[m][_XFreq2XSpectra[f][c]] ) {
                tmpWeight = _ClWeightData[_XFreq2XSpectra[f][c]][modeOffset+l];
                //SP: tmpBeamEigenmodes = pow(1+beta[_XFreq2XSpectra[f][c]]*_beamEigenmodes[_XFreq2XSpectra[f][c]][l],2);
                tmpBeamEigenmodes = 1.;
                tmpCal = 1+cal[_XSpectra2Maps[_XFreq2XSpectra[f][c]][0]]+cal[_XSpectra2Maps[_XFreq2XSpectra[f][c]][1]];
801 802
		//SP 15/09/15: au carre
		tmpCal*=(Aplanck*Aplanck);
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
#ifdef PolarizationEfficiency
                if( m == 0 ) residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                if( m == 1 || m == 2 ) {
                  tmpPolEffPP = (1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][0]])*(1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][1]]);
                  residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpPolEffPP*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                }
                if( m == 3 ) {
                  tmpPolEffTP = 1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][1]];
                  residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpPolEffTP*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                }
                if( m == 4 ) {
                  tmpPolEffPT = 1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][0]];
                  residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpPolEffPT*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                }
#else
                residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
#endif
                weight[m][f][l] += tmpWeight;
              }
            }
          }
        }
      }
    }
  }

  for(unsigned int m = 0; m < 4; m++) {
    if( _typeStatus[m] ) {
      for(unsigned int f = 0; f < _nXFreq; f++) {
        if( _XFreqStatus[m][f] ) {
          for(unsigned int l = _lminXFreq[m][f]; l <= _lmaxXFreq[m][f]; l++) {
            if( m < 3) residual[m][f][l] /= weight[m][f][l];
            else residual[3][f][l]  = (residual[3][f][l]+residual[4][f][l])/(weight[3][f][l]+weight[4][f][l]);
          }
        }
      }
    }
  }
  residual.resize(4);
  // End: Residuals

844
*/
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
845 846 847 848 849 850 851 852 853 854

  // Begin: chi2
  // diagonal
  for(unsigned int m = 0; m < 4; m++) {
    if( _typeStatus[m] ) {
      for(unsigned int f1 = 0; f1 < _nXFreq; f1++) {
        if( _XFreqStatus[m][f1] ) {
          for(unsigned int l1 = _lminXFreq[m][f1]; l1 <= _lmaxXFreq[m][f1]; l1++) {
            ndof++;
            for(unsigned int l2 = _lminXFreq[m][f1]; l2 <= _lmaxXFreq[m][f1]; l2++) {
855
              chi2 += _residual[m][f1][l1]*_invCovMat[m][m][f1][f1][l1-_lminXFreq[m][f1]][l2-_lminXFreq[m][f1]]*_residual[m][f1][l2];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
856 857 858 859 860 861
            }
          }
          for(unsigned int f2 = f1+1; f2 < _nXFreq; f2++) {
            if( _XFreqStatus[m][f2] ) {
              for(unsigned int l1 = _lminXFreq[m][f1]; l1 <= _lmaxXFreq[m][f1]; l1++) {
                for(unsigned int l2 = _lminXFreq[m][f2]; l2 <= _lmaxXFreq[m][f2]; l2++ ) {
862
                  chi2 += 2*_residual[m][f1][l1]*_invCovMat[m][m][f1][f2][l1-_lminXFreq[m][f1]][l2-_lminXFreq[m][f2]]*_residual[m][f2][l2];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
                }
              }
            }
          }
        }
      }
    }
  }

  // off-diagonal
  for(unsigned int m1 = 0; m1 < 3; m1++) {
    if( _typeStatus[m1] ) {
      for(unsigned int m2 = m1+1; m2 < 4; m2++) {
        if( _typeStatus[m2] ) {
          for(unsigned int f1 = 0; f1 < _nXFreq; f1++) {
            if( _XFreqStatus[m1][f1] ) {
              for(unsigned int f2 = 0; f2 < _nXFreq; f2++) {
                if( _XFreqStatus[m2][f2] ) {
                  for(unsigned int l1 = _lminXFreq[m1][f1]; l1 <= _lmaxXFreq[m1][f1]; l1++) {
                    for(unsigned int l2 = _lminXFreq[m2][f2]; l2 <= _lmaxXFreq[m2][f2]; l2++) {
883
                      chi2 += 2*_residual[m1][f1][l1]*_invCovMat[m1][m2][f1][f2][l1-_lminXFreq[m1][f1]][l2-_lminXFreq[m2][f2]]*_residual[m2][f2][l2];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  // End: chi2

  _chi2 = chi2;
  _ndof = ndof;

899 900 901 902 903 904 905 906 907 908 909
  if( _fileOut != "" ) {
    _residual.clear();
    _instrumental.clear();
    _weight.clear();
    computeResiduals( ClCMB, nuisance, true);
    WriteOutput( ClCMB, nuisance);
  }

  _residual.clear();
  _instrumental.clear();
  _weight.clear();
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
910 911 912 913 914 915

  _lnlkl= _chi2/2.;
  return _chi2/2.;
}


916
void HiLLiPOP::dump() const
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
{
  string CMBmode[5] = {"TT","EE","BB","TE"};

  cout << endl;
  cout << "************************* SET UP *************************" << endl;
  cout << "Total number of XSpectra = " << _nXSpectra << endl << endl;
  for(unsigned int i = 0; i < _nXSpectra; i++) {
    cout << "XSpectrum_" << _XSpectra2Maps[i][0] << "x" << _XSpectra2Maps[i][1] << " :" << endl;
    for(unsigned j = 0; j < 4; j++) cout << CMBmode[j] << " (status = " << _XSpectraStatus[j][i] << ") [" << _lminXSpectra[j][i] << "," << _lmaxXSpectra[j][i] << "]" << endl;
    cout << endl;
  }

  cout << "Total number of XFrequency = " << _nXFreq << endl << endl;
  for(unsigned int i = 0; i < _nXFreq; i++) {
    cout << "XFreq_" << _XFreq[i] << " :" << endl;
    for(unsigned j = 0; j < 4; j++) cout << CMBmode[j] << " (status = " << _XFreqStatus[j][i] << ") [" << _lminXFreq[j][i] << "," << _lmaxXFreq[j][i] << "]" << endl;
    cout << endl;
  }
  cout << "**********************************************************" << endl;
}


939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120

void HiLLiPOP::computeResiduals( vector<double> ClCMB, vector<double>& nuisance, bool fullRange)
{
  unsigned int lminXFreq, lmaxXFreq, lminXSpectra, lmaxXSpectra = 0;
  double tmpBeamEigenmodes = 1.;
  double foregrounds = 0;
  unsigned int modeOffset = 0;
  char tmpPar[32];

  _residual.resize(5);
  _instrumental.resize(5);
  _weight.resize(5);

  // Begin: gathering information regarding the parameters

  // Calibration
  vector<double> cal(_nMap);
  for(unsigned int i = 0; i < _nMap; i++) cal[i] = nuisance[i];

  //Absolute Calibration
  const double Aplanck=nuisance[getIndex("A_planck")];

#ifdef BeamEigenmodes
  // First beam eigenmode amplitude
  vector<double> beta(_nXSpectra);
  for(unsigned int c = 0; c < _nXSpectra; c++) beta[c] = nuisance[_nMap+c];
#endif

  // Amplitudes of the point sources
  vector<double> Aps(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
    sprintf(tmpPar,"Aps%s",_XFreq[f].c_str());
    Aps[f] = nuisance[getIndex(tmpPar)];
  }

  // tSZ amplitude
  double Asz = nuisance[getIndex("Asz")];

  // CIB amplitude
  double Acib = nuisance[getIndex("Acib")];

  // Dust amplitude
  double AdustTT = nuisance[getIndex("AdustTT")];
  double AdustPP = nuisance[getIndex("AdustPP")];
  double AdustTP = nuisance[getIndex("AdustTP")];

  // kSZ amplitude
  double Aksz = nuisance[getIndex("Aksz")];

  // tSZxCIB amplitude
  double Aszxcib = nuisance[getIndex("Aszxcib")];

#ifdef PolarizationEfficiency
  // Polarization efficiency
  vector<double> epsilon(_nMap);
  for(unsigned int i = 0; i < _nMap; i++) epsilon[i] = nuisance[getIndex("epsilon"+str(i))];
#endif
  // End: gathering information regarding the parameters


  // Init
  for(unsigned int m = 0; m < 5; m++) {
    _residual[m].resize(_nXFreq); _weight[m].resize(_nXFreq); _instrumental[m].resize(_nXFreq);
    for(unsigned int f = 0; f < _nXFreq; f++) {
          _residual[m][f].resize(_maxOflmax[0]+1,0);
            _weight[m][f].resize(_maxOflmax[0]+1,0);
      _instrumental[m][f].resize(_maxOflmax[0]+1,0);
    }
  }

  lminXFreq = lminXSpectra = 10;
  lmaxXFreq = lmaxXSpectra = 2500;
//   lminXFreq = 50;
//   lmaxXFreq = 2500;
//   lminXSpectra = 50;
//   lmaxXSpectra = 2500;

  // Begin: Residuals
  double tmpCal, tmpWeight;
#ifdef PolarizationEfficiency
  double tmpPolEffPP, tmpPolEffTP, tmpPolEffPT = 0.;
#endif
  for(unsigned int m = 0; m < 5; m++) {
    if( _modeStatus[m] ) {
      modeOffset = m*(_maxOflmax[0]+1);
      for(unsigned int f = 0; f < _nXFreq; f++) {
        if( _XFreqStatus[m][f] ) {

	  if( !fullRange) {
	    lminXFreq = _lminXFreq[m][f];
	    lmaxXFreq = _lmaxXFreq[m][f];
	  }
	  
          for(unsigned int l = lminXFreq; l <= lmaxXFreq; l++) {
            if( m == 0 ) foregrounds = Aps[f]+Asz*_ClSZ[f][l]+Acib*_ClCIB[f][l]+AdustTT*_ClDust[f][modeOffset+l]+Aksz*_ClkSZ[l]+Aszxcib*_ClSZxCIB[f][l];
            if( m == 1 || m == 2 ) foregrounds = AdustPP*_ClDust[f][modeOffset+l];
            if( m == 3 || m == 4 ) foregrounds = AdustTP*_ClDust[f][modeOffset+l];

            for(unsigned int c = 0; c < _XFreq2XSpectra[f].size(); c++) {
	      if( !fullRange) { 
		lminXSpectra = _lminXSpectra[m][_XFreq2XSpectra[f][c]];
		lmaxXSpectra = _lmaxXSpectra[m][_XFreq2XSpectra[f][c]];
	      }

              if( _XSpectraStatus[m][_XFreq2XSpectra[f][c]] && l >= lminXSpectra && l <= lmaxXSpectra ) {
                tmpWeight = _ClWeightData[_XFreq2XSpectra[f][c]][modeOffset+l];
#ifdef BeamEigenmodes
                tmpBeamEigenmodes = pow(1+beta[_XFreq2XSpectra[f][c]]*_beamEigenmodes[_XFreq2XSpectra[f][c]][l],2);
#endif
                tmpCal = (Aplanck*Aplanck) * (1+cal[_XSpectra2Maps[_XFreq2XSpectra[f][c]][0]]+cal[_XSpectra2Maps[_XFreq2XSpectra[f][c]][1]]);
#ifdef PolarizationEfficiency
                if( m == 0 ) {
		  _residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                  _instrumental[m][f][l] += tmpWeight*tmpCal*tmpBeamEigenmodes;
                }
                if( m == 1 || m == 2 ) {
                  tmpPolEffPP = (1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][0]])*(1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][1]]);
                  _residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpPolEffPP*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                  _instrumental[m][f][l] += tmpWeight*tmpCal*tmpPolEffPP*tmpBeamEigenmodes;
                }
                if( m == 3 ) {
                  tmpPolEffTP = 1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][1]];
                  _residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpPolEffTP*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                  _instrumental[m][f][l] += tmpWeight*tmpCal*tmpPolEffTP*tmpBeamEigenmodes; 
                }
                if( m == 4 ) {
                  tmpPolEffPT = 1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][0]];
                  _residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpPolEffPT*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                  _instrumental[m][f][l] += tmpWeight*tmpCal*tmpPolEffPT*tmpBeamEigenmodes;
                }
#else
                _residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                _instrumental[m][f][l] += tmpWeight*tmpCal*tmpBeamEigenmodes;
#endif
                _weight[m][f][l] += tmpWeight;
              }
            }
          }
        }
      }
    }
  }

  for(unsigned int m = 0; m < 4; m++) {
    if( _typeStatus[m] ) {
      for(unsigned int f = 0; f < _nXFreq; f++) {
        if( _XFreqStatus[m][f] ) {
	  if( !fullRange) {
	    lminXFreq = _lminXFreq[m][f];
	    lmaxXFreq = _lmaxXFreq[m][f];
	  }
          for(unsigned int l = lminXFreq; l <= lmaxXFreq; l++) {
            if( m < 3) {
              _instrumental[m][f][l] /= _weight[m][f][l];
	      _residual[m][f][l] /= _weight[m][f][l];
	    }
            else {
              _instrumental[3][f][l]  = (_instrumental[3][f][l]+_instrumental[4][f][l])/(_weight[3][f][l]+_weight[4][f][l]);
	      _residual[3][f][l]  = (_residual[3][f][l]+_residual[4][f][l])/(_weight[3][f][l]+_weight[4][f][l]);
	    }
          }
        }
      }
    }
  }
  _residual.resize(4);
  _instrumental.resize(4);
  // End: Residuals

}












1121
void HiLLiPOP::WriteOutput(const vector<double>& ClCMB,const vector<double>& nuisance)
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1122
{
1123 1124 1125

/*

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
  // Begin: gathering information regarding the parameters
  char tmpPar[32];
  vector<string>::const_iterator it;

  // Calibration
  vector<double> cal(_nMap);
  for(unsigned int i = 0; i < _nMap; i++) cal[i] = nuisance[i];

  // First beam eigenmode amplitude
  vector<double> beta(_nXSpectra);
  for(unsigned int c = 0; c < _nXSpectra; c++) beta[c] = nuisance[_nMap+c];

  // Amplitudes of the point sources
  vector<double> Aps(_nXFreq);
  for(unsigned int f = 0; f < _nXFreq; f++) {
    sprintf(tmpPar,"Aps%s",_XFreq[f].c_str());
    it = find(_n.begin(),_n.end(),tmpPar);
    if( it != _n.end() ) Aps[f] = nuisance[it-_n.begin()];
1144
    else {cout << "HiLLiPOP::WriteOutput. Aps mismatch" << endl; exit(0);}
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  }

  // tSZ amplitude
  double Asz = nuisance[++it-_n.begin()];

  // CIB amplitude
  double Acib = nuisance[++it-_n.begin()];

  // Dust amplitude
  double AdustTT = nuisance[++it-_n.begin()];
  double AdustPP = nuisance[++it-_n.begin()];
  double AdustTP = nuisance[++it-_n.begin()];

  // kSZ amplitude
  double Aksz = nuisance[++it-_n.begin()];

  // tSZxCIB amplitude
  double Aszxcib = nuisance[++it-_n.begin()];

#ifdef PolarizationEfficiency
  // Polarization efficiency
  vector<double> epsilon(_nMap);
  for(unsigned int i = 0; i < _nMap; i++) epsilon[i] = nuisance[++it-_n.begin()];
  // End: gathering information regarding the parameters
#endif

  // Begin: Residual
  unsigned int modeOffset = 0;

  vector<vector<vector<double> > > residual(5), weight(5), instrumental(5);
  for(unsigned int m = 0; m < 5; m++) {
    residual[m].resize(_nXFreq); weight[m].resize(_nXFreq); instrumental[m].resize(_nXFreq);
    for(unsigned int f = 0; f < _nXFreq; f++) {
      residual[m][f].resize(_maxOflmax[0]+1,0);
      weight[m][f].resize(_maxOflmax[0]+1,0);
      instrumental[m][f].resize(_maxOflmax[0]+1,0);
    }
  }

  unsigned int lminXFreq, lmaxXFreq, lminXSpectra, lmaxXSpectra = 0;

  for(unsigned int m = 0; m < 5; m++) {
    if( _modeStatus[m] ) {
      modeOffset = m*(_maxOflmax[0]+1);
      for(unsigned int f = 0; f < _nXFreq; f++) {
        if( _XFreqStatus[m][f] ) {
#ifdef FullRange
          lminXFreq = 50;
          lmaxXFreq = 2500;
#else
          lminXFreq = _lminXFreq[m][f];
          lmaxXFreq = _lmaxXFreq[m][f];
#endif
          for(unsigned int l = lminXFreq; l <= lmaxXFreq; l++) {
            double foregrounds = 0;
            if( m == 0 ) foregrounds = Aps[f]+Asz*_ClSZ[f][l]+Acib*_ClCIB[f][l]+AdustTT*_ClDust[f][modeOffset+l]+Aksz*_ClkSZ[l]+Aszxcib*_ClSZxCIB[f][l];
            if( m == 1 || m == 2 ) foregrounds = AdustPP*_ClDust[f][modeOffset+l];
            if( m == 3 || m == 4 ) foregrounds = AdustTP*_ClDust[f][modeOffset+l];

            double tmpCal, tmpBeamEigenmodes, tmpWeight, tmpPolEffPP, tmpPolEffTP, tmpPolEffPT = 0.;
            for(unsigned int c = 0; c < _XFreq2XSpectra[f].size(); c++) {
#ifdef FullRange
              lminXSpectra = 50;
              lmaxXSpectra = 2500;
#else
              lminXSpectra = _lminXSpectra[m][_XFreq2XSpectra[f][c]];
              lmaxXSpectra = _lmaxXSpectra[m][_XFreq2XSpectra[f][c]];
#endif
              if( _XSpectraStatus[m][_XFreq2XSpectra[f][c]] && l >= lminXSpectra && l <= lmaxXSpectra ) {
                tmpWeight = _ClWeightData[_XFreq2XSpectra[f][c]][modeOffset+l];
                tmpBeamEigenmodes = pow(1+beta[_XFreq2XSpectra[f][c]]*_beamEigenmodes[_XFreq2XSpectra[f][c]][l],2);
                tmpCal = 1+cal[_XSpectra2Maps[_XFreq2XSpectra[f][c]][0]]+cal[_XSpectra2Maps[_XFreq2XSpectra[f][c]][1]];
#ifdef PolarizationEfficiency 
                if( m == 0 ) {
                  residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                  instrumental[m][f][l] += tmpWeight*tmpCal*tmpBeamEigenmodes;
                }
                if( m == 1 || m == 2 ) {
                  tmpPolEffPP = (1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][0]])*(1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][1]]);
                  residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpPolEffPP*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                  instrumental[m][f][l] += tmpWeight*tmpCal*tmpPolEffPP*tmpBeamEigenmodes;
                }
                if( m == 3 ) {
                  tmpPolEffTP = 1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][1]];
                  residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpPolEffTP*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                  instrumental[m][f][l] += tmpWeight*tmpCal*tmpPolEffTP*tmpBeamEigenmodes; 
                }
                if( m == 4 ) {
                  tmpPolEffPT = 1+epsilon[_XSpectra2Maps[_XFreq2XSpectra[f][c]][0]];
                  residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpPolEffPT*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                  instrumental[m][f][l] += tmpWeight*tmpCal*tmpPolEffPT*tmpBeamEigenmodes;
                }
#else
                residual[m][f][l] += tmpWeight*(_ClData[_XFreq2XSpectra[f][c]][modeOffset+l]-tmpCal*tmpBeamEigenmodes*(ClCMB[modeOffset+l]+foregrounds));
                instrumental[m][f][l] += tmpWeight*tmpCal*tmpBeamEigenmodes;
#endif
                weight[m][f][l] += tmpWeight;
              }
            }
          }
        }
      }
    }
  }

  for(unsigned int m = 0; m < 4; m++) {
    if( _typeStatus[m] ) {
      for(unsigned int f = 0; f < _nXFreq; f++) {
        if( _XFreqStatus[m][f] ) {
#ifdef FullRange
          lminXFreq = 50;
          lmaxXFreq = 2500;
#else
          lminXFreq = _lminXFreq[m][f];
          lmaxXFreq = _lmaxXFreq[m][f];
#endif
          for(unsigned int l = lminXFreq; l <= lmaxXFreq; l++) {
            if( m < 3) {
              instrumental[m][f][l] /= weight[m][f][l];
              residual[m][f][l] /= weight[m][f][l];
            }
            else {
              instrumental[3][f][l]  = (instrumental[3][f][l]+instrumental[4][f][l])/(weight[3][f][l]+weight[4][f][l]);
              residual[3][f][l]  = (residual[3][f][l]+residual[4][f][l])/(weight[3][f][l]+weight[4][f][l]);
            }
          }
        }
      }
    }
  }
  residual.resize(4);
  instrumental.resize(4);
  // End: Residuals

1279
*/
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1280 1281 1282

  // Begin: output
  cout.precision(16);
1283
  char tmpPar[32];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1284 1285 1286 1287 1288 1289 1290 1291 1292

  string CMBmode[] = {"TT","EE","BB","TE"};

  cout << "writing output in " << _fileOut << endl;

  ofstream of(_fileOut.c_str());

  // Begin: header
  of << "#Chi2 = " << _chi2 << endl;
1293 1294 1295 1296 1297 1298 1299 1300
  of << "#Multipole";
  for(unsigned int m = 0; m < 4; m++) of << "\t CMB" << CMBmode[m];
  for(unsigned int m = 0; m < 4; m++) 
    if( _modeStatus[m] || (m==3 && (_modeStatus[3] || _modeStatus[4])))
      for(unsigned int f = 0; f < _nXFreq; f++) {
	of << "\t Residual"     << CMBmode[m] << _XFreq[f];
	//of << "\t Instrumental" << CMBmode[m] << _XFreq[f];
      }
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1301 1302 1303
  for(unsigned int f = 0; f < _nXFreq; f++) of << "\t PS"  << _XFreq[f];
  for(unsigned int f = 0; f < _nXFreq; f++) of << "\t SZ"  << _XFreq[f];
  for(unsigned int f = 0; f < _nXFreq; f++) of << "\t CIB" << _XFreq[f];
1304 1305 1306
  for(unsigned int m = 0; m < 4; m++) 
    if( _modeStatus[m] || (m==3 && (_modeStatus[3] || _modeStatus[4])))
      for(unsigned int f = 0; f < _nXFreq; f++) of << "\t Dust" << CMBmode[m] << _XFreq[f];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1307 1308 1309 1310 1311 1312 1313 1314 1315
  of << "\t kSZ";
  for(unsigned int f = 0; f < _nXFreq; f++) of << "\t SZxCIB" << _XFreq[f];
  of << endl;
  // End: header

  for(unsigned int l = 0; l < _maxOflmax[0]+1; l++) {
    of << l;
    for(unsigned int m = 0; m < 4; m++) of << " " << ClCMB[m*(_maxOflmax[0]+1)+l];
    for(unsigned int m = 0; m < 4; m++) {
1316 1317 1318 1319 1320
      if( _modeStatus[m] || (m==3 && (_modeStatus[3] || _modeStatus[4])))
	for(unsigned int f = 0; f < _nXFreq; f++) {
	  if( _XFreqStatus[m][f] ) of << " " << _residual[m][f][l]; // << " " << _instrumental[m][f][l]; 
	  else of << " " << 0;// << " " << 0;
	}
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1321
    }
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    for(unsigned int f = 0; f < _nXFreq; f++) {
      sprintf(tmpPar,"Aps%s",_XFreq[f].c_str());
      of << " " << nuisance[getIndex(tmpPar)];
    }
    for(unsigned int f = 0; f < _nXFreq; f++) of << " " << nuisance[getIndex("Asz")]*_ClSZ[f][l];
    for(unsigned int f = 0; f < _nXFreq; f++) of << " " <<  nuisance[getIndex("Acib")]*_ClCIB[f][l];
    for(unsigned int m = 0; m < 4; m++) {
      if( _modeStatus[m] || (m==3 && (_modeStatus[3] || _modeStatus[4])))
	for(unsigned int f = 0; f < _nXFreq; f++) {
	  if( m == 0 ) of << " " << nuisance[getIndex("AdustTT")]*_ClDust[f][m*(_maxOflmax[0]+1)+l];
	  if( m == 1 || m == 2 ) of << " " << nuisance[getIndex("AdustPP")]*_ClDust[f][m*(_maxOflmax[0]+1)+l];
	  if( m == 3 &&  _modeStatus[3] && !_modeStatus[4] ) of << " " << nuisance[getIndex("AdustTP")]*_ClDust[f][3*(_maxOflmax[0]+1)+l];
	  if( m == 3 && !_modeStatus[3] &&  _modeStatus[4] ) of << " " << nuisance[getIndex("AdustTP")]*_ClDust[f][4*(_maxOflmax[0]+1)+l];
	  if( m == 3 &&  _modeStatus[3] &&  _modeStatus[4] ) of << " " << nuisance[getIndex("AdustTP")]*(_ClDust[f][3*(_maxOflmax[0]+1)+l]+_ClDust[f][4*(_maxOflmax[0]+1)+l])/2.;
	}
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1337
    }
1338 1339
    of << " " << nuisance[getIndex("Aksz")]*_ClkSZ[l];
    for(unsigned int f = 0; f < _nXFreq; f++) of << " " << nuisance[getIndex("Aszxcib")]*_ClSZxCIB[f][l];
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
1340 1341 1342 1343 1344 1345
    of << endl;
  }
  of.close();
  // End: output
}