| 1 | /* ======================================================================== *\ | 
|---|
| 2 | ! | 
|---|
| 3 | ! * | 
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction | 
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful | 
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes. | 
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY. | 
|---|
| 8 | ! * | 
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its | 
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee, | 
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and | 
|---|
| 12 | ! * that both that copyright notice and this permission notice appear | 
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express | 
|---|
| 14 | ! * or implied warranty. | 
|---|
| 15 | ! * | 
|---|
| 16 | ! | 
|---|
| 17 | ! | 
|---|
| 18 | !   Author(s): Javier López  04/2004 <mailto:jlopez@ifae.es> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2004 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | // MSrcPosFromFile | 
|---|
| 28 | // | 
|---|
| 29 | // Task to calculate the position of the source as a function of run number | 
|---|
| 30 | // | 
|---|
| 31 | //  Output Containers: | 
|---|
| 32 | //    MSrcPosCam | 
|---|
| 33 | // | 
|---|
| 34 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 35 |  | 
|---|
| 36 | #include <fstream> | 
|---|
| 37 |  | 
|---|
| 38 | #include <TH2F.h> | 
|---|
| 39 |  | 
|---|
| 40 | #include "MParList.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include "MSrcPosFromFile.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include "MRawRunHeader.h" | 
|---|
| 45 | #include "MSrcPosCam.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include "MLog.h" | 
|---|
| 48 | #include "MLogManip.h" | 
|---|
| 49 |  | 
|---|
| 50 | ClassImp(MSrcPosFromFile); | 
|---|
| 51 |  | 
|---|
| 52 | using namespace std; | 
|---|
| 53 |  | 
|---|
| 54 | static const TString gsDefName  = "MSrcPosFromFile"; | 
|---|
| 55 | static const TString gsDefTitle = "Calculate position of the (off axis) source"; | 
|---|
| 56 |  | 
|---|
| 57 | // ------------------------------------------------------------------------- | 
|---|
| 58 | // | 
|---|
| 59 | // Default constructor | 
|---|
| 60 | // | 
|---|
| 61 | MSrcPosFromFile::MSrcPosFromFile(TString cardpath, OnOffMode_t mode, const char *name, const char *title) | 
|---|
| 62 | : fRawRunHeader(NULL), fSrcPos(NULL), fMode(mode), fSourcePositionFilePath(cardpath) | 
|---|
| 63 | { | 
|---|
| 64 | fName  = name  ? name  : gsDefName.Data(); | 
|---|
| 65 | fTitle = title ? title : gsDefTitle.Data(); | 
|---|
| 66 |  | 
|---|
| 67 | fLastRun = 0; | 
|---|
| 68 |  | 
|---|
| 69 | // Count the number of runs in the card with the source poistions | 
|---|
| 70 | ReadSourcePositionsFile(kCount); | 
|---|
| 71 |  | 
|---|
| 72 | fRunList = new Int_t[fNumRuns]; | 
|---|
| 73 | fRunSrcPos = new MSrcPosCam[fNumRuns]; | 
|---|
| 74 | fRunMap = new TExMap(fNumRuns); | 
|---|
| 75 |  | 
|---|
| 76 | Float_t cameraSize   = 600; //[mm] | 
|---|
| 77 | Float_t binPrecision =  3;  //[mm] ~ 0.01 deg | 
|---|
| 78 |  | 
|---|
| 79 | UInt_t nbins =  (UInt_t)(cameraSize*2/binPrecision); | 
|---|
| 80 |  | 
|---|
| 81 | fHistSrcPos = new TH2F("HistSrcPos","",nbins,-cameraSize,cameraSize,nbins,-cameraSize,cameraSize); | 
|---|
| 82 |  | 
|---|
| 83 | // Read card with the source poistions | 
|---|
| 84 | ReadSourcePositionsFile(kRead); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | MSrcPosFromFile::~MSrcPosFromFile() | 
|---|
| 88 | { | 
|---|
| 89 | delete [] fRunList; | 
|---|
| 90 | delete [] fRunSrcPos; | 
|---|
| 91 | delete fRunMap; | 
|---|
| 92 | delete fHistSrcPos; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | // ------------------------------------------------------------------------- | 
|---|
| 96 | // | 
|---|
| 97 | Int_t MSrcPosFromFile::PreProcess(MParList *pList) | 
|---|
| 98 | { | 
|---|
| 99 |  | 
|---|
| 100 | fRawRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader")); | 
|---|
| 101 | if (!fRawRunHeader) | 
|---|
| 102 | { | 
|---|
| 103 | *fLog << err << AddSerialNumber("MRawRunHeader") << " not found ... aborting" << endl; | 
|---|
| 104 | return kFALSE; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam"); | 
|---|
| 108 | if (!fSrcPos) | 
|---|
| 109 | return kFALSE; | 
|---|
| 110 |  | 
|---|
| 111 | return kTRUE; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | // -------------------------------------------------------------------------- | 
|---|
| 115 | // | 
|---|
| 116 | // | 
|---|
| 117 | Bool_t MSrcPosFromFile::ReInit(MParList *pList) | 
|---|
| 118 | { | 
|---|
| 119 | return kTRUE; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | // -------------------------------------------------------------------------- | 
|---|
| 123 | // | 
|---|
| 124 | // | 
|---|
| 125 | Int_t MSrcPosFromFile::Process() | 
|---|
| 126 | { | 
|---|
| 127 |  | 
|---|
| 128 | switch(fMode) | 
|---|
| 129 | { | 
|---|
| 130 | case kOn: | 
|---|
| 131 | { | 
|---|
| 132 | const UInt_t run = fRawRunHeader->GetRunNumber(); | 
|---|
| 133 |  | 
|---|
| 134 | MSrcPosCam* srcpos = (MSrcPosCam*)fRunMap->GetValue(run); | 
|---|
| 135 |  | 
|---|
| 136 | Float_t x; | 
|---|
| 137 | Float_t y; | 
|---|
| 138 |  | 
|---|
| 139 | if (srcpos) | 
|---|
| 140 | { | 
|---|
| 141 | x = srcpos->GetX(); | 
|---|
| 142 | y = srcpos->GetY(); | 
|---|
| 143 |  | 
|---|
| 144 |  | 
|---|
| 145 | if (srcpos && run != fLastRun) | 
|---|
| 146 | { | 
|---|
| 147 | fSrcPos->SetXY(x,y); | 
|---|
| 148 |  | 
|---|
| 149 | *fLog << inf << "Source position for run " << run; | 
|---|
| 150 | *fLog << inf << "\tX\t" << setprecision(2) << x; | 
|---|
| 151 | *fLog << inf << "\tY\t" << setprecision(2) << y << endl; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | fLastRun = run; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | x = fSrcPos->GetX(); | 
|---|
| 158 | y = fSrcPos->GetY(); | 
|---|
| 159 |  | 
|---|
| 160 | fHistSrcPos->Fill(x,y); | 
|---|
| 161 | break; | 
|---|
| 162 | } | 
|---|
| 163 | case kOff: | 
|---|
| 164 | { | 
|---|
| 165 | Axis_t x; | 
|---|
| 166 | Axis_t y; | 
|---|
| 167 |  | 
|---|
| 168 | fHistSrcPos->GetRandom2(x,y); | 
|---|
| 169 | fSrcPos->SetXY(x,y); | 
|---|
| 170 |  | 
|---|
| 171 | break; | 
|---|
| 172 | } | 
|---|
| 173 | default: | 
|---|
| 174 | *fLog << err << "Wrond mode " << fMode << endl; | 
|---|
| 175 | return kFALSE; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | return kTRUE; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 |  | 
|---|
| 182 | Int_t MSrcPosFromFile::PostProcess() | 
|---|
| 183 | { | 
|---|
| 184 |  | 
|---|
| 185 | *fLog << dbg << endl; | 
|---|
| 186 | *fLog << dbg << "fHistSrcPos->GetEntries() " << fHistSrcPos->GetEntries() << endl; | 
|---|
| 187 | *fLog << dbg << "fHistSrcPos->ProjectionX()->GetMean() " << fHistSrcPos->ProjectionX()->GetMean() << endl; | 
|---|
| 188 | *fLog << dbg << "fHistSrcPos->ProjectionX()->GetRMS() " << fHistSrcPos->ProjectionX()->GetRMS() << endl; | 
|---|
| 189 | *fLog << dbg << "fHistSrcPos->ProjectionY()->GetMean() " << fHistSrcPos->ProjectionY()->GetMean() << endl; | 
|---|
| 190 | *fLog << dbg << "fHistSrcPos->ProjectionY()->GetRMS() " << fHistSrcPos->ProjectionY()->GetRMS() << endl; | 
|---|
| 191 |  | 
|---|
| 192 | return kTRUE; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 |  | 
|---|
| 196 | Int_t MSrcPosFromFile::ReadSourcePositionsFile(UShort_t readmode) | 
|---|
| 197 | { | 
|---|
| 198 |  | 
|---|
| 199 | ifstream fin(fSourcePositionFilePath); | 
|---|
| 200 | if(!fin) | 
|---|
| 201 | { | 
|---|
| 202 | *fLog << err << "MSrcPosFromFile::ReadSourcePositionsFile. Cannot open file " << fSourcePositionFilePath << endl; | 
|---|
| 203 | return kFALSE; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | UInt_t run; | 
|---|
| 207 | Float_t x,y; | 
|---|
| 208 |  | 
|---|
| 209 | fNumRuns=0; | 
|---|
| 210 |  | 
|---|
| 211 | *fLog << dbg << "MSrcPosFromFile::ReadSourcePositionsFile(" << readmode << ')' << endl; | 
|---|
| 212 | while(1) | 
|---|
| 213 | { | 
|---|
| 214 | fin >> run >> x >> y; | 
|---|
| 215 | if(fin.eof()) | 
|---|
| 216 | break; | 
|---|
| 217 |  | 
|---|
| 218 | switch(readmode) | 
|---|
| 219 | { | 
|---|
| 220 | case kCount: | 
|---|
| 221 | { | 
|---|
| 222 |  | 
|---|
| 223 | *fLog << dbg << "Source position for run " << run; | 
|---|
| 224 | *fLog << dbg << "\tX\t" << x << " mm"; | 
|---|
| 225 | *fLog << dbg << "\tY\t" << y << " mm" << endl; | 
|---|
| 226 |  | 
|---|
| 227 | fNumRuns++; | 
|---|
| 228 | break; | 
|---|
| 229 | } | 
|---|
| 230 | case kRead: | 
|---|
| 231 | { | 
|---|
| 232 | fRunList[fNumRuns] = run; | 
|---|
| 233 | fRunSrcPos[fNumRuns].SetXY(x,y); | 
|---|
| 234 | fRunMap->Add(fRunList[fNumRuns],(Long_t)&(fRunSrcPos[fNumRuns])); | 
|---|
| 235 | fNumRuns++; | 
|---|
| 236 | break; | 
|---|
| 237 | } | 
|---|
| 238 | default: | 
|---|
| 239 | *fLog << err << "Read mode " << readmode << " node defined" << endl; | 
|---|
| 240 | return kFALSE; | 
|---|
| 241 | } | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | fin.close(); | 
|---|
| 245 |  | 
|---|
| 246 |  | 
|---|
| 247 | return kTRUE; | 
|---|
| 248 | } | 
|---|